Class: Gaskit::Flow
Overview
The ‘Gaskit::Flow` class defines and executes a pipeline of operations, each of which returns a `Gaskit::OperationResult`. Flows can be defined via a class-based DSL or run inline using a block.
Steps share context and flow state, with support for early exits, manual control, and step-by-step walking. A flow is constructed from a sequence of registered operations, each of which may receive and return input to influence subsequent steps.
## Features
-
Declarative or block-based step definitions
-
Per-step argument and context overrides
-
Shared flow-level context with metadata injection
-
Manual stepping (‘walk` and `next_step`)
-
Rewind capability for retryable flows
-
Hookable lifecycle via ‘Gaskit::Hookable`
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
-
.call(*args, context: {}, **kwargs, &block) ⇒ FlowResult
Executes the flow with soft failure handling.
-
.call!(*args, context: {}, **kwargs, &block) ⇒ FlowResult
Executes the flow with hard failure handling (raises on unhandled errors).
-
.inherited(subclass) ⇒ void
Called when a subclass is defined, initializing an empty step list.
-
.invoke(raise_on_failure, context, *args, **kwargs, &block) ⇒ FlowResult
Internal flow execution logic.
-
.step(operation, context: {}, **kwargs) ⇒ void
Registers a step in the class-level flow definition.
-
.steps ⇒ Array<Array>
Returns the list of declared steps for the flow class.
-
.walk(*args, context: {}, **kwargs) ⇒ Flow
Creates a flow instance for step-by-step execution.
-
.walk!(*args, context: {}, **kwargs) ⇒ Flow
Same as #walk but raises on any step failure.
Instance Method Summary collapse
-
#next_step(*args, **kwargs) ⇒ Gaskit::OperationResult?
Executes the next step in the flow.
-
#next_step? ⇒ Boolean
Returns true if there are more steps remaining in the sequence.
-
#pending_step(*args, **kwargs) ⇒ Hash?
Returns the metadata for the pending step.
-
#results ⇒ Array<Hash>
Returns the result hashes from all executed steps.
-
#rewind ⇒ void
Rewinds the flow to its initial state.
-
#step(operation, context: {}, **kwargs) ⇒ Gaskit::OperationResult
Runs a step inline from within a block-based flow definition.
Methods included from Hookable
#apply_after_hooks, #apply_around_hooks, #apply_before_hooks, #apply_hooks, included
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
155 156 157 |
# File 'lib/gaskit/flow.rb', line 155 def logger @logger end |
Class Method Details
.call(*args, context: {}, **kwargs, &block) ⇒ FlowResult
Executes the flow with soft failure handling.
94 95 96 |
# File 'lib/gaskit/flow.rb', line 94 def call(*args, context: {}, **kwargs, &block) invoke(false, context, *args, **kwargs, &block) end |
.call!(*args, context: {}, **kwargs, &block) ⇒ FlowResult
Executes the flow with hard failure handling (raises on unhandled errors).
105 106 107 |
# File 'lib/gaskit/flow.rb', line 105 def call!(*args, context: {}, **kwargs, &block) invoke(true, context, *args, **kwargs, &block) end |
.inherited(subclass) ⇒ void
This method returns an undefined value.
Called when a subclass is defined, initializing an empty step list.
66 67 68 69 |
# File 'lib/gaskit/flow.rb', line 66 def inherited(subclass) subclass.instance_variable_set(:@steps, @steps) super end |
.invoke(raise_on_failure, context, *args, **kwargs, &block) ⇒ FlowResult
Internal flow execution logic.
136 137 138 139 |
# File 'lib/gaskit/flow.rb', line 136 def invoke(raise_on_failure, context, *args, **kwargs, &block) flow = build(raise_on_failure, context, *args, **kwargs, &block) flow.send(:execute, &block) end |
.step(operation, context: {}, **kwargs) ⇒ void
This method returns an undefined value.
Registers a step in the class-level flow definition.
84 85 86 |
# File 'lib/gaskit/flow.rb', line 84 def step(operation, context: {}, **kwargs) steps << [operation, context, kwargs] end |
.steps ⇒ Array<Array>
Returns the list of declared steps for the flow class.
74 75 76 |
# File 'lib/gaskit/flow.rb', line 74 def steps @steps ||= [] end |
.walk(*args, context: {}, **kwargs) ⇒ Flow
Creates a flow instance for step-by-step execution.
115 116 117 |
# File 'lib/gaskit/flow.rb', line 115 def walk(*args, context: {}, **kwargs) build(false, context, *args, **kwargs) end |
.walk!(*args, context: {}, **kwargs) ⇒ Flow
Same as #walk but raises on any step failure.
125 126 127 |
# File 'lib/gaskit/flow.rb', line 125 def walk!(*args, context: {}, **kwargs) build(true, context, *args, **kwargs) end |
Instance Method Details
#next_step(*args, **kwargs) ⇒ Gaskit::OperationResult?
Executes the next step in the flow.
186 187 188 189 190 191 192 193 |
# File 'lib/gaskit/flow.rb', line 186 def next_step(*args, **kwargs) return unless next_step? operation, context, step_kwargs = @step_sequence[@step_index] @step_index += 1 process_step(operation, context, step_kwargs, args, kwargs) end |
#next_step? ⇒ Boolean
Returns true if there are more steps remaining in the sequence.
160 161 162 |
# File 'lib/gaskit/flow.rb', line 160 def next_step? @step_index < @step_sequence.size end |
#pending_step(*args, **kwargs) ⇒ Hash?
Returns the metadata for the pending step.
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/gaskit/flow.rb', line 167 def pending_step(*args, **kwargs) return nil unless next_step? operation, context, step_kwargs = @step_sequence[@step_index] context = @context.merge(context) args, kwargs = resolve_step_input( args: args, kwargs: kwargs, step_kwargs: step_kwargs ) { operation: operation, context: context, args: args, kwargs: kwargs } end |
#results ⇒ Array<Hash>
Returns the result hashes from all executed steps.
218 219 220 |
# File 'lib/gaskit/flow.rb', line 218 def results @steps.map { |entry| entry[:result] } end |
#rewind ⇒ void
This method returns an undefined value.
Rewinds the flow to its initial state.
208 209 210 211 212 213 |
# File 'lib/gaskit/flow.rb', line 208 def rewind @input = @initial_input.dup @result = nil @steps.clear @step_index = 0 end |
#step(operation, context: {}, **kwargs) ⇒ Gaskit::OperationResult
Runs a step inline from within a block-based flow definition.
201 202 203 |
# File 'lib/gaskit/flow.rb', line 201 def step(operation, context: {}, **kwargs) process_step(operation, context, kwargs) end |