Sha256: df6ed3406c15235acff31d25d695c289ada370a480d8b3c99f52420590da624a

Contents?: true

Size: 1.1 KB

Versions: 17

Compression:

Stored size: 1.1 KB

Contents

module Mobility
  module Plugins
    module Cache
=begin

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 {Mobility::Backends::Table} and
{Mobility::Backends::KeyValue}.

=end
      class TranslationCacher < Module
        # @param [Symbol] fetch_method Name of translation fetch method to cache
        def initialize(fetch_method)
          define_method fetch_method do |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

          define_method :cache do
            @cache ||= {}
          end

          define_method :clear_cache do
            @cache = {}
          end

          private :cache, :clear_cache
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
mobility-0.5.1 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.5.0 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.4.3 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.4.2 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.4.1 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.4.0 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.3.6 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.3.5 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.3.4 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.3.3 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.3.2 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.3.1 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.3.0 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.2.3 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.2.2 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.2.1 lib/mobility/plugins/cache/translation_cacher.rb
mobility-0.2.0 lib/mobility/plugins/cache/translation_cacher.rb