Class: FastGettext::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_gettext/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



5
6
7
8
# File 'lib/fast_gettext/cache.rb', line 5

def initialize
  @store = {}
  reload!
end

Instance Method Details

#[]=(key, value) ⇒ Object

TODO: only used for tests, maybe if-else around it …



20
21
22
# File 'lib/fast_gettext/cache.rb', line 20

def []=(key, value)
  @current[key] = value
end

#delete(key) ⇒ Object



35
36
37
# File 'lib/fast_gettext/cache.rb', line 35

def delete(key)
  @current.delete(key)
end

#fetch(key) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/fast_gettext/cache.rb', line 10

def fetch(key)
  translation = @current[key]
  if translation.nil? # uncached
    @current[key] = yield || false # TODO: get rid of this false hack and cache :missing
  else
    translation
  end
end

#reload!Object



39
40
41
42
# File 'lib/fast_gettext/cache.rb', line 39

def reload!
  @current = {}
  @current[""] = false
end

#switch_to(text_domain, locale) ⇒ Object

key performance gain:

  • no need to lookup locale on each translation

  • no need to lookup text_domain on each translation

  • super-simple hash lookup



28
29
30
31
32
33
# File 'lib/fast_gettext/cache.rb', line 28

def switch_to(text_domain, locale)
  @store[text_domain] ||= {}
  @store[text_domain][locale] ||= {}
  @store[text_domain][locale][""] = false # ignore gettext meta key when translating
  @current = @store[text_domain][locale]
end