lib/mobility/plugins/cache/translation_cacher.rb in mobility-0.5.1 vs lib/mobility/plugins/cache/translation_cacher.rb in mobility-0.6.0
- old
+ new
@@ -13,27 +13,27 @@
=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)
+ 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
- end
+ EOM
- define_method :cache do
- @cache ||= {}
- end
+ include CacheMethods
+ end
- define_method :clear_cache do
- @cache = {}
- end
-
- private :cache, :clear_cache
+ module CacheMethods
+ private
+ def cache; @cache ||= {}; end
+ def clear_cache; @cache = {}; end
end
end
end
end
end