Class: Globalize::Model::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/globalize/model/active_record/adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Adapter

Returns a new instance of Adapter.



24
25
26
27
28
29
30
# File 'lib/globalize/model/active_record/adapter.rb', line 24

def initialize(record)
  @record = record
  
  # TODO what exactly are the roles of cache and stash
  @cache = AttributeStash.new
  @stash = AttributeStash.new
end

Instance Method Details

#clearObject

Clears the cache



57
58
59
60
# File 'lib/globalize/model/active_record/adapter.rb', line 57

def clear
  @cache.clear
  @stash.clear
end

#fetch(locale, attr_name) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/globalize/model/active_record/adapter.rb', line 32

def fetch(locale, attr_name)
  # locale = I18n.locale
  is_cached = @cache.contains?(locale, attr_name)
  is_cached ? @cache.read(locale, attr_name) : begin
    value = fetch_attribute locale, attr_name
    @cache.write locale, attr_name, value if value && value.locale == locale
    value
  end
end

#stash(locale, attr_name, value) ⇒ Object



42
43
44
45
# File 'lib/globalize/model/active_record/adapter.rb', line 42

def stash(locale, attr_name, value)
  @stash.write locale, attr_name, value
  @cache.write locale, attr_name, value
end

#update_translations!Object



47
48
49
50
51
52
53
54
# File 'lib/globalize/model/active_record/adapter.rb', line 47

def update_translations!
  @stash.each do |locale, attrs|
    translation = @record.globalize_translations.find_or_initialize_by_locale(locale.to_s)
    attrs.each{|attr_name, value| translation[attr_name] = value }
    translation.save!
  end
  @stash.clear
end