Class: ActiveModel::LazyIndexedAttributeSet
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
AttributeSet
|
|
|
Instance Chain:
self,
AttributeSet
|
|
| Inherits: |
ActiveModel::AttributeSet
|
| Defined in: | activemodel/lib/active_model/attribute_set.rb |
Overview
Attributes are cached in a plain ::Array, by the index of the attribute's
name in #indexes, which cover every attribute of the model plus any
extra columns (such as aliases) the row was loaded with.
Methods that take a name look its index up once and work by index from
there.
Constant Summary
-
UNCAST =
private
# File 'activemodel/lib/active_model/attribute_set.rb', line 289
Marks the slots in
@casted_valuesthat haven't been cast yet, since a value can be cast tonil.Object.new.freeze
Class Method Summary
-
.new(row, indexes, ctx) ⇒ LazyIndexedAttributeSet
constructor
rowis the raw data from the database, an::Arraylaid out by #indexes (seeIndexedRow), which also type any names the model doesn't have (like fromselect count(*) as blah).
AttributeSet - Inherited
Instance Attribute Summary
- #indexes readonly
AttributeSet - Inherited
Instance Method Summary
-
#[](name)
: (String) ->
Attribute - #[]=(name, attribute)
-
#deep_dup
: () ->
LazyIndexedAttributeSet -
#fetch_value(name, &block)
: (String) ?{ (String) -> untyped } -> untyped.
-
#fetch_value!(name)
: (String) -> untyped.
-
#fetch_value_at(index, &block)
Rows are laid out by slot, so the slots that generated reader methods pass are indexes into this set.
-
#fetch_value_at!(index)
: (Integer) -> untyped.
-
#initialize_copy(other)
: (ActiveModel::LazyIndexedAttributeSet) -> void.
-
#key?(name) ⇒ Boolean
: (String) -> bool.
-
#keys
: () -> Array.
-
#map(&block)
: () { (ActiveModel::Attribute) ->
Attribute} ->LazyIndexedAttributeSet -
#to_h
Alias for #to_hash.
-
#to_hash
(also: #to_h)
Same keys, in the same order, as #keys.
-
#write_cast_value(name, value)
: (String, untyped) ->
Attribute -
#write_from_database(name, value)
: (String, untyped) ->
Attribute -
#write_from_user(name, value)
: (String, untyped) -> untyped.
-
#attributes
protected
An IndexedRow of every attribute, for the methods that work on all of them.
-
#transform_attributes!
protected
Replaces each cached attribute with the block's result.
- #attribute_at(name, index) private
-
#cache_value_at(index, value)
private
Keeps the value of a built attribute (such as one the user wrote) where the generated readers look first.
-
#cast_value(name, index)
private
The value at
indexfor an attribute that hasn't been built or cast yet: cast from the row (and kept in@casted_values), or the block's result for the default attribute when the row doesn't have it. - #default_attribute(name, index, value_present = IndexedRow.key_at?(@row, index), value = (@row[index] if value_present)) private
-
#freeze_attributes
private
: () -> void.
-
#index_for_name(name)
private
#indexes has every name the set can hold, so a name without an index isn't an attribute.
-
#lookup(index)
private
: (Integer) ->
ActiveModel::Attribute? -
#materialize
private
: () -> void.
-
#row_keys
private
Every name the set can hold.
-
#store(index, attribute)
private
The attribute now holds the value, so the cast value is dropped.
-
#type_for(index)
private
: (Integer) ->
Type::Value -
#value_at(index, name, &block)
private
These are on the path of every attribute read, so they check the caches directly and only call out to cast a value the first time.
-
#value_at!(index, name)
private
: (Integer,
::String) -> untyped. -
#write(name)
private
Stores the block's result for the attribute
name.
AttributeSet - Inherited
| #==, | |
| #[] | : (String) -> |
| #[]= | |
| #accessed, #cast_types, | |
| #deep_dup | : () -> |
| #fetch_value, | |
| #fetch_value! | Like #fetch_value, but raises |
| #fetch_value_at | Like #fetch_value, but looks the attribute up by its |
| #fetch_value_at! | Like #fetch_value!, but looks the attribute up by its |
| #freeze | : () -> self. |
| #include? | Alias for AttributeSet#key?. |
| #indexes | : () -> (Hash[String, Integer] | |
| #initialize_clone, #initialize_dup, #key?, #keys, | |
| #map | : () { (ActiveModel::Attribute) -> |
| #name_at | The name of the attribute at |
| #reset, | |
| #to_h | Alias for AttributeSet#to_hash. |
| #to_hash, #values, #values_before_type_cast, #values_for_database, | |
| #write_cast_value | : (String, untyped) -> |
| #write_from_database | : (String, untyped) -> |
| #write_from_user | : (String, untyped) -> untyped. |
| #default_attribute, | |
| #freeze_attributes | : () -> void. |
| #lookup | : (String) -> |
| #raise_unless_laid_out | Records marshaled in the |
| #store | |
Constructor Details
.new(row, indexes, ctx) ⇒ LazyIndexedAttributeSet
row is the raw data from the database, an ::Array laid out by
#indexes (see IndexedRow), which also type any names the model
doesn't have (like from select count(*) as blah). ctx is a
Builder::Context, for looking up the model's attributes by index.
: (Array, Hash[String, Integer] | IndexedRow::ExtendedIndexes, AttributeSet::Builder::Context) -> void
# File 'activemodel/lib/active_model/attribute_set.rb', line 299
def initialize(row, indexes, ctx) # Attributes are only built when something needs one (a write, a # default, or #materialize), so @attributes starts out nil. super(nil) @row = row @ctx = ctx # Values cast from row, by the same index. Only values in the row # are cast, so this is never longer than the row. Slots with a built # attribute are UNCAST here, so reads check this first. @casted_values = Array.new(row.size, UNCAST) @materialized = false @indexes = indexes end
Instance Attribute Details
#indexes (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 292
attr_reader :indexes
Instance Method Details
#[](name)
: (String) -> Attribute
# File 'activemodel/lib/active_model/attribute_set.rb', line 320
def [](name) if index = index_for_name(name) attribute_at(name, index) else Attribute.null(name) end end
#[]=(name, attribute)
[ GitHub ]#attribute_at(name, index) (private)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 549
def attribute_at(name, index) lookup(index) || default_attribute(name, index) end
#attributes (protected)
An IndexedRow of every attribute, for the methods that work on all
of them. Built on each call, so writes must go through #store.
: () -> IndexedRow::Mutable
# File 'activemodel/lib/active_model/attribute_set.rb', line 457
def attributes materialize row = IndexedRow.unset_row(indexes.size) @attributes&.each_with_index { |attr, i| row[i] = attr if attr } IndexedRow::Mutable.new(indexes, row) end
#cache_value_at(index, value) (private)
Keeps the value of a built attribute (such as one the user wrote) where the generated readers look first. It's the attribute's own value, so changes made to it in place are still detected, and #store drops it when the attribute is replaced. : (Integer, untyped) -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 543
def cache_value_at(index, value) @casted_values[index] = value if index < @casted_values.size value end
#cast_value(name, index) (private)
The value at index for an attribute that hasn't been built or cast
yet: cast from the row (and kept in @casted_values), or the block's
result for the default attribute when the row doesn't have it.
: (String, ::Integer) { (ActiveModel::Attribute) -> untyped } -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 566
def cast_value(name, index) if IndexedRow.key_at?(@row, index) @casted_values[index] = type_for(index).deserialize(@row[index]) else yield default_attribute(name, index, false, nil) end end
#deep_dup
: () -> LazyIndexedAttributeSet
# File 'activemodel/lib/active_model/attribute_set.rb', line 443
def deep_dup map(&:deep_dup) end
#default_attribute(name, index, value_present = IndexedRow.key_at?(@row, index), value = (@row[index] if value_present)) (private)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_set.rb', line 593
def default_attribute( name, index, value_present = IndexedRow.key_at?(@row, index), value = (@row[index] if value_present) ) if value_present casted = @casted_values.fetch(index, UNCAST) casted = nil if UNCAST.equal?(casted) store(index, Attribute.from_database(name, value, type_for(index), casted)) elsif @ctx.type_key_at?(index) if attr = @ctx.default_at(index) store(index, attr.dup) else store(index, Attribute.uninitialized(name, type_for(index))) end else Attribute.null(name) end end
#fetch_value(name, &block)
: (String) ?{ (String) -> untyped } -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 374
def fetch_value(name, &block) index = index_for_name(name) return unless index value_at(index, name, &block) end
#fetch_value!(name)
: (String) -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 382
def fetch_value!(name) index = index_for_name(name) return unless index value_at!(index, name) end
#fetch_value_at(index, &block)
Rows are laid out by slot, so the slots that generated reader methods pass are indexes into this set. : (Integer) ?{ (String) -> untyped } -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 392
def fetch_value_at(index, &block) # Same as #value_at, inlined since generated readers call this. value = @casted_values.fetch(index, UNCAST) return value unless UNCAST.equal?(value) if (attrs = @attributes) && (attr = attrs[index]) attr.value(&block) else cast_value(@ctx.name_at(index), index) { |attribute| attribute.value(&block) } end end
#fetch_value_at!(index)
: (Integer) -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 405
def fetch_value_at!(index) value = @casted_values.fetch(index, UNCAST) return value unless UNCAST.equal?(value) if (attrs = @attributes) && (attr = attrs[index]) cache_value_at(index, attr.value!) else cast_value(@ctx.name_at(index), index, &:value!) end end
#freeze_attributes (private)
: () -> void
# File 'activemodel/lib/active_model/attribute_set.rb', line 586
def freeze_attributes materialize @attributes ||= Array.new(indexes.size) @attributes.freeze end
#index_for_name(name) (private)
#indexes has every name the set can hold, so a name without an index isn't an attribute. : (String) -> Integer?
# File 'activemodel/lib/active_model/attribute_set.rb', line 495
def index_for_name(name) indexes[name] end
#initialize_copy(other)
: (ActiveModel::LazyIndexedAttributeSet) -> void
# File 'activemodel/lib/active_model/attribute_set.rb', line 314
def initialize_copy(other) super @casted_values = @casted_values.dup end
#key?(name) ⇒ Boolean
: (String) -> bool
# File 'activemodel/lib/active_model/attribute_set.rb', line 334
def key?(name) index = index_for_name(name) return false unless index IndexedRow.key_at?(@row, index) || ((@ctx.type_key_at?(index) || !lookup(index).nil?) && attribute_at(name, index).initialized?) end
#keys
: () -> Array
# File 'activemodel/lib/active_model/attribute_set.rb', line 343
def keys keys = row_keys | @ctx.type_keys keys.keep_if do |name| index = index_for_name(name) if attribute = lookup(index) attribute.initialized? else IndexedRow.key_at?(@row, index) || attribute_at(name, index).initialized? end end end
#lookup(index) (private)
: (Integer) -> ActiveModel::Attribute?
# File 'activemodel/lib/active_model/attribute_set.rb', line 500
def lookup(index) @attributes&.[](index) end
#map(&block)
: () { (ActiveModel::Attribute) -> Attribute } -> LazyIndexedAttributeSet
# File 'activemodel/lib/active_model/attribute_set.rb', line 448
def map(&block) materialize dup.transform_attributes!(&block) end
#materialize (private)
: () -> void
# File 'activemodel/lib/active_model/attribute_set.rb', line 484
def materialize unless @materialized row_keys.each { |key| self[key] } @ctx.type_keys.each { |key| self[key] } @materialized = true end end
#row_keys (private)
Every name the set can hold. : () -> Array
#store(index, attribute) (private)
#to_h
Alias for #to_hash.
# File 'activemodel/lib/active_model/attribute_set.rb', line 371
alias :to_h :to_hash
#to_hash Also known as: #to_h
# File 'activemodel/lib/active_model/attribute_set.rb', line 358
def to_hash hash = {} indexes.each_key do |name| index = index_for_name(name) if !lookup(index) && IndexedRow.key_at?(@row, index) hash[name] = value_at(index, name) else attribute = attribute_at(name, index) hash[name] = attribute.value if attribute.initialized? end end hash end
#transform_attributes! (protected)
Replaces each cached attribute with the block's result. Only for
fresh copies, since it modifies @attributes in place. The new
attributes may hold new values, so their slots are uncast.
: () { (ActiveModel::Attribute) -> Attribute } -> self
# File 'activemodel/lib/active_model/attribute_set.rb', line 469
def transform_attributes! return self unless @attributes size = @casted_values.size @attributes.each_with_index do |attr, i| next unless attr @attributes[i] = yield(attr) @casted_values[i] = UNCAST if i < size end self end
#type_for(index) (private)
: (Integer) -> Type::Value
# File 'activemodel/lib/active_model/attribute_set.rb', line 575
def type_for(index) @ctx.type_at(index, @indexes) end
#value_at(index, name, &block) (private)
These are on the path of every attribute read, so they check the
caches directly and only call out to cast a value the first time.
: (Integer, ::String) ?{ (String) -> untyped } -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 515
def value_at(index, name, &block) value = @casted_values.fetch(index, UNCAST) return value unless UNCAST.equal?(value) if (attrs = @attributes) && (attr = attrs[index]) attr.value(&block) else cast_value(name, index) { |attribute| attribute.value(&block) } end end
#value_at!(index, name) (private)
: (Integer, ::String) -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 527
def value_at!(index, name) value = @casted_values.fetch(index, UNCAST) return value unless UNCAST.equal?(value) if (attrs = @attributes) && (attr = attrs[index]) cache_value_at(index, attr.value!) else cast_value(name, index, &:value!) end end
#write(name) (private)
Stores the block's result for the attribute name. Unknown names
get Attribute::Null, which raises before anything is stored.
: (String) { (ActiveModel::Attribute) -> Attribute } -> Attribute
# File 'activemodel/lib/active_model/attribute_set.rb', line 556
def write(name) index = index_for_name(name) attribute = index ? attribute_at(name, index) : Attribute.null(name) store(index, yield(attribute)) end
#write_cast_value(name, value)
: (String, untyped) -> Attribute
# File 'activemodel/lib/active_model/attribute_set.rb', line 438
def write_cast_value(name, value) write(name) { |attribute| attribute.with_cast_value(value) } end
#write_from_database(name, value)
: (String, untyped) -> Attribute
# File 'activemodel/lib/active_model/attribute_set.rb', line 417
def write_from_database(name, value) write(name) { |attribute| attribute.with_value_from_database(value) } end
#write_from_user(name, value)
: (String, untyped) -> untyped
# File 'activemodel/lib/active_model/attribute_set.rb', line 422
def write_from_user(name, value) raise FrozenError, "can't modify frozen attributes" if frozen? index = index_for_name(name) attribute = write(name) { |attr| attr.with_value_from_user(value) } # Cast now so the first read of the attribute stays on the fast path. # A mutable type's cast can copy the value (deserialize(serialize(v)) # for a serialized column) or run a coder that rejects it, so those # wait for the first read, which caches them (see #cache_value_at): # changes made in place to the assigned object before then are saved. if index < @casted_values.size && !attribute.type.mutable? @casted_values[index] = attribute.value end value end