Class: Mobility::Backends::Sequel::Container

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

Overview

Implements the Container backend for Sequel models.

Defined Under Namespace

Classes: InvalidColumnType, JSONBOp, JSONOp

Backend Accessors collapse

Backend Configuration collapse

Instance Method Summary collapse

Methods included from Container

included

Methods included from Mobility::Backends::Sequel

included

Class Method Details

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • column_name (Symbol) — default: :translations

    Name of column on which to store translations



42
43
44
45
46
47
48
49
50
# File 'lib/mobility/backends/sequel/container.rb', line 42

def self.configure(options)
  options[:column_name] ||= :translations
  options[:column_name] = options[:column_name].to_sym
  column_name, db_schema = options[:column_name], model_class.db_schema
  options[:column_type] = db_schema[column_name] && (db_schema[column_name][:db_type]).to_sym
  unless %i[json jsonb].include?(options[:column_type])
    raise InvalidColumnType, "#{options[:column_name]} must be a column of type json or jsonb"
  end
end

Instance Method Details

#each_locale {|Locale| ... } ⇒ Object

Yields locales available for this attribute.

Yield Parameters:

  • Locale (Symbol)


54
55
56
57
58
# File 'lib/mobility/backends/sequel/container.rb', line 54

def each_locale
  model[column_name].each do |l, _|
    yield l.to_sym unless read(l).nil?
  end
end

#read(locale, _ = nil) ⇒ String, ...

Note:

Translation may be a string, integer, boolean, hash or array since value is stored on a JSON hash.

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (String, Integer, Boolean)

    Value of translation



24
25
26
# File 'lib/mobility/backends/sequel/container.rb', line 24

def read(locale, _ = nil)
  model_translations(locale)[attribute]
end

#write(locale, value, _ = nil) ⇒ String, ...

Note:

Translation may be a string, integer, boolean, hash or array since value is stored on a JSON hash.

Returns Updated value.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (String, Integer, Boolean)

    Value to write

  • options (Hash)

Returns:

  • (String, Integer, Boolean)

    Updated value



34
35
36
37
# File 'lib/mobility/backends/sequel/container.rb', line 34

def write(locale, value, _ = nil)
  set_attribute_translation(locale, value)
  model_translations(locale)[attribute]
end