Class: OpenLoam::Workflow::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/open_loam/workflow.rb

Overview

Collects the DSL block and validates the machine as a whole before freezing it. Validation runs at the end, so state and transition may appear in any order inside the block.

Instance Method Summary collapse

Constructor Details

#initialize(column, initial) ⇒ Builder

Returns a new instance of Builder.



49
50
51
52
53
54
# File 'lib/open_loam/workflow.rb', line 49

def initialize(column, initial)
  @column = column.to_s
  @initial = initial&.to_s
  @states = []
  @transitions = {}
end

Instance Method Details

#build(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/open_loam/workflow.rb', line 69

def build(&block)
  instance_eval(&block)
  validate!

  Definition.new(
    column: @column,
    initial: @initial || @states.first,
    states: @states.freeze,
    transitions: @transitions.freeze
  ).freeze
end

#state(name) ⇒ Object



56
57
58
# File 'lib/open_loam/workflow.rb', line 56

def state(name)
  @states << name.to_s
end

#transition(name, from:, to:, roles: []) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/open_loam/workflow.rb', line 60

def transition(name, from:, to:, roles: [])
  @transitions[name.to_sym] = Transition.new(
    name: name.to_sym,
    from: Array(from).map(&:to_s).freeze,
    to: to.to_s,
    roles: Array(roles).map(&:to_sym).freeze
  ).freeze
end