lib/mobility/backends/key_value.rb in mobility-0.8.13 vs lib/mobility/backends/key_value.rb in mobility-1.0.0.alpha
- old
+ new
@@ -42,12 +42,10 @@
@see Mobility::Backends::ActiveRecord::KeyValue
@see Mobility::Backends::Sequel::KeyValue
=end
module KeyValue
- extend Backend::OrmDelegator
-
# @!method association_name
# Returns the name of the polymorphic association.
# @return [Symbol] Name of the association
# @!method class_name
@@ -85,35 +83,28 @@
end
module ClassMethods
# @!group Backend Configuration
# @option options [Symbol,String] type Column type to use
- # @option options [Symbol] associaiton_name (:<type>_translations) Name
+ # @option options [Symbol] association_name (:<type>_translations) Name
# of association method, defaults to +<type>_translations+
# @option options [Symbol] class_name Translation class, defaults to
# +Mobility::<ORM>::<type>Translation+
# @raise [ArgumentError] if +type+ is not set, and both +class_name+
# and +association_name+ are also not set
def configure(options)
options[:type] &&= options[:type].to_sym
options[:association_name] &&= options[:association_name].to_sym
options[:class_name] &&= Util.constantize(options[:class_name])
if !(options[:type] || (options[:class_name] && options[:association_name]))
- # TODO: Remove warning and raise ArgumentError in v1.0
- warn %{
-WARNING: In previous versions, the Mobility KeyValue backend defaulted to a
-text type column, but this behavior is now deprecated and will be removed in
-the next release. Either explicitly specify the type by passing type: :text in
-each translated model, or set a default option in your configuration.
- }
- options[:type] = :text
+ raise ArgumentError, "KeyValue backend requires an explicit type option, either text or string."
end
end
# Apply custom processing for plugin
- # @param (see Backend::Setup#apply_plugin)
- # @return (see Backend::Setup#apply_plugin)
+ # @param (see Backend::ClassMethods#apply_plugin)
+ # @return (see Backend::ClassMethods#apply_plugin)
def apply_plugin(name)
if name == :cache
include self::Cache
true
else
@@ -125,10 +116,27 @@
table_alias_affix % "#{attr}_#{Mobility.normalize_locale(locale)}"
end
end
module Cache
- include Plugins::Cache::TranslationCacher.new(:translation_for)
+ def translation_for(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
+
+ def clear_cache
+ @cache = {}
+ end
+
+ private
+
+ def cache
+ @cache ||= {}
+ end
end
end
end
end