Module: Mobility::Backend::ClassMethods
- Defined in:
- lib/mobility/backend.rb
Overview
Defines setup hooks for backend to customize model class.
Instance Method Summary collapse
-
#build_subclass(model_class, options) ⇒ Class
Build a subclass of this backend class for a given set of options.
- #inherited(subclass) ⇒ Object
-
#inspect ⇒ String
Show useful information about this backend class, if it has no name.
-
#option_reader(name) ⇒ Object
Create instance and class methods to access value on options hash.
-
#setup {|attribute_names, options| ... } ⇒ Object
Assign block to be called on model class.
-
#setup_model(model_class, attribute_names) ⇒ Object
Call setup block on a class with attributes and options.
-
#valid_keys ⇒ Array
Returns valid option keys for this backend.
Instance Method Details
#build_subclass(model_class, options) ⇒ Class
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
167 168 169 170 171 172 173 |
# File 'lib/mobility/backend.rb', line 167 def build_subclass(model_class, ) Class.new(self) do @model_class = model_class configure() if respond_to?(:configure) @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 |
#inspect ⇒ String
Show useful information about this backend class, if it has no name.
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
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
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.
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.
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, , &setup_block) end |
#valid_keys ⇒ Array
Returns valid option keys for this backend. This is overriden in backends to define which keys are valid for each backend class.
126 127 128 |
# File 'lib/mobility/backend.rb', line 126 def valid_keys [] end |