module I18n::Backend::Moped::Implementation

Attributes

collection[RW]

Public Class Methods

new(collection, options={}) click to toggle source
# File lib/i18n/backend/moped.rb, line 10
def initialize(collection, options={})
  @collection, @options = collection, options
end

Public Instance Methods

available_locales() click to toggle source
# File lib/i18n/backend/moped.rb, line 14
def available_locales
  collection.find.distinct(:locale).map {|l| l.to_sym}
end
store_translations(locale, data, options = {}) click to toggle source
# File lib/i18n/backend/moped.rb, line 18
def store_translations(locale, data, options = {})
  escape = options.fetch(:escape, true)
  flatten_translations(locale, data, escape, false).each do |key, value|
    collection.find(:key => key.to_s, :locale => locale.to_s).remove_all
    doc = {:key => key.to_s, :value => value, :locale => locale.to_s}
    collection.insert(doc)
  end
end

Protected Instance Methods

lookup(locale, key, scope = [], options = {}) click to toggle source
# File lib/i18n/backend/moped.rb, line 28
def lookup(locale, key, scope = [], options = {})
  key = normalize_flat_keys(locale, key, scope, options[:separator])
  result = collection.find(:locale => locale.to_s, :key => /^#{Regexp.quote(key.to_s)}(\.|$)/).entries

  if result.empty?
    nil
  elsif result.first["key"] == key.to_s
    result.first["value"]
  else
    chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1
    result = result.inject({}) do |hash, r|
      hash[r["key"].slice(chop_range)] = r["value"]
      hash
    end
    result.deep_symbolize_keys
  end
end