Sha256: 54f087e9c67bb8c96af11ee99d8e0431e62d2d0ea2080b62eb9ce50c1daacd0c

Contents?: true

Size: 1011 Bytes

Versions: 16

Compression:

Stored size: 1011 Bytes

Contents

module FastGettext
  class Cache
    def initialize
      @store = {}
      reload!
    end

    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

    # TODO only used for tests, maybe if-else around it ...
    def []=(key, value)
      @current[key] = value
    end

    # key performance gain:
    # - no need to lookup locale on each translation
    # - no need to lookup text_domain on each translation
    # - super-simple hash lookup
    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

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

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
fast_gettext-1.8.0 lib/fast_gettext/cache.rb
fast_gettext-1.7.0 lib/fast_gettext/cache.rb
fast_gettext-1.1.2 lib/fast_gettext/cache.rb
fast_gettext-1.1.1 lib/fast_gettext/cache.rb
fast_gettext-1.6.0 lib/fast_gettext/cache.rb
fast_gettext-1.5.1 lib/fast_gettext/cache.rb
fast_gettext-1.5.0 lib/fast_gettext/cache.rb
fast_gettext-1.4.1 lib/fast_gettext/cache.rb
fast_gettext-1.4.0 lib/fast_gettext/cache.rb
fast_gettext-1.3.0 lib/fast_gettext/cache.rb
fast_gettext-1.2.0 lib/fast_gettext/cache.rb
fast_gettext-1.1.0 lib/fast_gettext/cache.rb
fast_gettext-1.0.0 lib/fast_gettext/cache.rb
fast_gettext-0.9.2 lib/fast_gettext/cache.rb
fast_gettext-0.9.1 lib/fast_gettext/cache.rb
fast_gettext-0.9.0 lib/fast_gettext/cache.rb