Module: Cooperator::ClassMethods

Defined in:
lib/cooperator.rb

Instance Method Summary collapse

Instance Method Details

#acceptedObject



10
11
12
# File 'lib/cooperator.rb', line 10

def accepted
  @_accepted ||= []
end

#accepts(*properties) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cooperator.rb', line 28

def accepts(*properties)
  properties.each do |property|
    define_method property do
      if context.include? property
        context.send property
      else
        nil
      end
    end

    accepted << property
  end
end

#commits(*properties) ⇒ Object



42
43
44
45
46
# File 'lib/cooperator.rb', line 42

def commits(*properties)
  properties.each do |property|
    committed << property
  end
end

#committedObject



14
15
16
# File 'lib/cooperator.rb', line 14

def committed
  @_committed ||= []
end

#expectedObject



6
7
8
# File 'lib/cooperator.rb', line 6

def expected
  @_expected ||= []
end

#expects(*properties) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/cooperator.rb', line 18

def expects(*properties)
  properties.each do |property|
    define_method property do
      context.send property
    end

    expected << property
  end
end

#perform(context = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cooperator.rb', line 48

def perform(context = {})
  expected.each do |property|
    raise Exception, "missing expected property: #{property}" unless context.include? property
  end

  action = new context

  catch :_finish do
    action.perform
  end

  committed.each do |property|
    raise Exception, "missing committed property: #{property}" unless action.context.include? property
  end

  action.context
end

#rollback(context = {}) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/cooperator.rb', line 66

def rollback(context = {})
  action = new context

  action.rollback if action.respond_to? :rollback

  action.context
end