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(property, default: nil) ⇒ Object



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

def accepts(property, default: nil)
  define_method property do
    if context.include? property
      value = context[property]
      value.is_a?(Proc) ? value.call : value
    else
      nil
    end
  end

  accepted << property

  defaults[property] = default if default
end

#commits(property) ⇒ Object



45
46
47
# File 'lib/cooperator.rb', line 45

def commits(property)
  committed << property
end

#committedObject



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

def committed
  @_committed ||= []
end

#defaultsObject



18
19
20
# File 'lib/cooperator.rb', line 18

def defaults
  @_defaults ||= {}
end

#expectedObject



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

def expected
  @_expected ||= []
end

#expects(property) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/cooperator.rb', line 22

def expects(property)
  define_method property do
    context[property]
  end

  expected << property
end

#perform(context = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cooperator.rb', line 49

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

  defaults.each do |property, value|
    context[property] = value unless context.include? property
  end

  action = new context

  catch :_finish do
    action.perform
  end

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

  action.context
end

#rollback(context = {}) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/cooperator.rb', line 73

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

  action.rollback if action.respond_to? :rollback

  action.context
end