Class: MotionRecord::Base
- Inherits:
-
Object
- Object
- MotionRecord::Base
- Includes:
- Persistence, ScopeHelpers, Serialization
- Defined in:
- lib/motion_record/base.rb
Direct Known Subclasses
Constant Summary
Constants included from Persistence
Persistence::TIMESTAMP_COLUMNS
Class Method Summary collapse
- .attribute_defaults ⇒ Object
- .attribute_names ⇒ Object
- .connection ⇒ Object
-
.define_attribute(name, options = {}) ⇒ Object
Add attribute methods to the model.
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
- #to_attribute_hash ⇒ Object
Methods included from ScopeHelpers
Methods included from Persistence
#destroy, #mark_persisted!, #mark_unpersisted!, #persisted?, #save
Methods included from Serialization
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_defaults ⇒ Object
47 48 49 |
# File 'lib/motion_record/base.rb', line 47 def attribute_defaults @attribute_defaults ||= {} end |
.attribute_names ⇒ Object
43 44 45 |
# File 'lib/motion_record/base.rb', line 43 def attribute_names @attribute_names ||= [] end |
.connection ⇒ Object
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, = {}) attr_accessor name self.attribute_names << name.to_sym if [:default] self.attribute_defaults[name.to_sym] = [:default] end end |
Instance Method Details
#connection ⇒ Object
17 18 19 |
# File 'lib/motion_record/base.rb', line 17 def connection self.class.connection end |
#to_attribute_hash ⇒ Object
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 |