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

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
# File 'lib/volt/models/model_helpers.rb', line 25

def class_at_path(path)
  if path && path.last == :[]
    begin
      # TODO: SECURITY on the back-end we need to check that the model class we're loading
      # is coming from the models folder.

      # remove the _ and then singularize
      klass_name = path[-2][1..-1].singularize.camelize

      klass_name = klass_name.camelize
      klass = Object.send(:const_get, klass_name.to_sym)
    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



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 = value.to_h
  elsif value.is_a?(ArrayModel)
    value = value.to_a
  end

  return value
end

#event_added(event, scope_provider, 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, scope_provider, first, first_for_event)
  @persistor.event_added(event, scope_provider, 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