class ActiveModel::LazyAttributeSet

:nodoc:

def attributes

def attributes
  unless @materialized
    values.each_key { |key| self[key] }
    types.each_key { |key| self[key] }
    @materialized = true
  end
  @attributes
end

def default_attribute(

def default_attribute(
  name,
  value_present = true,
  value = values.fetch(name) { value_present = false }
)
  type = additional_types.fetch(name, types[name])
  if value_present
    @attributes[name] = Attribute.from_database(name, value, type, @casted_values[name])
  elsif types.key?(name)
    if attr = default_attributes[name]
      @attributes[name] = attr.dup
    else
      @attributes[name] = Attribute.uninitialized(name, type)
    end
  else
    Attribute.null(name)
  end
end

def fetch_value(name, &block)

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

def initialize(values, types, additional_types, default_attributes, attributes = {})

:nodoc:
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

def key?(name)

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

def keys

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