Module: ModelHelpers
- Included in:
- ArrayModel, Model
- Defined in:
- lib/volt/models/model_helpers.rb
Overview
A place for things shared between an ArrayModel and a Model
Instance Method Summary collapse
-
#class_at_path(path) ⇒ Object
Gets the class for a model at the specified path.
- #deep_unwrap(value) ⇒ Object
-
#event_added(event, first, first_for_event) ⇒ Object
Pass to the persisotr.
-
#event_removed(event, last, last_for_event) ⇒ Object
Pass to the persistor.
Instance Method Details
#class_at_path(path) ⇒ Object
Gets the class for a model at the specified path.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/volt/models/model_helpers.rb', line 24 def class_at_path(path) if path begin # remove the _ and then singularize if path.last == :[] klass_name = path[-2][1..-1].singularize.camelize else klass_name = path[-1][1..-1].singularize.camelize end klass = $page.model_classes[klass_name] || Model rescue NameError => e # Ignore exception, just means the model isn't defined klass = Model end else klass = Model end return klass end |
#deep_unwrap(value) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/volt/models/model_helpers.rb', line 3 def deep_unwrap(value) if value.is_a?(Model) value = value.to_h elsif value.is_a?(ArrayModel) value = value.to_a end return value end |
#event_added(event, first, first_for_event) ⇒ Object
Pass to the persisotr
14 15 16 |
# File 'lib/volt/models/model_helpers.rb', line 14 def event_added(event, first, first_for_event) @persistor.event_added(event, first, first_for_event) if @persistor end |
#event_removed(event, last, last_for_event) ⇒ Object
Pass to the persistor
19 20 21 |
# File 'lib/volt/models/model_helpers.rb', line 19 def event_removed(event, last, last_for_event) @persistor.event_removed(event, last, last_for_event) if @persistor end |