Class: Mobility::Backends::Sequel::KeyValue

Inherits:
Object
  • Object
show all
Includes:
KeyValue, Mobility::Backends::Sequel, Util
Defined in:
lib/mobility/backends/sequel/key_value.rb

Overview

Note:

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, DestroyKeyValueTranslations, Translation Classes: CacheRequired, QualifiedIdentifier, StringTranslation, TextTranslation

Constant Summary

Constants included from Util

Util::VALID_CONSTANT_NAME_REGEXP

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#blank?, #camelize, #constantize, #demodulize, #foreign_key, included, #presence, #present?, #singularize, #underscore

Methods included from KeyValue

#association_name, #class_name, #each_locale, #read, #write

Methods included from Mobility::Backends::Sequel

included

Class Method Details

.build_op(attr, locale) ⇒ Object


40
41
42
# File 'lib/mobility/backends/sequel/key_value.rb', line 40

def build_op(attr, locale)
  QualifiedIdentifier.new(table_alias(attr, locale), :value, locale, self, attr)
end

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • type (Symbol, String)

    Column type to use

  • association_name (Symbol) — default: :<type>_translations

    Name of association method, defaults to <type>_translations

  • class_name (Symbol)

    Translation class, defaults to Mobility::<ORM>::<type>Translation

Raises:

  • (CacheRequired)

    if cache is disabled

  • (ArgumentError)

    if type is not set, and both class_name and association_name are also not set


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mobility/backends/sequel/key_value.rb', line 27

def configure(options)
  raise CacheRequired, "Cache required for Sequel::KeyValue backend" if options[:cache] == false
  super
  if type = options[:type]
    options[:association_name] ||= :"#{options[:type]}_translations"
    options[:class_name]       ||= const_get("#{type.capitalize}Translation")
  end
  options[:table_alias_affix] = "#{model_class}_%s_#{options[:association_name]}"
rescue NameError
  raise ArgumentError, "You must define a Mobility::Sequel::#{type.capitalize}Translation class."
end

.prepare_dataset(dataset, predicate, locale) ⇒ Sequel::Dataset

Returns Prepared dataset.

Parameters:

  • dataset (Sequel::Dataset)

    Dataset to prepare

  • predicate (Object)

    Predicate

  • locale (Symbol)

    Locale

Returns:

  • (Sequel::Dataset)

    Prepared dataset


48
49
50
51
52
# File 'lib/mobility/backends/sequel/key_value.rb', line 48

def prepare_dataset(dataset, predicate, locale)
  visit(predicate, locale).inject(dataset) do |ds, (attr, join_type)|
    join_translations(ds, attr, locale, join_type)
  end
end

Instance Method Details

#save_translationsObject

Saves translation which have been built and which have non-blank values.


173
174
175
176
177
178
# File 'lib/mobility/backends/sequel/key_value.rb', line 173

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::Backends::Sequel::KeyValue::TextTranslation, Mobility::Backends::Sequel::KeyValue::StringTranslation

Returns translation for a given locale, or initializes one if none is present.


166
167
168
169
170
# File 'lib/mobility/backends/sequel/key_value.rb', line 166

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