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, persistence: nil) ⇒ Builder

Returns a new instance of Builder.



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

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

Instance Method Details

#buildObject



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
174
# File 'lib/phronomy/workflow.rb', line 135

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,
    persistence: @persistence
  )
  Workflow.new(runner)
end

#entry(name, callable) ⇒ Object



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

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

#exit(name, callable) ⇒ Object



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

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

#initial(state_name) ⇒ Object

rubocop:disable Style/TrivialAccessors



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

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.



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

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.



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

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



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

def wait_state(name)
  @wait_state_names << name
end