Class: ActiveData::Model::Attributes::Dictionary

Inherits:
Attribute
  • Object
show all
Defined in:
lib/active_data/model/attributes/dictionary.rb

Instance Attribute Summary

Attributes inherited from Base

#name, #owner

Instance Method Summary collapse

Methods inherited from Attribute

#default, #defaultize, #enum, #enumerize, #normalize, #write

Methods inherited from Base

#came_from_default?, #came_from_user?, #initialize, #inspect_attribute, #pollute, #query, #readonly?, #reflection, #reset, #typecast, #value_present?, #write, #write_value

Constructor Details

This class inherits a constructor from ActiveData::Model::Attributes::Base

Instance Method Details

#readObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/active_data/model/attributes/dictionary.rb', line 7

def read
  @value ||= begin
    hash = read_before_type_cast
    hash = hash.stringify_keys.slice(*keys) if keys.present?

    normalize(Hash[hash.map do |key, value|
      [key, enumerize(typecast(value))]
    end].with_indifferent_access).with_indifferent_access
  end
end

#read_before_type_castObject



18
19
20
21
22
# File 'lib/active_data/model/attributes/dictionary.rb', line 18

def read_before_type_cast
  @value_before_type_cast ||= Hash[(@value_cache.presence || {}).map do |key, value|
    [key, defaultize(value)]
  end].with_indifferent_access
end