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.



10
11
12
13
14
# File 'lib/globalize/active_record/adapter.rb', line 10

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, name) ⇒ Object



16
17
18
19
20
# File 'lib/globalize/active_record/adapter.rb', line 16

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

#resetObject



36
37
38
# File 'lib/globalize/active_record/adapter.rb', line 36

def reset
  cache.clear
end

#save_translations!Object



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

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

#write(locale, name, value) ⇒ Object



22
23
24
25
# File 'lib/globalize/active_record/adapter.rb', line 22

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