123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::LazyIndexedAttributeSet

Do not use. This class is for internal use only.
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

Class Method Summary

AttributeSet - Inherited

Instance Attribute Summary

Instance Method Summary

AttributeSet - Inherited

#==,
#[]

: (String) -> Attribute

#[]=

: (String, Attribute) -> Attribute

#accessed, #cast_types,
#deep_dup

: () -> AttributeSet

#fetch_value,
#fetch_value!

Like #fetch_value, but raises MissingAttributeError if the attribute is uninitialized.

#fetch_value_at

Like #fetch_value, but looks the attribute up by its index in #indexes rather than by name.

#fetch_value_at!

Like #fetch_value!, but looks the attribute up by its index (see #fetch_value_at).

#freeze

: () -> self.

#include?
#indexes

: () -> (Hash[String, Integer] | IndexedRow::ExtendedIndexes).

#initialize_clone, #initialize_dup, #key?, #keys,
#map

: () { (ActiveModel::Attribute) -> Attribute } -> AttributeSet

#name_at

The name of the attribute at index.

#reset,
#to_h
#to_hash, #values, #values_before_type_cast, #values_for_database,
#write_cast_value

: (String, untyped) -> Attribute

#write_from_database

: (String, untyped) -> Attribute

#write_from_user

: (String, untyped) -> untyped.

#default_attribute,
#freeze_attributes

: () -> void.

#lookup

: (String) -> ActiveModel::Attribute?

#raise_unless_laid_out

Records marshaled in the ::Rails 6.1 format have their instance variables dumped directly.

#store

: (String, Attribute) -> Attribute

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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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)

: (String, Attribute) -> Attribute

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 329

def []=(name, attribute)
  store(indexes.fetch(name), attribute)
end

#attribute_at(name, index) (private)

: (String, ::Integer) -> Attribute

[ 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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)

: (String, ::Integer, ?bool, ?untyped) -> Attribute

[ 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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?

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 495

def index_for_name(name)
  indexes[name]
end

#initialize_copy(other)

: (ActiveModel::LazyIndexedAttributeSet) -> void

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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?

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 500

def lookup(index)
  @attributes&.[](index)
end

#map(&block)

: () { (ActiveModel::Attribute) -> Attribute } -> LazyIndexedAttributeSet

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 448

def map(&block)
  materialize
  dup.transform_attributes!(&block)
end

#materialize (private)

: () -> void

[ GitHub ]

  
# 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

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 581

def row_keys
  indexes.keys
end

#store(index, attribute) (private)

The attribute now holds the value, so the cast value is dropped. : (Integer, Attribute) -> Attribute

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 506

def store(index, attribute)
  (@attributes ||= Array.new(indexes.size))[index] = attribute
  @casted_values[index] = UNCAST if index < @casted_values.size
  attribute
end

#to_h

Alias for #to_hash.

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_set.rb', line 371

alias :to_h :to_hash

#to_hash Also known as: #to_h

Same keys, in the same order, as #keys. Values the row has are read like the generated readers do, without building an Attribute for each. : () -> Hash[String, untyped]

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

Raises:

  • (FrozenError)
[ GitHub ]

  
# 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