Class: Mobility::Plugins::Cache::TranslationCacher

Inherits:
Module
  • Object
show all
Defined in:
lib/mobility/plugins/cache/translation_cacher.rb

Overview

Creates a module to cache a given translation fetch method. The cacher defines private methods cache and clear_cache to access and clear, respectively, a translations hash.

This cacher is used to cache translation values in Mobility::Plugins::Cache, and also to cache translation records in Backends::Table and Backends::KeyValue.

Defined Under Namespace

Modules: CacheMethods

Instance Method Summary collapse

Constructor Details

#initialize(fetch_method) ⇒ TranslationCacher

Returns a new instance of TranslationCacher.

Parameters:

  • fetch_method (Symbol)

    Name of translation fetch method to cache


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mobility/plugins/cache/translation_cacher.rb', line 17

def initialize(fetch_method)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{fetch_method} locale, **options
      return super(locale, options) if options.delete(:cache) == false
      if cache.has_key?(locale)
        cache[locale]
      else
        cache[locale] = super(locale, options)
      end
    end
  EOM

  include CacheMethods
end