Module: SequentialWorkflow

Defined in:
lib/sequential_workflow.rb,
lib/sequential_workflow/version.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#workflow_stateObject

Returns the value of attribute workflow_state.



5
6
7
# File 'lib/sequential_workflow.rb', line 5

def workflow_state
  @workflow_state
end

Instance Method Details

#catch(catch_with) ⇒ Object



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

def catch(catch_with)
  method(catch_with).call(@reason) if rejected?
end

#fulfill(value) ⇒ Object



31
32
33
34
35
# File 'lib/sequential_workflow.rb', line 31

def fulfill(value)
  @workflow_state = :fulfilled
  @value = value
  self
end

#reject(e) ⇒ Object



37
38
39
40
41
# File 'lib/sequential_workflow.rb', line 37

def reject(e)
  @workflow_state = :rejected
  @reason = e
  self
end

#rejected?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/sequential_workflow.rb', line 43

def rejected?
  @workflow_state == :rejected
end

#start(initial_method) ⇒ Object



7
8
9
10
11
12
# File 'lib/sequential_workflow.rb', line 7

def start(initial_method)
  result = method(initial_method).call
  fulfill result
rescue => e
  reject(e)
end

#then(on_success, rescue_with = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sequential_workflow.rb', line 18

def then(on_success, rescue_with = nil)
  case workflow_state
  when :fulfilled
    result = method(on_success).call(@value)
    fulfill result
  when :rejected
    method(rescue_with).call(@reason) if rescue_with
    self
  end
rescue => e
  reject e
end