Module: Mor::Model::ClassMethods

Defined in:
lib/mor/model.rb

Instance Method Summary collapse

Instance Method Details

#allObject



53
54
55
# File 'lib/mor/model.rb', line 53

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



61
62
63
# File 'lib/mor/model.rb', line 61

def deserialize object
  object
end

#find(id) ⇒ Object



40
41
42
43
44
# File 'lib/mor/model.rb', line 40

def find id
  if object = Mor.cache.get(key(id))
    self.deserialize(object)
  end
end

#find_by(hash) ⇒ Object



46
47
48
49
50
51
# File 'lib/mor/model.rb', line 46

def find_by 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 
  }.first
end

#indexObject



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_keyObject



22
23
24
# File 'lib/mor/model.rb', line 22

def primary_key
  :id
end

#serialize(object) ⇒ Object



57
58
59
# File 'lib/mor/model.rb', line 57

def serialize object
  object
end