Class: Mobility::Backend::Sequel::Table

Inherits:
Object
  • Object
show all
Includes:
Mobility::Backend
Defined in:
lib/mobility/backend/sequel/table.rb

Overview

Implements the Table 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) ⇒ Table

Returns a new instance of Table.

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



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

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

Instance Attribute Details

#association_nameSymbol (readonly)

Returns name of the association method.

Returns:

  • (Symbol)

    name of the association method



14
15
16
# File 'lib/mobility/backend/sequel/table.rb', line 14

def association_name
  @association_name
end

Class Method Details

.configure!(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • association_name (Symbol) — default: :mobility_model_translations

    Name of association method

  • table_name (Symbol)

    Name of translation table

  • foreign_key (Symbol)

    Name of foreign key

  • subclass_name (Symbol)

    Name of subclass to append to model class to generate translation class

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mobility/backend/sequel/table.rb', line 40

def self.configure!(options)
  raise CacheRequired, "Cache required for Sequel::Table backend" if options[:cache] == false
  table_name = options[:model_class].table_name
  options[:table_name]  ||= :"#{table_name.to_s.singularize}_translations"
  options[:foreign_key] ||= table_name.to_s.downcase.singularize.camelize.foreign_key
  if (association_name = options[:association_name]).present?
    options[:subclass_name] ||= association_name.to_s.singularize.camelize
  else
    options[:association_name] = :mobility_model_translations
    options[:subclass_name] ||= :Translation
  end
  %i[table_name foreign_key association_name subclass_name].each { |key| options[key] = options[key].to_sym }
end

Instance Method Details

#clear_cacheObject



116
117
118
# File 'lib/mobility/backend/sequel/table.rb', line 116

def clear_cache
  model_cache.clear if model_cache
end

#new_cacheTable::TranslationsCache



106
107
108
109
# File 'lib/mobility/backend/sequel/table.rb', line 106

def new_cache
  reset_model_cache unless model_cache
  model_cache.for(attribute)
end

#read(locale, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



25
26
27
# File 'lib/mobility/backend/sequel/table.rb', line 25

def read(locale, **options)
  translation_for(locale).send(attribute)
end

#write(locale, value, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



30
31
32
# File 'lib/mobility/backend/sequel/table.rb', line 30

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

#write_to_cache?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/mobility/backend/sequel/table.rb', line 112

def write_to_cache?
  true
end