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 Classes: CacheRequired, QueryMethods

Constant Summary

Constants included from Util

Util::VALID_CONSTANT_NAME_REGEXP

Instance Attribute Summary collapse

Attributes included from KeyValue

#association_name

Backend Configuration collapse

Instance Method Summary collapse

Methods included from Util

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

Methods included from KeyValue

#each_locale, #read, #write

Methods included from Mobility::Backend::OrmDelegator

#for

Methods included from Mobility::Backends::Sequel

included, #setup_query_methods

Constructor Details

#initialize(model, attribute, options = {}) ⇒ KeyValue

Returns a new instance of KeyValue.

Parameters:

  • model

    Model on which backend is defined

  • attribute (String)

    Backend attribute

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • association_name (Symbol)

    Name of association

  • class_name (Class)

    Translation model class



33
34
35
36
# File 'lib/mobility/backends/sequel/key_value.rb', line 33

def initialize(model, attribute, options = {})
  super
  @class_name       = options[:class_name]
end

Instance Attribute Details

#class_nameClass (readonly)

Returns translation model class.

Returns:

  • (Class)

    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

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • type (Symbol, String) — default: :text

    Column type to use

  • associaiton_name (Symbol) — default: :text_translations

    Name of association method

  • class_name (Symbol) — default: {Mobility::Sequel::TextTranslation}

    Translation class

Raises:

  • (CacheRequired)

    if cache is disabled

  • (ArgumentError)

    if type is not either :text or :string



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

def self.configure(options)
  super
  raise CacheRequired, "Cache required for Sequel::KeyValue backend" if options[:cache] == false
  type = options[:type]
  options[:class_name] ||= Mobility::Sequel.const_get("#{type.capitalize}Translation".freeze)
  options[:class_name] = options[:class_name].constantize if options[:class_name].is_a?(String)
  options[:association_name] ||= :"#{options[:type]}_translations"
  %i[type association_name].each { |key| options[key] = options[key].to_sym }
end

Instance Method Details

#save_translationsObject

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