Class: Mobility::Backend::Sequel::KeyValue

Inherits:
Object
  • Object
show all
Includes:
Mobility::Backend
Defined in:
lib/mobility/backend/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

Classes: CacheRequired, QueryMethods

Instance Attribute Summary collapse

Attributes included from Mobility::Backend

#attribute, #model, #options

Backend Accessors collapse

Backend Configuration collapse

Cache Methods collapse

Instance Method Summary collapse

Methods included from Mobility::Backend

included, method_name

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)

    a customizable set of options

Options Hash (**options):

  • fallbacks (Hash)

    Fallbacks hash

  • association_name (Symbol)

    Name of association

  • class_name (Class)

    Translation model class



26
27
28
29
30
# File 'lib/mobility/backend/sequel/key_value.rb', line 26

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

Instance Attribute Details

#association_nameSymbol (readonly)

Returns name of the association.

Returns:

  • (Symbol)

    name of the association



18
19
20
# File 'lib/mobility/backend/sequel/key_value.rb', line 18

def association_name
  @association_name
end

#class_nameClass (readonly)

Returns translation model class.

Returns:

  • (Class)

    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

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • type (Symbol) — default: :text

    Column type to use

  • associaiton_name (Symbol) — default: :mobility_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



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!(options)
  raise CacheRequired, "Cache required for Sequel::KeyValue backend" if options[:cache] == false
  options[:type]             ||= :text
  case type = options[:type].to_sym
  when :text, :string
    options[:class_name]     ||= Mobility::Sequel.const_get("#{type.capitalize}Translation")
  else
    raise ArgumentError, "type must be one of: [text, string]"
  end
  options[:class_name] = options[:class_name].constantize if options[:class_name].is_a?(String)
  options[:association_name] ||= options[:class_name].table_name.to_sym
  %i[type association_name].each { |key| options[key] = options[key].to_sym }
end

Instance Method Details

#new_cacheKeyValue::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.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



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

def read(locale, **options)
  translation_for(locale).value
end

#save_translationsObject

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.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (Object)

    Value to write

  • options (Hash)

Returns:



39
40
41
# File 'lib/mobility/backend/sequel/key_value.rb', line 39

def write(locale, value, **options)
  translation_for(locale).tap { |t| t.value = value }.value
end

#write_to_cache?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/mobility/backend/sequel/key_value.rb', line 114

def write_to_cache?
  true
end