Module: Mobility::Backend::ClassMethods

Defined in:
lib/mobility/backend.rb

Overview

Defines setup hooks for backend to customize model class.

Instance Method Summary collapse

Instance Method Details

#build_subclass(model_class, options) ⇒ Class

Note:

This method also freezes the options hash to prevent it from being changed.

Build a subclass of this backend class for a given set of options

Parameters:

  • model_class (Class)

    Class

  • options (Hash)

Returns:

  • (Class)

    backend subclass



172
173
174
# File 'lib/mobility/backend.rb', line 172

def build_subclass(model_class, options)
  ConfiguredBackend.build(self, model_class, options)
end

#inherited(subclass) ⇒ Object



162
163
164
# File 'lib/mobility/backend.rb', line 162

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

#model_classObject



194
195
196
# File 'lib/mobility/backend.rb', line 194

def model_class
  raise_unconfigured!(:model_class)
end

#option_reader(name) ⇒ Object

Create instance and class methods to access value on options hash

Parameters:

  • name (Symbol)

    Name of option reader



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mobility/backend.rb', line 178

def option_reader(name)
  module_eval <<-EOM, __FILE__, __LINE__ + 1
  def self.#{name}
    options[:#{name}]
  end

  def #{name}
    self.class.options[:#{name}]
  end
  EOM
end

#optionsObject



190
191
192
# File 'lib/mobility/backend.rb', line 190

def options
  raise_unconfigured!(:options)
end

#setup {|attribute_names, 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:



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mobility/backend.rb', line 148

def setup &block
  if @setup_block
    setup_block = @setup_block
    exec_setup_block = method(:exec_setup_block)
    @setup_block = lambda do |attributes, options, backend_class|
      [setup_block, block].each do |blk|
        exec_setup_block.call(self, attributes, options, backend_class, &blk)
      end
    end
  else
    @setup_block = block
  end
end

#setup_model(_model_class, _attributes) ⇒ Object



198
199
200
# File 'lib/mobility/backend.rb', line 198

def setup_model(_model_class, _attributes)
  raise_unconfigured!(:setup_model)
end

#valid_keysArray

Returns valid option keys for this backend. This is overriden in backends to define which keys are valid for each backend class.

Returns:

  • (Array)


140
141
142
# File 'lib/mobility/backend.rb', line 140

def valid_keys
  []
end