Class: MotionRecord::Base

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

Direct Known Subclasses

Schema::Migration

Constant Summary

Constants included from Persistence

Persistence::TIMESTAMP_COLUMNS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ScopeHelpers

included

Methods included from Persistence

#destroy, #mark_persisted!, #mark_unpersisted!, #persisted?, #save

Methods included from Serialization

included

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/motion_record/base.rb', line 7

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

Class Method Details

.attribute_defaultsObject



47
48
49
# File 'lib/motion_record/base.rb', line 47

def attribute_defaults
  @attribute_defaults ||= {}
end

.attribute_namesObject



43
44
45
# File 'lib/motion_record/base.rb', line 43

def attribute_names
  @attribute_names ||= []
end

.connectionObject



51
52
53
# File 'lib/motion_record/base.rb', line 51

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)


35
36
37
38
39
40
41
# 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

Instance Method Details

#connectionObject



17
18
19
# File 'lib/motion_record/base.rb', line 17

def connection
  self.class.connection
end

#to_attribute_hashObject



11
12
13
14
15
# 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