class MotionRecord::Base

Public Class Methods

new(attributes={}) click to toggle source
# File lib/motion_record/base.rb, line 7
def initialize(attributes={})
  initialize_from_attribute_hash(attributes)
end

Protected Class Methods

attribute_defaults() click to toggle source
# File lib/motion_record/base.rb, line 47
def attribute_defaults
  @attribute_defaults ||= {}
end
attribute_names() click to toggle source
# File lib/motion_record/base.rb, line 43
def attribute_names
  @attribute_names ||= []
end
connection() click to toggle source
# File lib/motion_record/base.rb, line 51
def connection
  ConnectionAdapters::SQLiteAdapter.instance
end
define_attribute(name, options = {}) click to toggle source

Add attribute methods to the model

name - Symobl name of the attribute options - optional configuration Hash:

:default - default value for the attribute (nil otherwise)
# File lib/motion_record/base.rb, line 35
def define_attribute(name, options = {})
  attr_accessor name
  self.attribute_names << name.to_sym
  if options[:default]
    self.attribute_defaults[name.to_sym] = options[:default]
  end
end

Public Instance Methods

connection() click to toggle source
# File lib/motion_record/base.rb, line 17
def connection
  self.class.connection
end
to_attribute_hash() click to toggle source
# File lib/motion_record/base.rb, line 11
def to_attribute_hash
  self.class.attribute_names.each_with_object({}) do |name, hash|
    hash[name] = self.instance_variable_get "@#{name}"
  end
end

Protected Instance Methods

initialize_from_attribute_hash(hash) click to toggle source
# File lib/motion_record/base.rb, line 23
def initialize_from_attribute_hash(hash)
  self.class.attribute_defaults.merge(hash).each do |name, value|
    self.instance_variable_set "@#{name}", value
  end
end