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, &default) ⇒ LazyAttributeHash
constructor
A new instance of LazyAttributeHash.
- #initialize_dup(_) ⇒ Object
- #key?(key) ⇒ Boolean
- #marshal_dump ⇒ Object
- #marshal_load(delegate_hash) ⇒ Object
- #select ⇒ Object
Constructor Details
#initialize(types, values, additional_types, &default) ⇒ LazyAttributeHash
Returns a new instance of LazyAttributeHash.
28 29 30 31 32 33 34 35 |
# File 'lib/active_record/attribute_set/builder.rb', line 28 def initialize(types, values, additional_types, &default) @types = types @values = values @additional_types = additional_types @materialized = false @delegate_hash = {} @default = default || proc {} end |
Instance Method Details
#==(other) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/active_record/attribute_set/builder.rb', line 73 def ==(other) if other.is_a?(LazyAttributeHash) materialize == other.materialize else materialize == other end end |
#[](key) ⇒ Object
41 42 43 |
# File 'lib/active_record/attribute_set/builder.rb', line 41 def [](key) delegate_hash[key] || assign_default_value(key) end |
#[]=(key, value) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/active_record/attribute_set/builder.rb', line 45 def []=(key, value) if frozen? raise RuntimeError, "Can't modify frozen hash" end delegate_hash[key] = value end |
#deep_dup ⇒ Object
52 53 54 55 56 |
# File 'lib/active_record/attribute_set/builder.rb', line 52 def deep_dup dup.tap do |copy| copy.instance_variable_set(:@delegate_hash, delegate_hash.transform_values(&:dup)) end end |
#initialize_dup(_) ⇒ Object
58 59 60 61 |
# File 'lib/active_record/attribute_set/builder.rb', line 58 def initialize_dup(_) @delegate_hash = Hash[delegate_hash] super end |
#key?(key) ⇒ Boolean
37 38 39 |
# File 'lib/active_record/attribute_set/builder.rb', line 37 def key?(key) delegate_hash.key?(key) || values.key?(key) || types.key?(key) end |
#marshal_dump ⇒ Object
81 82 83 |
# File 'lib/active_record/attribute_set/builder.rb', line 81 def marshal_dump materialize end |
#marshal_load(delegate_hash) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/active_record/attribute_set/builder.rb', line 85 def marshal_load(delegate_hash) @delegate_hash = delegate_hash @types = {} @values = {} @additional_types = {} @materialized = true end |
#select ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/active_record/attribute_set/builder.rb', line 63 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 |