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.



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



69
70
71
72
73
74
75
# File 'lib/active_record/attribute_set/builder.rb', line 69

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

#initialize_dup(_) ⇒ Object



54
55
56
57
# File 'lib/active_record/attribute_set/builder.rb', line 54

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

#initialized_keysObject



50
51
52
# File 'lib/active_record/attribute_set/builder.rb', line 50

def initialized_keys
  delegate_hash.keys | values.keys
end

#key?(key) ⇒ Boolean

Returns:

  • (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

#selectObject



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

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