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

Inherits:
Object
  • Object
show all
Includes:
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 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


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

def self.configure(options)
  options[:column_name] ||= :translations
  options[:column_name] = options[:column_name].to_sym
  column_name, db_schema = options[:column_name], options[: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

#column_nameSymbol

Returns (:translations) Name of translations column.

Returns:

  • (Symbol)

    (:translations) Name of translations column


17
# File 'lib/mobility/backends/sequel/container.rb', line 17

option_reader :column_name

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

Yields locales available for this attribute.

Yield Parameters:

  • Locale (Symbol)

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

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


26
27
28
# File 'lib/mobility/backends/sequel/container.rb', line 26

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


36
37
38
39
# File 'lib/mobility/backends/sequel/container.rb', line 36

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