Class: ActiveModel::LazyAttributeHash

Inherits:
Object
  • Object
show all
Defined in:
activemodel/lib/active_model/attribute_set/builder.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(types, values, additional_types, default_attributes, delegate_hash = {}) ⇒ LazyAttributeHash

Returns a new instance of LazyAttributeHash.



97
98
99
100
101
102
103
104
# 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 Method Details

#==(other) ⇒ Object



134
135
136
137
138
139
140
# 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) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#deep_dupObject



118
119
120
121
122
# 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) ⇒ Object



129
130
131
132
# 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(_) ⇒ Object



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# 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_dumpObject



142
143
144
# 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) ⇒ Object



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

def marshal_load(values)
  initialize(*values)
end