Module: Uorm::CRUD

Defined in:
lib/uorm/crud.rb,
lib/uorm/crud/callbacks.rb,
lib/uorm/crud/persistance_helper.rb

Defined Under Namespace

Modules: Callbacks, PersistanceHelper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/uorm/crud.rb', line 6

def self.included base
  base.class_eval do
    include ::Uorm::CRUD::PersistanceHelper
  end

  base.instance_eval do
    def all args = {}, &block
      if block_given?
        persistance.all(args) { |attrs| Fiber.new { yield new(attrs) }.resume if attrs }
      else
        persistance.all(args).map! { |attrs| new(attrs) }
      end
    end

    def find id, &block
      if block_given?
        persistance.find(id) { |attrs| Fiber.new { yield new(attrs) }.resume if attrs }
      else
        new persistance.find(id)
      end
    end

    def create attrs, &block
      obj = new(attrs)
      block_given? ? obj.save(&block) : obj.save
    end
  end
end

Instance Method Details

#deleteObject



69
70
71
72
# File 'lib/uorm/crud.rb', line 69

def delete
  persistance.delete self
  self
end

#save(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/uorm/crud.rb', line 35

def save &block
  if id
    if block_given?
      cb = ::Uorm::CRUD::Callbacks::Update.new
      cb.callback(&block)
      persistance.update self, cb
    else
      persistance.update self
    end
  else
    if block_given?
      cb = ::Uorm::CRUD::Callbacks::Create.new
      cb.callback(&block)
      persistance.create self, cb
    else
      persistance.create self
    end
  end

  self
end

#update(attrs, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/uorm/crud.rb', line 57

def update attrs, &block
  update_attributes attrs
  if block_given?
    cb = ::Uorm::CRUD::Callbacks::Update.new
    cb.callback(&block)
    persistance.update self, cb
  else
    persistance.update self
  end
  self
end