# File lib/mor/model.rb, line 26 def primary_key :id end
module Mor::Model::ClassMethods
Public Instance Methods
all()
click to toggle source
# File lib/mor/model.rb, line 62 def all self.index.map{|id| find(id) } end
attr_accessor(*attrs)
click to toggle source
Calls superclass method
# 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 = {})
click to toggle source
# File lib/mor/model.rb, line 30 def create attributes = {} instance = self.new(attributes) instance.save instance end
deserialize(object)
click to toggle source
# File lib/mor/model.rb, line 70 def deserialize object object end
find(id)
click to toggle source
# 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)
click to toggle source
# File lib/mor/model.rb, line 57 def find_by hash return nil unless hash where(hash).first end
index()
click to toggle source
# File lib/mor/model.rb, line 36 def index Mor.cache.get(self.key) || (self.index=[]) end
index=(index)
click to toggle source
# File lib/mor/model.rb, line 40 def index=index Mor.cache.set(self.key,index) ? index : [] end
key(primary_key=self.primary_key)
click to toggle source
# File lib/mor/model.rb, line 22 def key primary_key=self.primary_key :"#{self.name}:#{primary_key}" end
primary_key()
click to toggle source
serialize(object)
click to toggle source
# File lib/mor/model.rb, line 66 def serialize object object end
where(hash)
click to toggle source
# 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