Class: ActiveModel::LazyAttributeHash
- Defined in:
- activemodel/lib/active_model/attribute_set/builder.rb
Overview
:nodoc:
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #deep_dup ⇒ Object
-
#initialize(types, values, additional_types, default_attributes, delegate_hash = {}) ⇒ LazyAttributeHash
constructor
A new instance of LazyAttributeHash.
- #initialize_dup(_) ⇒ Object
- #key?(key) ⇒ Boolean
- #marshal_dump ⇒ Object
- #marshal_load(values) ⇒ Object
- #select ⇒ Object
Constructor Details
#initialize(types, values, additional_types, default_attributes, delegate_hash = {}) ⇒ LazyAttributeHash
Returns a new instance of LazyAttributeHash.
25 26 27 28 29 30 31 32 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 25 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
70 71 72 73 74 75 76 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 70 def ==(other) if other.is_a?(LazyAttributeHash) materialize == other.materialize else materialize == other end end |
#[](key) ⇒ Object
38 39 40 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 38 def [](key) delegate_hash[key] || assign_default_value(key) end |
#[]=(key, value) ⇒ Object
42 43 44 45 46 47 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 42 def []=(key, value) if frozen? raise RuntimeError, "Can't modify frozen hash" end delegate_hash[key] = value end |
#deep_dup ⇒ Object
49 50 51 52 53 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 49 def deep_dup dup.tap do |copy| copy.instance_variable_set(:@delegate_hash, delegate_hash.transform_values(&:dup)) end end |
#initialize_dup(_) ⇒ Object
55 56 57 58 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 55 def initialize_dup(_) @delegate_hash = Hash[delegate_hash] super end |
#key?(key) ⇒ Boolean
34 35 36 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 34 def key?(key) delegate_hash.key?(key) || values.key?(key) || types.key?(key) end |
#marshal_dump ⇒ Object
78 79 80 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 78 def marshal_dump [@types, @values, @additional_types, @default_attributes, @delegate_hash] end |
#marshal_load(values) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 82 def marshal_load(values) if values.is_a?(Hash) empty_hash = {}.freeze initialize(empty_hash, empty_hash, empty_hash, empty_hash, values) @materialized = true else initialize(*values) end end |
#select ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'activemodel/lib/active_model/attribute_set/builder.rb', line 60 def select keys = types.keys | values.keys | delegate_hash.keys keys.each_with_object({}) do |key, hash| attribute = self[key] if yield(key, attribute) hash[key] = attribute end end end |