Class: Mongoid::Globalize::Adapter

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

Overview

The Adapter class used for stashing translates and changes in its before they will be persisted or rejected.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Adapter

Initialises new instance of Adapter. Creates empty stash for storing translates. Param: translatable Class



11
12
13
14
# File 'lib/mongoid_globalize/adapter.rb', line 11

def initialize(record)
  self.record = record
  self.stash = Attributes.new
end

Instance Attribute Details

#recordObject

Returns the value of attribute record.



5
6
7
# File 'lib/mongoid_globalize/adapter.rb', line 5

def record
  @record
end

#stashObject

Returns the value of attribute stash.



5
6
7
# File 'lib/mongoid_globalize/adapter.rb', line 5

def stash
  @stash
end

#translationsObject

Returns the value of attribute translations.



5
6
7
# File 'lib/mongoid_globalize/adapter.rb', line 5

def translations
  @translations
end

Instance Method Details

#fetch(locale, name) ⇒ Object

Returns value of attribute for given locale or it’s fallbacks. Param: String or Symbol - name of locale Param: String or Symbol - name of attribute Returns nil if no value finded



30
31
32
33
34
35
36
# File 'lib/mongoid_globalize/adapter.rb', line 30

def fetch(locale, name)
  Mongoid::Globalize.fallbacks(locale).each do |fallback|
    value = fetch_stash(fallback, name) || fetch_attribute(fallback, name)
    return value unless fallbacks_for?(value)
  end
  return nil
end

#fetch_stash(locale, name) ⇒ Object

Returns value of attribute from stash for given locale. Param: String or Symbol - name of locale Param: String or Symbol - name of attribute Returns nil if no value finded



20
21
22
23
24
# File 'lib/mongoid_globalize/adapter.rb', line 20

def fetch_stash(locale, name)
  value = stash.read(locale, name)
  return value if value
  return nil
end

#prepare_translations!Object

Prepares data from stash for persisting in embeded Translation documents. Also clears stash for further operations.



48
49
50
51
52
53
54
55
56
57
# File 'lib/mongoid_globalize/adapter.rb', line 48

def prepare_translations!
  stash.each do |locale, attrs|
    if attrs.any?
      translation = record.translations.find_by_locale(locale)
      translation ||= record.translations.build(:locale => locale)
      attrs.each{ |name, value| translation[name] = value }
    end
  end
  reset
end

#resetObject

Clears stash.



60
61
62
# File 'lib/mongoid_globalize/adapter.rb', line 60

def reset
  stash.clear
end

#write(locale, name, value) ⇒ Object

Writes value of attribute for given locale into stash. Param: String or Symbol - name of locale Param: String or Symbol - name of attribute Param: Object - value of attribute



42
43
44
# File 'lib/mongoid_globalize/adapter.rb', line 42

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