Class: CouchI18n::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_i18n/store.rb

Instance Method Summary collapse

Instance Method Details

#[](key, options = {}) ⇒ Object

alias for read



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/couch_i18n/store.rb', line 18

def [](key, options = {})
  key = key.to_s.gsub('/', '.')
  Rails.cache.fetch("couch_i18n-#{key}") do
    old_database_name = get_couchrest_name
    begin
      set_couchrest_name CouchPotato::Config.database_name # Set database to original configured name
      translation = CouchI18n::Translation.find_by_key(key.to_s)
      translation ||= CouchI18n::Translation.create(:key => key, :value => options[:default].presence || key.to_s.split('.').last, :translated => false)
    ensure
      set_couchrest_name old_database_name
    end
    translation.value
  end
end

#[]=(key, value, options = {}) ⇒ Object

Now the store features



9
10
11
12
13
14
15
# File 'lib/couch_i18n/store.rb', line 9

def []=(key, value, options = {})
  key = key.to_s.gsub('/', '.')
  existing = CouchI18n::Translation.find_by_key(key) rescue nil
  translation = existing || CouchI18n::Translation.new(:key => key)
  translation.value = value
  translation.save
end

#available_localesObject



4
5
6
# File 'lib/couch_i18n/store.rb', line 4

def available_locales
  CouchI18n::Translation.get_keys_by_level(0)
end

#get_couchrest_nameObject



41
42
43
# File 'lib/couch_i18n/store.rb', line 41

def get_couchrest_name
  CouchPotato.database.couchrest_database.name
end

#keysObject



45
46
47
# File 'lib/couch_i18n/store.rb', line 45

def keys
  CouchI18n::Translation.all.map(&:key)
end

#set_couchrest_name(name) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/couch_i18n/store.rb', line 33

def set_couchrest_name(name)
  d = CouchPotato.database.couchrest_database
  d.instance_variable_set('@name', name)
  d.instance_variable_set('@uri', "/#{name.gsub('/', '%2F')}")
  d.instance_variable_set('@bulk_save_cache', [])
  d.instance_variable_set('@root', d.host + d.uri)
end