Class: OrchestrateFlow::Workflow
- Inherits:
-
Object
- Object
- OrchestrateFlow::Workflow
- Defined in:
- lib/orchestrate_flow/workflow.rb
Overview
The Workflow class represents a basic state machine for defining workflows with states, transitions, and event-driven actions. It allows developers to manage complex workflows using defined states and triggers.
Defined Under Namespace
Classes: InvalidTransitionError
Class Attribute Summary collapse
-
.event_actions ⇒ Object
readonly
Returns the value of attribute event_actions.
-
.states ⇒ Object
readonly
Returns the value of attribute states.
-
.transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
-
.on(event, &action) ⇒ Object
Define actions to trigger on events.
-
.state(name) ⇒ Object
Define a new state.
-
.transition(event:, from:, to:) ⇒ Object
Define a transition between states.
Instance Method Summary collapse
-
#initialize ⇒ Workflow
constructor
A new instance of Workflow.
-
#trigger(event) ⇒ Object
Execute a transition event.
Constructor Details
#initialize ⇒ Workflow
Returns a new instance of Workflow.
10 11 12 13 14 |
# File 'lib/orchestrate_flow/workflow.rb', line 10 def initialize @state = :pending @transitions = {} @event_actions = {} end |
Class Attribute Details
.event_actions ⇒ Object (readonly)
Returns the value of attribute event_actions.
55 56 57 |
# File 'lib/orchestrate_flow/workflow.rb', line 55 def event_actions @event_actions end |
.states ⇒ Object (readonly)
Returns the value of attribute states.
47 48 49 |
# File 'lib/orchestrate_flow/workflow.rb', line 47 def states @states end |
.transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
51 52 53 |
# File 'lib/orchestrate_flow/workflow.rb', line 51 def transitions @transitions end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
8 9 10 |
# File 'lib/orchestrate_flow/workflow.rb', line 8 def state @state end |
Class Method Details
.on(event, &action) ⇒ Object
Define actions to trigger on events
30 31 32 33 |
# File 'lib/orchestrate_flow/workflow.rb', line 30 def self.on(event, &action) @event_actions ||= {} @event_actions[event] = action end |
.state(name) ⇒ Object
Define a new state
17 18 19 20 21 |
# File 'lib/orchestrate_flow/workflow.rb', line 17 def self.state(name) @states ||= [] @states << name define_method("#{name}?") { @state == name } end |
.transition(event:, from:, to:) ⇒ Object
Define a transition between states
24 25 26 27 |
# File 'lib/orchestrate_flow/workflow.rb', line 24 def self.transition(event:, from:, to:) @transitions ||= {} @transitions[event] = { from: from, to: to } end |
Instance Method Details
#trigger(event) ⇒ Object
Execute a transition event
36 37 38 39 40 41 42 43 |
# File 'lib/orchestrate_flow/workflow.rb', line 36 def trigger(event) unless valid_transition?(event) raise InvalidTransitionError, "Cannot transition from #{@state} using event #{event}" end execute_event_action(event) @state = self.class.transitions[event][:to] end |