123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::LazyAttributeHash

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activemodel/lib/active_model/attribute_set/builder.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(types, values, additional_types, default_attributes, delegate_hash = {}) ⇒ LazyAttributeHash

[ GitHub ]

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

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

Instance Attribute Details

#additional_types (readonly, private)

[ GitHub ]

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

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

#default_attributes (readonly, private)

[ GitHub ]

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

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

#delegate_hash (readonly, private)

[ GitHub ]

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

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

#each_value (readonly)

[ GitHub ]

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

delegate :transform_values, :each_value, :fetch, :except, to: :materialize

#except (readonly)

[ GitHub ]

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

delegate :transform_values, :each_value, :fetch, :except, to: :materialize

#fetch (readonly)

[ GitHub ]

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

delegate :transform_values, :each_value, :fetch, :except, to: :materialize

#transform_values (readonly)

[ GitHub ]

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

delegate :transform_values, :each_value, :fetch, :except, to: :materialize

#types (readonly, private)

[ GitHub ]

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

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

#values (readonly, private)

[ GitHub ]

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

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

Instance Method Details

#==(other)

[ GitHub ]

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

def ==(other)
  if other.is_a?(LazyAttributeHash)
    materialize == other.materialize
  else
    materialize == other
  end
end

#[](key)

[ GitHub ]

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

def [](key)
  delegate_hash[key] || assign_default_value(key)
end

#[]=(key, value)

[ GitHub ]

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

def []=(key, value)
  delegate_hash[key] = value
end

#assign_default_value(name) (private)

[ GitHub ]

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

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

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

#deep_dup

[ GitHub ]

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

def deep_dup
  dup.tap do |copy|
    copy.instance_variable_set(:@delegate_hash, delegate_hash.transform_values(&:dup))
  end
end

#each_key(&block)

[ GitHub ]

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

def each_key(&block)
  keys = types.keys | values.keys | delegate_hash.keys
  keys.each(&block)
end

#initialize_dup(_)

[ GitHub ]

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

def initialize_dup(_)
  @delegate_hash = Hash[delegate_hash]
  super
end

#key?(key) ⇒ Boolean

[ GitHub ]

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

def key?(key)
  delegate_hash.key?(key) || values.key?(key) || types.key?(key)
end

#marshal_dump

[ GitHub ]

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

def marshal_dump
  [@types, @values, @additional_types, @default_attributes, @delegate_hash]
end

#marshal_load(values)

[ GitHub ]

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

def marshal_load(values)
  initialize(*values)
end

#materialize (protected)

[ GitHub ]

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

def materialize
  unless @materialized
    values.each_key { |key| self[key] }
    types.each_key { |key| self[key] }
    unless frozen?
      @materialized = true
    end
  end
  delegate_hash
end