Class: I18n::Backend::Mongodb

Inherits:
Object
  • Object
show all
Includes:
Base, Flatten
Defined in:
lib/i18n/backend/mongodb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, subtrees = true) ⇒ Mongodb

Returns a new instance of Mongodb.



10
11
12
13
# File 'lib/i18n/backend/mongodb.rb', line 10

def initialize(collection, subtrees = true)
  @collection, @subtrees = collection, subtrees
  collection.indexes.create({key: 1, locale: 1}, {unique: true})
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



8
9
10
# File 'lib/i18n/backend/mongodb.rb', line 8

def collection
  @collection
end

Instance Method Details

#available_localesObject



22
23
24
# File 'lib/i18n/backend/mongodb.rb', line 22

def available_locales
  collection.find.distinct(:locale).map(&:to_sym)
end

#store_translations(locale, data, options = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/i18n/backend/mongodb.rb', line 15

def store_translations(locale, data, options={})
  escape = options.fetch(:escape, true)
  flatten_translations(locale, data, escape, @subtrees).each do |key, value|
    collection.find(key: key.to_s, locale: locale.to_s).upsert('$set' => { value: value })
  end
end