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


167
168
169
170
171
172
173
# File 'lib/mobility/backend.rb', line 167

def build_subclass(model_class, options)
  Class.new(self) do
    @model_class = model_class
    configure(options) if respond_to?(:configure)
    @options = options.freeze
  end
end

#inherited(subclass) ⇒ Object


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

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

#inspectString

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

Returns:

  • (String)

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

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

#option_reader(name) ⇒ Object

Create instance and class methods to access value on options hash

Parameters:

  • name (Symbol)

    Name of option reader


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

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:


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mobility/backend.rb', line 134

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.

Parameters:

  • model_class

    Class to be setup-ed

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

156
157
158
159
# File 'lib/mobility/backend.rb', line 156

def setup_model(model_class, attribute_names)
  return unless setup_block = @setup_block
  model_class.class_exec(attribute_names, options, &setup_block)
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)

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

def valid_keys
  []
end