Class: ActiveModel::LazyAttributeSet

Inherits:
AttributeSet show all
Defined in:
lib/active_model/attribute_set/builder.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from AttributeSet

#==, #[], #[]=, #accessed, #cast_types, #deep_dup, #freeze, #initialize_clone, #initialize_dup, #map, #reset, #reverse_merge!, #to_hash, #values_before_type_cast, #values_for_database, #write_cast_value, #write_from_database, #write_from_user

Constructor Details

#initialize(values, types, additional_types, default_attributes, attributes = {}) ⇒ LazyAttributeSet

Returns a new instance of LazyAttributeSet.



22
23
24
25
26
27
28
29
30
# File 'lib/active_model/attribute_set/builder.rb', line 22

def initialize(values, types, additional_types, default_attributes, attributes = {})
  super(attributes)
  @values = values
  @types = types
  @additional_types = additional_types
  @default_attributes = default_attributes
  @casted_values = {}
  @materialized = false
end

Instance Method Details

#fetch_value(name, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_model/attribute_set/builder.rb', line 41

def fetch_value(name, &block)
  if attr = @attributes[name]
    return attr.value(&block)
  end

  @casted_values.fetch(name) do
    value_present = true
    value = values.fetch(name) { value_present = false }

    if value_present
      type = additional_types.fetch(name, types[name])
      @casted_values[name] = type.deserialize(value)
    else
      attr = default_attribute(name, value_present, value)
      attr.value(&block)
    end
  end
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/active_model/attribute_set/builder.rb', line 32

def key?(name)
  (values.key?(name) || types.key?(name) || @attributes.key?(name)) && self[name].initialized?
end

#keysObject



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

def keys
  keys = values.keys | types.keys | @attributes.keys
  keys.keep_if { |name| self[name].initialized? }
end