Class: ActiveRecord::LazyAttributeHash

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(types, values, additional_types) ⇒ LazyAttributeHash

Returns a new instance of LazyAttributeHash.



25
26
27
28
29
30
31
# File 'lib/active_record/attribute_set/builder.rb', line 25

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

Instance Method Details

#[](key) ⇒ Object



37
38
39
# File 'lib/active_record/attribute_set/builder.rb', line 37

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

#[]=(key, value) ⇒ Object



41
42
43
44
45
46
# File 'lib/active_record/attribute_set/builder.rb', line 41

def []=(key, value)
  if frozen?
    raise RuntimeError, "Can't modify frozen hash"
  end
  delegate_hash[key] = value
end

#initialize_dup(_) ⇒ Object



52
53
54
55
# File 'lib/active_record/attribute_set/builder.rb', line 52

def initialize_dup(_)
  @delegate_hash = delegate_hash.transform_values(&:dup)
  super
end

#initialized_keysObject



48
49
50
# File 'lib/active_record/attribute_set/builder.rb', line 48

def initialized_keys
  delegate_hash.keys | values.keys
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/active_record/attribute_set/builder.rb', line 33

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

#selectObject



57
58
59
60
61
62
63
64
65
# File 'lib/active_record/attribute_set/builder.rb', line 57

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