Module: Mobility::Backend::Setup

Defined in:
lib/mobility/backend.rb

Overview

Defines setup hooks for backend to customize model class.

Instance Method Summary collapse

Instance Method Details

#for(_) ⇒ self

Attributes uses this method to get a backend class specific to the model using the backend. Backend classes can override this method to return a class specific to the model class using the backend (e.g. either an ActiveRecord or Sequel backend class depending on whether the model is an ActiveRecord model or a Sequel model.)

Returns:

  • (self)

    returns itself

See Also:


147
148
149
# File 'lib/mobility/backend.rb', line 147

def for(_)
  self
end

#inherited(subclass) ⇒ Object


126
127
128
# File 'lib/mobility/backend.rb', line 126

def inherited(subclass)
  subclass.instance_variable_set(:@setup_block, @setup_block)
end

#setup {|attributes, options| ... } ⇒ Object

Note:

When called multiple times, setup blocks will be appended so that they are run together consecutively on class.

Assign block to be called on model class.

Yields:


114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mobility/backend.rb', line 114

def setup &block
  if @setup_block
    setup_block = @setup_block
    @setup_block = lambda do |*args|
      class_exec(*args, &setup_block)
      class_exec(*args, &block)
    end
  else
    @setup_block = block
  end
end

#setup_model(model_class, attributes, **options) ⇒ Object

Call setup block on a class with attributes and options.

Parameters:

  • model_class

    Class to be setup-ed

  • attributes (Array<String>)
  • options (Hash)

134
135
136
137
# File 'lib/mobility/backend.rb', line 134

def setup_model(model_class, attributes, **options)
  return unless setup_block = @setup_block
  model_class.class_exec(attributes, options, &setup_block)
end