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
- #model_class ⇒ Object
-
#option_reader(name) ⇒ Object
Create instance and class methods to access value on options hash.
- #options ⇒ Object
-
#setup {|attribute_names, options| ... } ⇒ Object
Assign block to be called on model class.
- #setup_model(_model_class, _attributes) ⇒ Object
-
#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
172 173 174 |
# File 'lib/mobility/backend.rb', line 172 def build_subclass(model_class, ) ConfiguredBackend.build(self, model_class, ) 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_class ⇒ Object
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
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 |
#options ⇒ Object
190 191 192 |
# File 'lib/mobility/backend.rb', line 190 def raise_unconfigured!(:options) 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.
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, , backend_class| [setup_block, block].each do |blk| exec_setup_block.call(self, attributes, , 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_keys ⇒ Array
Returns valid option keys for this backend. This is overriden in backends to define which keys are valid for each backend class.
140 141 142 |
# File 'lib/mobility/backend.rb', line 140 def valid_keys [] end |