Class: Globalize::ActiveRecord::Adapter
- Inherits:
-
Object
- Object
- Globalize::ActiveRecord::Adapter
- Defined in:
- lib/globalize/active_record/adapter.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
The cache caches attributes that already were looked up for read access.
-
#record ⇒ Object
readonly
The cache caches attributes that already were looked up for read access.
-
#stash ⇒ Object
readonly
The cache caches attributes that already were looked up for read access.
Instance Method Summary collapse
- #fetch(locale, name) ⇒ Object
-
#initialize(record) ⇒ Adapter
constructor
A new instance of Adapter.
- #reset ⇒ Object
- #save_translations! ⇒ Object
- #write(locale, name, value) ⇒ Object
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
#cache ⇒ Object (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 |
#record ⇒ Object (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 |
#stash ⇒ Object (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 |
#reset ⇒ Object
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 |