Class: Globalize::ActiveRecord::Adapter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Adapter

Returns a new instance of Adapter.



8
9
10
11
12
# File 'lib/globalize/active_record/adapter.rb', line 8

def initialize(record)
  @record = record
  @cache = Attributes.new
  @stash = Attributes.new
end

Instance Attribute Details

#cacheObject (readonly)

The cache caches attributes that already were looked up for read access. The stash keeps track of new or changed values that need to be saved.



6
7
8
# File 'lib/globalize/active_record/adapter.rb', line 6

def cache
  @cache
end

#recordObject (readonly)

The cache caches attributes that already were looked up for read access. The stash keeps track of new or changed values that need to be saved.



6
7
8
# File 'lib/globalize/active_record/adapter.rb', line 6

def record
  @record
end

#stashObject (readonly)

The cache caches attributes that already were looked up for read access. The stash keeps track of new or changed values that need to be saved.



6
7
8
# File 'lib/globalize/active_record/adapter.rb', line 6

def stash
  @stash
end

Instance Method Details

#fetch(locale, attr_name) ⇒ Object



14
15
16
17
18
# File 'lib/globalize/active_record/adapter.rb', line 14

def fetch(locale, attr_name)
  cache.contains?(locale, attr_name) ?
    cache.read(locale, attr_name) :
    cache.write(locale, attr_name, fetch_attribute(locale, attr_name))
end

#resetObject



34
35
36
# File 'lib/globalize/active_record/adapter.rb', line 34

def reset
  cache.clear
end

#save_translations!Object



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

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

#write(locale, attr_name, value) ⇒ Object



20
21
22
23
# File 'lib/globalize/active_record/adapter.rb', line 20

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