Class: HasLocalizationTable::ActiveRecord::Attributes::Cache

Inherits:
Hash
  • Object
show all
Defined in:
lib/has_localization_table/active_record/attributes/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Cache

Returns a new instance of Cache.



7
8
9
10
# File 'lib/has_localization_table/active_record/attributes/cache.rb', line 7

def initialize(klass)
  @klass = klass
  reset
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/has_localization_table/active_record/attributes/cache.rb', line 5

def klass
  @klass
end

Instance Method Details

#changed?(attr, locale) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/has_localization_table/active_record/attributes/cache.rb', line 29

def changed?(attr, locale)
  localization = klass.localization_for(locale)
  return false unless localization

  localization.send(attr) != self[attr.to_sym][locale.id]
end

#clear(attr, locale) ⇒ Object



21
22
23
# File 'lib/has_localization_table/active_record/attributes/cache.rb', line 21

def clear(attr, locale)
  self[attr.to_sym].delete(locale.id)
end

#get(attr, locale) ⇒ Object



12
13
14
15
# File 'lib/has_localization_table/active_record/attributes/cache.rb', line 12

def get(attr, locale)
  clear(attr, locale) if changed?(attr, locale)
  self[attr.to_sym][locale.id] ||= yield
end

#resetObject



25
26
27
# File 'lib/has_localization_table/active_record/attributes/cache.rb', line 25

def reset
  replace(klass.localized_attributes.inject({}) { |memo, attr| memo[attr] = {}; memo })
end

#set(attr, locale, value) ⇒ Object



17
18
19
# File 'lib/has_localization_table/active_record/attributes/cache.rb', line 17

def set(attr, locale, value)
  self[attr.to_sym][locale.id] = value
end