Module: Mor::Model::ClassMethods
- Defined in:
- lib/mor/model.rb
Instance Method Summary collapse
- #all ⇒ Object
- #attr_accessor(*attrs) ⇒ Object
- #create(attributes = {}) ⇒ Object
- #deserialize(object) ⇒ Object
- #find(id) ⇒ Object
- #index ⇒ Object
- #index=(index) ⇒ Object
- #key(primary_key = self.primary_key) ⇒ Object
- #primary_key ⇒ Object
- #serialize(object) ⇒ Object
Instance Method Details
#all ⇒ Object
44 45 46 |
# File 'lib/mor/model.rb', line 44 def all self.index.map{|id| find(id) } end |
#attr_accessor(*attrs) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/mor/model.rb', line 10 def attr_accessor *attrs super(*attrs) attrs.delete_if{ |a| a == :attributes }.each { |attr| define_method(:"#{attr}="){|val| self.attributes[attr]=val} define_method(attr){ self.attributes[attr] } } end |
#create(attributes = {}) ⇒ Object
26 27 28 29 30 |
# File 'lib/mor/model.rb', line 26 def create attributes = {} instance = self.new(attributes) instance.save instance end |
#deserialize(object) ⇒ Object
52 53 54 |
# File 'lib/mor/model.rb', line 52 def deserialize object object end |
#find(id) ⇒ Object
40 41 42 |
# File 'lib/mor/model.rb', line 40 def find id self.deserialize(Mor.cache.get(key(id))) end |
#index ⇒ Object
32 33 34 |
# File 'lib/mor/model.rb', line 32 def index Mor.cache.get(self.key) || (self.index=[]) end |
#index=(index) ⇒ Object
36 37 38 |
# File 'lib/mor/model.rb', line 36 def index=index Mor.cache.set(self.key,index) ? index : [] end |
#key(primary_key = self.primary_key) ⇒ Object
18 19 20 |
# File 'lib/mor/model.rb', line 18 def key primary_key=self.primary_key :"#{self.name}:#{primary_key}" end |
#primary_key ⇒ Object
22 23 24 |
# File 'lib/mor/model.rb', line 22 def primary_key :id end |
#serialize(object) ⇒ Object
48 49 50 |
# File 'lib/mor/model.rb', line 48 def serialize object object end |