Class: Mobility::Backends::Sequel::KeyValue
- Inherits:
-
Object
- Object
- Mobility::Backends::Sequel::KeyValue
- Includes:
- KeyValue, Mobility::Backends::Sequel, Util
- Defined in:
- lib/mobility/backends/sequel/key_value.rb
Overview
This backend requires the cache to be enabled in order to track and store changed translations, since Sequel does not support +build+-type methods on associations like ActiveRecord.
Implements the KeyValue backend for Sequel models.
Defined Under Namespace
Modules: Cache Classes: CacheRequired, QueryMethods
Constant Summary
Constants included from Util
Util::VALID_CONSTANT_NAME_REGEXP
Instance Attribute Summary collapse
-
#class_name ⇒ Class
readonly
Translation model class.
Attributes included from KeyValue
Backend Configuration collapse
Instance Method Summary collapse
-
#initialize(model, attribute, options = {}) ⇒ KeyValue
constructor
A new instance of KeyValue.
-
#save_translations ⇒ Object
Saves translation which have been built and which have non-blank values.
-
#translation_for(locale, _) ⇒ Mobility::Sequel::TextTranslation, Mobility::Sequel::StringTranslation
Returns translation for a given locale, or initializes one if none is present.
Methods included from Util
#blank?, #camelize, #constantize, #demodulize, #foreign_key, included, #presence, #present?, #singularize, #underscore
Methods included from KeyValue
Methods included from Mobility::Backend::OrmDelegator
Methods included from Mobility::Backends::Sequel
included, #setup_query_methods
Constructor Details
#initialize(model, attribute, options = {}) ⇒ KeyValue
Returns a new instance of KeyValue.
33 34 35 36 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 33 def initialize(model, attribute, = {}) super @class_name = [:class_name] end |
Instance Attribute Details
#class_name ⇒ Class (readonly)
Returns translation model class.
28 29 30 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 28 def class_name @class_name end |
Class Method Details
.configure(options) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 44 def self.configure() super raise CacheRequired, "Cache required for Sequel::KeyValue backend" if [:cache] == false type = [:type] [:class_name] ||= Mobility::Sequel.const_get("#{type.capitalize}Translation".freeze) [:class_name] = [:class_name].constantize if [:class_name].is_a?(String) [:association_name] ||= :"#{[:type]}_translations" %i[type association_name].each { |key| [key] = [key].to_sym } end |
Instance Method Details
#save_translations ⇒ Object
Saves translation which have been built and which have non-blank values.
116 117 118 119 120 121 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 116 def save_translations cache.each_value do |translation| next unless present?(translation.value) translation.id ? translation.save : model.send("add_#{singularize(association_name)}", translation) end end |
#translation_for(locale, _) ⇒ Mobility::Sequel::TextTranslation, Mobility::Sequel::StringTranslation
Returns translation for a given locale, or initializes one if none is present.
109 110 111 112 113 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 109 def translation_for(locale, _) translation = model.send(association_name).find { |t| t.key == attribute && t.locale == locale.to_s } translation ||= class_name.new(locale: locale, key: attribute) translation end |