123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::LazyAttributeSet

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/builder.rb

Class Method Summary

Instance Attribute Summary

AttributeSet - Inherited

Instance Method Summary

Constructor Details

.new(values, types, additional_types, default_attributes, attributes = {}) ⇒ LazyAttributeSet

[ GitHub ]

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

def initialize(values, types, additional_types, default_attributes, attributes = {})
  super(attributes)
  @values = values
  @types = types
  @additional_types = additional_types
  @default_attributes = default_attributes
  @casted_values = {}
  @materialized = false
end

Instance Attribute Details

#additional_types (readonly, private)

[ GitHub ]

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

attr_reader :values, :types, :additional_types, :default_attributes

#default_attributes (readonly, private)

[ GitHub ]

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

attr_reader :values, :types, :additional_types, :default_attributes

#types (readonly, private)

[ GitHub ]

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

attr_reader :values, :types, :additional_types, :default_attributes

#values (readonly, private)

[ GitHub ]

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

attr_reader :values, :types, :additional_types, :default_attributes

Instance Method Details

#attributes (protected)

[ GitHub ]

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

def attributes
  unless @materialized
    values.each_key { |key| self[key] }
    types.each_key { |key| self[key] }
    @materialized = true
  end
  @attributes
end

#default_attribute(name, value_present = true, value = values.fetch(name) { value_present = false }) (private)

[ GitHub ]

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

def default_attribute(
  name,
  value_present = true,
  value = values.fetch(name) { value_present = false }
)
  type = additional_types.fetch(name, types[name])

  if value_present
    @attributes[name] = Attribute.from_database(name, value, type, @casted_values[name])
  elsif types.key?(name)
    if attr = default_attributes[name]
      @attributes[name] = attr.dup
    else
      @attributes[name] = Attribute.uninitialized(name, type)
    end
  else
    Attribute.null(name)
  end
end

#fetch_value(name, &block)

[ GitHub ]

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

def fetch_value(name, &block)
  if attr = @attributes[name]
    return attr.value(&block)
  end

  @casted_values.fetch(name) do
    value_present = true
    value = values.fetch(name) { value_present = false }

    if value_present
      type = additional_types.fetch(name, types[name])
      @casted_values[name] = type.deserialize(value)
    else
      attr = default_attribute(name, value_present, value)
      attr.value(&block)
    end
  end
end

#key?(name) ⇒ Boolean

[ GitHub ]

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

def key?(name)
  (values.key?(name) || types.key?(name) || @attributes.key?(name)) && self[name].initialized?
end

#keys

[ GitHub ]

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

def keys
  keys = values.keys | types.keys | @attributes.keys
  keys.keep_if { |name| self[name].initialized? }
end