Module: Mobility::Backends::Sequel::ClassMethods

Defined in:
lib/mobility/backends/sequel.rb

Instance Method Summary collapse

Instance Method Details

#[](name, locale) ⇒ Object

Parameters:

  • name (Symbol)

    Attribute name

  • locale (Symbol)

    Locale



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

def [](name, locale)
  build_op(name.to_s, locale)
end

#build_op(_attr, _locale) ⇒ Object

Returns Op for this translated attribute.

Parameters:

  • _attr (String)

    Attribute name

  • _locale (Symbol)

    Locale

Returns:

  • Op for this translated attribute

Raises:



22
23
24
# File 'lib/mobility/backends/sequel.rb', line 22

def build_op(_attr, _locale)
  raise NotImplementedError
end

#define_column_changes(mod, attributes, column_affix: "%s") ⇒ Object

Forces Sequel to notice changes when Mobility setter method is called. TODO: Find a better way to do this.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mobility/backends/sequel.rb', line 37

def define_column_changes(mod, attributes, column_affix: "%s")
  mod.class_eval do
    attributes.each do |attribute|
      define_method "#{attribute}=" do |value, **options|
        if !options[:super] && send(attribute) != value
          locale = options[:locale] || Mobility.locale
          column = (column_affix % attribute).to_sym
          attribute_with_locale = :"#{attribute}_#{Mobility.normalize_locale(locale)}"
          @changed_columns = changed_columns | [column, attribute.to_sym, attribute_with_locale]
        end
        super(value, **options)
      end
    end
  end
end

#define_hash_initializer(mod, columns) ⇒ Object

Initialize column value(s) by default to a hash. TODO: Find a better way to do this.



55
56
57
58
59
60
61
62
63
64
# File 'lib/mobility/backends/sequel.rb', line 55

def define_hash_initializer(mod, columns)
  mod.class_eval do
    class_eval <<-EOM, __FILE__, __LINE__ + 1
      def initialize_set(values)
        #{columns.map { |c| "self[:#{c}] = {}" }.join(';')}
        super
      end
    EOM
  end
end

#prepare_dataset(dataset, _predicate, _locale) ⇒ Sequel::Dataset

Returns Prepared dataset.

Parameters:

  • dataset (Sequel::Dataset)

    Dataset to prepare

  • predicate (Object)

    Predicate

  • locale (Symbol)

    Locale

Returns:

  • (Sequel::Dataset)

    Prepared dataset



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

def prepare_dataset(dataset, _predicate, _locale)
  dataset
end