Class: ActiveRecord::LazyAttributeHash
- Inherits:
-
Object
- Object
- ActiveRecord::LazyAttributeHash
- Defined in:
- lib/active_record/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) ⇒ LazyAttributeHash
constructor
A new instance of LazyAttributeHash.
- #initialize_dup(_) ⇒ Object
- #key?(key) ⇒ Boolean
- #select ⇒ Object
Constructor Details
#initialize(types, values, additional_types) ⇒ LazyAttributeHash
Returns a new instance of LazyAttributeHash.
27 28 29 30 31 32 33 |
# File 'lib/active_record/attribute_set/builder.rb', line 27 def initialize(types, values, additional_types) @types = types @values = values @additional_types = additional_types @materialized = false @delegate_hash = {} end |
Instance Method Details
#==(other) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/active_record/attribute_set/builder.rb', line 71 def ==(other) if other.is_a?(LazyAttributeHash) materialize == other.materialize else materialize == other end end |
#[](key) ⇒ Object
39 40 41 |
# File 'lib/active_record/attribute_set/builder.rb', line 39 def [](key) delegate_hash[key] || assign_default_value(key) end |
#[]=(key, value) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/active_record/attribute_set/builder.rb', line 43 def []=(key, value) if frozen? raise RuntimeError, "Can't modify frozen hash" end delegate_hash[key] = value end |
#deep_dup ⇒ Object
50 51 52 53 54 |
# File 'lib/active_record/attribute_set/builder.rb', line 50 def deep_dup dup.tap do |copy| copy.instance_variable_set(:@delegate_hash, delegate_hash.transform_values(&:dup)) end end |
#initialize_dup(_) ⇒ Object
56 57 58 59 |
# File 'lib/active_record/attribute_set/builder.rb', line 56 def initialize_dup(_) @delegate_hash = Hash[delegate_hash] super end |
#key?(key) ⇒ Boolean
35 36 37 |
# File 'lib/active_record/attribute_set/builder.rb', line 35 def key?(key) delegate_hash.key?(key) || values.key?(key) || types.key?(key) end |
#select ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/active_record/attribute_set/builder.rb', line 61 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 |