Class: Flows::Flow
- Inherits:
-
Object
- Object
- Flows::Flow
- Defined in:
- lib/flows/flow.rb,
lib/flows/flow/node.rb,
lib/flows/flow/errors.rb,
lib/flows/flow/router.rb,
lib/flows/flow/router/custom.rb,
lib/flows/flow/router/errors.rb,
lib/flows/flow/router/simple.rb
Overview
You should not use Flow in your business code. It's designed to be underlying execution engine for high-level abstractions. In other words - it's for libraries, not applications.
Abstraction for build deterministic finite-state machine-like execution objects.
Let's refer to 'deterministic finite-state machine' as DFSM.
It's NOT an implementation of DFSM. It just shares a lot of structural ideas. You can also think about Flow as an oriented graph, where:
- you have the one and only one initial node
- you start execution from the initial node
- after node execution your are going to some next node or stop execution.
And edges formed by possible next nodes.
DFSM has a very important property:
From any state, there is only one transition for any allowed input.
So, we represent DFSM states as Nodes. Each Node, basing on input (input includes execution context also) performs some side effects and returns output and next Node name (DFSM state).
Side effects here can be spitted into two types:
- modification of execution context
- rest of them: working with 3rd party libraries, printing to STDOUT, etc.
Final state represented by special symbol :end
.
Defined Under Namespace
Classes: Error, InvalidFirstNodeError, InvalidNodeRouteError, Node, Router
Instance Method Summary collapse
-
#call(input, context:) ⇒ Object
Executes a flow.
-
#initialize(start_node:, node_map:) ⇒ Flow
constructor
A new instance of Flow.
Constructor Details
#initialize(start_node:, node_map:) ⇒ Flow
Returns a new instance of Flow.
72 73 74 75 76 77 |
# File 'lib/flows/flow.rb', line 72 def initialize(start_node:, node_map:) @start_node = start_node @node_map = node_map check_routing_integrity end |
Instance Method Details
#call(input, context:) ⇒ Object
Executes a flow.
84 85 86 87 88 89 90 91 92 |
# File 'lib/flows/flow.rb', line 84 def call(input, context:) current_node_name = @start_node while current_node_name != :end input, current_node_name = @node_map[current_node_name].call(input, context: context) end input end |