module Volt::Models::Helpers::Base

A place for things shared between an ArrayModel and a Model

Constants

ID_CHARS

Public Class Methods

included(base) click to toggle source
# File lib/volt/models/helpers/base.rb, line 122
def self.included(base)
  base.send :extend, ClassMethods
end

Public Instance Methods

deep_unwrap(value) click to toggle source
# File lib/volt/models/helpers/base.rb, line 6
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) click to toggle source

Pass to the persisotr

# File lib/volt/models/helpers/base.rb, line 17
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) click to toggle source

Pass to the persistor

# File lib/volt/models/helpers/base.rb, line 22
def event_removed(event, last, last_for_event)
  @persistor.event_removed(event, last, last_for_event) if @persistor
end
generate_id() click to toggle source

Create a random unique id that can be used as the mongo id as well

# File lib/volt/models/helpers/base.rb, line 29
def generate_id
  id = []
  24.times { id << ID_CHARS.sample }

  id.join
end
root() click to toggle source

returns the root model for the collection the model is currently on. So if the model is persisted somewhere on store, it will return “`store“`

# File lib/volt/models/helpers/base.rb, line 69
def root
  persistor.try(:root_model)
end
self_attributes() click to toggle source

Return the attributes that are only for this model and any hash sub models but not any sub-associations.

# File lib/volt/models/helpers/base.rb, line 39
def self_attributes
  # Don't store any sub-models, those will do their own saving.
  attributes.reject { |k, v| v.is_a?(ArrayModel) }.map do |k,v|
    if v.is_a?(Model)
      v = v.self_attributes
    end

    [k,v]
  end.to_h
end
setup_persistor(persistor) click to toggle source

Takes the persistor if there is one and

# File lib/volt/models/helpers/base.rb, line 52
def setup_persistor(persistor)
  # Use page as the default persistor
  persistor ||= Persistors::Page
  if persistor.respond_to?(:new)
    @persistor = persistor.new(self)
  else
    # an already initialized persistor was passed in
    @persistor = persistor
  end
end
store() click to toggle source
# File lib/volt/models/helpers/base.rb, line 63
def store
  Volt.current_app.store
end