Module: Volt::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.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/volt/models/model_helpers.rb', line 25 def class_at_path(path) if path begin # remove the _ and then singularize if path.last == :[] index = -2 else index = -1 end klass_name = path[index].singularize.camelize 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 klass end |
#deep_unwrap(value) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/volt/models/model_helpers.rb', line 4 def deep_unwrap(value) if value.is_a?(Model) value.to_h elsif value.is_a?(ArrayModel) value.to_a else value end end |
#event_added(event, first, first_for_event) ⇒ Object
Pass to the persisotr
15 16 17 |
# File 'lib/volt/models/model_helpers.rb', line 15 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
20 21 22 |
# File 'lib/volt/models/model_helpers.rb', line 20 def event_removed(event, last, last_for_event) @persistor.event_removed(event, last, last_for_event) if @persistor end |