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
- #find_by(hash) ⇒ Object
- #index ⇒ Object
- #index=(index) ⇒ Object
- #key(primary_key = self.primary_key) ⇒ Object
- #primary_key ⇒ Object
- #serialize(object) ⇒ Object
- #where(hash) ⇒ Object
Instance Method Details
#all ⇒ Object
62 63 64 |
# File 'lib/mor/model.rb', line 62 def all self.index.map{|id| find(id) } end |
#attr_accessor(*attrs) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mor/model.rb', line 11 def attr_accessor *attrs super(*attrs) attrs.delete_if{ |a| a == :attributes }.each { |attr| define_method(:"#{attr}="){ |val| self.attributes[attr]=val self.attributes[:id]=val if attr == self.class.primary_key && attr != :id } define_method(attr){ self.attributes[attr] } } end |
#create(attributes = {}) ⇒ Object
30 31 32 33 34 |
# File 'lib/mor/model.rb', line 30 def create attributes = {} instance = self.new(attributes) instance.save instance end |
#deserialize(object) ⇒ Object
70 71 72 |
# File 'lib/mor/model.rb', line 70 def deserialize object object end |
#find(id) ⇒ Object
44 45 46 47 48 |
# File 'lib/mor/model.rb', line 44 def find id if object = Mor.cache.get(key(id)) self.deserialize(object) end end |
#find_by(hash) ⇒ Object
57 58 59 60 |
# File 'lib/mor/model.rb', line 57 def find_by hash return nil unless hash where(hash).first end |
#index ⇒ Object
36 37 38 |
# File 'lib/mor/model.rb', line 36 def index Mor.cache.get(self.key) || (self.index=[]) end |
#index=(index) ⇒ Object
40 41 42 |
# File 'lib/mor/model.rb', line 40 def index=index Mor.cache.set(self.key,index) ? index : [] end |
#key(primary_key = self.primary_key) ⇒ Object
22 23 24 |
# File 'lib/mor/model.rb', line 22 def key primary_key=self.primary_key :"#{self.name}:#{primary_key}" end |
#primary_key ⇒ Object
26 27 28 |
# File 'lib/mor/model.rb', line 26 def primary_key :id end |
#serialize(object) ⇒ Object
66 67 68 |
# File 'lib/mor/model.rb', line 66 def serialize object object end |
#where(hash) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/mor/model.rb', line 50 def where hash return nil unless hash self.all.select{ |app| app.attributes.select{ |k,v| hash.keys.include?(k.to_sym) }.values.sort == hash.values.sort } end |