Class: Phronomy::Workflow::Builder

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

Constant Summary collapse

FINISH =
Phronomy::WorkflowRunner::FINISH

Instance Method Summary collapse

Constructor Details

#initialize(context_class, state_store: nil) ⇒ Builder

Returns a new instance of Builder.



79
80
81
82
83
84
85
86
87
88
# File 'lib/phronomy/workflow.rb', line 79

def initialize(context_class, state_store: nil)
  @context_class = context_class
  @state_store = state_store
  @initial = nil
  @declared_states = []
  @entry_actions = {}
  @exit_actions = {}
  @transitions = []
  @wait_state_names = []
end

Instance Method Details

#buildObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/phronomy/workflow.rb', line 134

def build
  validate_graph!

  auto_transitions = []
  external_events = {}
  @transitions.each do |transition|
    if transition[:on]
      event_name = transition[:on].to_sym
      external_events[event_name] ||= []
      external_events[event_name] << {
        from: transition[:from],
        to: transition[:to],
        guard: transition[:guard],
        action: transition[:action],
        event: event_name
      }
    else
      auto_transitions << {
        from: transition[:from],
        to: transition[:to],
        guard: transition[:guard],
        action: transition[:action],
        event: :state_completed
      }
    end
  end

  runner = Phronomy::WorkflowRunner.new(
    state_class: @context_class,
    entry_actions: @entry_actions.dup,
    exit_actions: @exit_actions.dup,
    declared_states: @declared_states.dup,
    auto_transitions: auto_transitions,
    external_events: external_events,
    entry_point: @initial || @declared_states.first,
    wait_state_names: @wait_state_names.dup,
    state_store: @state_store
  )
  Workflow.new(runner)
end

#entry(name, callable) ⇒ Object



104
105
106
# File 'lib/phronomy/workflow.rb', line 104

def entry(name, callable)
  (@entry_actions[name] ||= []) << callable
end

#exit(name, callable) ⇒ Object



108
109
110
# File 'lib/phronomy/workflow.rb', line 108

def exit(name, callable)
  (@exit_actions[name] ||= []) << callable
end

#initial(state_name) ⇒ Object

rubocop:disable Style/TrivialAccessors



90
91
92
# File 'lib/phronomy/workflow.rb', line 90

def initial(state_name) # rubocop:disable Style/TrivialAccessors
  @initial = state_name
end

#state(name, action: nil) ⇒ Object

Declares a Workflow state.

Entry actions are synchronous Run-to-Completion callbacks. To start asynchronous work, register its listener/callback inside the action and return the context or nil. Returning Phronomy::Task is an error.



99
100
101
102
# File 'lib/phronomy/workflow.rb', line 99

def state(name, action: nil)
  @declared_states << name
  entry(name, action) if action
end

#transition(from:, to:, guard: nil, on: nil, action: nil) ⇒ Object

Declares a transition.

Guards and actions may accept either (context) or (context, event). Transition actions are synchronous Run-to-Completion callbacks executed after the source exit callbacks and before the target entry callbacks. They may start asynchronous work and register listeners, but returning Phronomy::Task is an error; completion must arrive as a later event.



123
124
125
126
127
128
129
130
131
132
# File 'lib/phronomy/workflow.rb', line 123

def transition(from:, to:, guard: nil, on: nil, action: nil)
  destination = (to == :__finish__) ? FINISH : to
  @transitions << {
    from: from,
    to: destination,
    guard: guard,
    on: on,
    action: action
  }
end

#wait_state(name) ⇒ Object



112
113
114
# File 'lib/phronomy/workflow.rb', line 112

def wait_state(name)
  @wait_state_names << name
end