Class: Mongoid::Globalize::Attributes

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

Overview

TODO: Think about using HashWithIndifferentAccess ?

Instance Method Summary collapse

Instance Method Details

#[](locale) ⇒ Object

Returns translations for given locale. Creates empty hash for locale, if given locale doesn’t present. Param: String or Symbol - locale Result: Hash of translations



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

def [](locale)
  locale = locale.to_sym
  self[locale] = {} unless has_key?(locale)
  self.fetch(locale)
end

#contains?(locale, name) ⇒ Boolean

Checks that given locale has translation for given name. Param: String or Symbol - locale Param: String or Symbol - name of field Result: true or false

Returns:

  • (Boolean)


20
21
22
# File 'lib/mongoid_globalize/attributes.rb', line 20

def contains?(locale, name)
  self[locale].has_key?(name.to_s)
end

#read(locale, name) ⇒ Object

Returns translation for given name and given locale. Param: String or Symbol - locale Param: String or Symbol - name of field Result: Object



28
29
30
# File 'lib/mongoid_globalize/attributes.rb', line 28

def read(locale, name)
  self[locale][name.to_s]
end

#write(locale, name, value) ⇒ Object

Writes translation for given name and given locale. Param: String or Symbol - locale Param: String or Symbol - name of field Param: Object



36
37
38
39
# File 'lib/mongoid_globalize/attributes.rb', line 36

def write(locale, name, value)
  #raise 'z' if value.nil? # TODO
  self[locale][name.to_s] = value
end