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

#apply_plugin(_) ⇒ Boolean

Note:

This is currently only called by Plugins::Cache.

Called from plugins to apply custom processing for this backend. Name is the name of the plugin.


211
212
213
# File 'lib/mobility/backend.rb', line 211

def apply_plugin(_)
  false
end

#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.)


202
203
204
# File 'lib/mobility/backend.rb', line 202

def for(_)
  self
end

#inherited(subclass) ⇒ Object


152
153
154
155
# File 'lib/mobility/backend.rb', line 152

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

#inspectString

Show useful information about this backend class, if it has no name.


217
218
219
# File 'lib/mobility/backend.rb', line 217

def inspect
  name ? super : "#<#{superclass.name}>"
end

#option_reader(name) ⇒ Object

Create instance and class methods to access value on options hash


182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mobility/backend.rb', line 182

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

#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:


140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mobility/backend.rb', line 140

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, attribute_names) ⇒ Object

Call setup block on a class with attributes and options.


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

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

#with_options(options = {}, &block) ⇒ 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


171
172
173
174
175
176
177
178
# File 'lib/mobility/backend.rb', line 171

def with_options(options = {}, &block)
  configure(options) if respond_to?(:configure)
  options.freeze
  Class.new(self) do
    @options = options
    class_eval(&block) if block_given?
  end
end