Sha256: dbf5a59a7a927e05ef872692c04f5ddfc21e2d5cf4e076e89dcb82297bd17d2f

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

# A place for things shared between an ArrayModel and a Model

module ModelHelpers
  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
  
  # Pass to the persisotr
  def event_added(event, scope_provider, first)
    @persistor.event_added(event, scope_provider, first) if @persistor
  end
  
  # Pass to the persistor
  def event_removed(event, no_more_events)
    @persistor.event_removed(event, no_more_events) if @persistor
  end
  
  # Gets the class for a model at the specified path.
  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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
volt-0.6.5 lib/volt/models/model_helpers.rb
volt-0.6.4 lib/volt/models/model_helpers.rb
volt-0.6.3 lib/volt/models/model_helpers.rb
volt-0.6.2 lib/volt/models/model_helpers.rb
volt-0.6.1 lib/volt/models/model_helpers.rb