Class: Mobility::Backend::Sequel::KeyValue
- Includes:
- Mobility::Backend
- Defined in:
- lib/mobility/backend/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
Classes: CacheRequired, QueryMethods
Instance Attribute Summary collapse
-
#association_name ⇒ Symbol
readonly
Name of the association.
-
#class_name ⇒ Class
readonly
Translation model class.
Attributes included from Mobility::Backend
Backend Accessors collapse
-
#read(locale, **options) ⇒ Object
Value of translation.
-
#write(locale, value, **options) ⇒ Object
Updated value.
Backend Configuration collapse
Cache Methods 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 Mobility::Backend
Constructor Details
#initialize(model, attribute, **options) ⇒ KeyValue
Returns a new instance of KeyValue.
26 27 28 29 30 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 26 def initialize(model, attribute, **) super @association_name = [:association_name] @class_name = [:class_name] end |
Instance Attribute Details
#association_name ⇒ Symbol (readonly)
Returns name of the association.
18 19 20 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 18 def association_name @association_name end |
#class_name ⇒ Class (readonly)
Returns translation model class.
21 22 23 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 21 def class_name @class_name end |
Class Method Details
.configure!(options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 50 def self.configure!() raise CacheRequired, "Cache required for Sequel::KeyValue backend" if [:cache] == false [:type] ||= :text case type = [:type].to_sym when :text, :string [:class_name] ||= Mobility::Sequel.const_get("#{type.capitalize}Translation") else raise ArgumentError, "type must be one of: [text, string]" end [:class_name] = [:class_name].constantize if [:class_name].is_a?(String) [:association_name] ||= [:class_name].table_name.to_sym %i[type association_name].each { |key| [key] = [key].to_sym } end |
Instance Method Details
#new_cache ⇒ KeyValue::TranslationsCache
109 110 111 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 109 def new_cache KeyValue::TranslationsCache.new(self) end |
#read(locale, **options) ⇒ Object
Returns Value of translation.
34 35 36 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 34 def read(locale, **) translation_for(locale).value end |
#save_translations ⇒ Object
Saves translation which have been built and which have non-blank values.
129 130 131 132 133 134 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 129 def save_translations cache.each_translation do |translation| next unless translation.value.present? translation.id ? translation.save : model.send("add_#{association_name.to_s.singularize}", 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.
122 123 124 125 126 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 122 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 |
#write(locale, value, **options) ⇒ Object
Returns Updated value.
39 40 41 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 39 def write(locale, value, **) translation_for(locale).tap { |t| t.value = value }.value end |
#write_to_cache? ⇒ Boolean
114 115 116 |
# File 'lib/mobility/backend/sequel/key_value.rb', line 114 def write_to_cache? true end |