Class: ActiveData::Model::Attributes::Localized

Inherits:
Attribute show all
Defined in:
lib/active_data/model/attributes/localized.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

#locale_query(locale) ⇒ Object



35
36
37
38
# File 'lib/active_data/model/attributes/localized.rb', line 35

def locale_query(locale)
  value = read_locale(locale)
  !(value.respond_to?(:zero?) ? value.zero? : value.blank?)
end

#readObject



5
6
7
8
9
# File 'lib/active_data/model/attributes/localized.rb', line 5

def read
  @value ||= Hash[read_before_type_cast.map do |locale, value|
    [locale.to_s, normalize(enumerize(typecast(value)))]
  end]
end

#read_before_type_castObject



11
12
13
14
15
# File 'lib/active_data/model/attributes/localized.rb', line 11

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

#read_locale(locale) ⇒ Object



23
24
25
26
27
# File 'lib/active_data/model/attributes/localized.rb', line 23

def read_locale(locale)
  read[owner.class.fallbacks(locale).detect do |fallback|
    read[fallback.to_s]
  end.to_s]
end

#read_locale_before_type_cast(locale) ⇒ Object



29
30
31
32
33
# File 'lib/active_data/model/attributes/localized.rb', line 29

def read_locale_before_type_cast(locale)
  read_before_type_cast[owner.class.fallbacks(locale).detect do |fallback|
    read_before_type_cast[fallback.to_s]
  end.to_s]
end

#write_locale(value, locale) ⇒ Object



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

def write_locale(value, locale)
  pollute do
    write(read.merge(locale.to_s => value))
  end
end