Class: MotionRecord::Base

Inherits:
Object
  • Object
show all
Includes:
Persistence
Defined in:
lib/motion_record/base.rb

Direct Known Subclasses

Schema::Migration

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#delete!, #mark_persisted!, #mark_unpersisted!, #persisted?, #primary_key_condition, #save!

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
# File 'lib/motion_record/base.rb', line 5

def initialize(attributes={})
  initialize_from_attribute_hash(attributes)
end

Class Method Details

.attribute_defaultsObject



45
46
47
# File 'lib/motion_record/base.rb', line 45

def attribute_defaults
  @attribute_defaults ||= {}
end

.attribute_namesObject



41
42
43
# File 'lib/motion_record/base.rb', line 41

def attribute_names
  @attribute_names ||= []
end

.connectionObject



49
50
51
# File 'lib/motion_record/base.rb', line 49

def connection
  ConnectionAdapters::SQLiteAdapter.instance
end

.define_attribute(name, options = {}) ⇒ Object

Add attribute methods to the model

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

:default - default value for the attribute (nil otherwise)


33
34
35
36
37
38
39
# File 'lib/motion_record/base.rb', line 33

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

Instance Method Details

#connectionObject



15
16
17
# File 'lib/motion_record/base.rb', line 15

def connection
  self.class.connection
end

#to_attribute_hashObject



9
10
11
12
13
# File 'lib/motion_record/base.rb', line 9

def to_attribute_hash
  self.class.attribute_names.each_with_object({}) do |name, hash|
    hash[name] = self.instance_variable_get "@#{name}"
  end
end