Class: StatesLanguageMachine::StateMachine
- Inherits:
-
Object
- Object
- StatesLanguageMachine::StateMachine
- Defined in:
- lib/ruby_slm/state_machine.rb
Instance Attribute Summary collapse
-
#comment ⇒ String?
readonly
The comment describing the state machine.
-
#definition ⇒ Hash
readonly
The raw definition of the state machine.
-
#start_state ⇒ String
readonly
The name of the starting state.
-
#states ⇒ Hash<String, States::Base>
readonly
Mapping of state names to state objects.
-
#timeout_seconds ⇒ Integer?
readonly
The timeout in seconds for the entire state machine.
Instance Method Summary collapse
-
#get_state(state_name) ⇒ States::Base
Get a state by name.
-
#initialize(definition, format: :yaml) ⇒ StateMachine
constructor
A new instance of StateMachine.
-
#start_execution(input = {}, execution_name = nil, context = {}) ⇒ Execution
Start a new execution of this state machine.
-
#state_names ⇒ Array<String>
The names of all states in the machine.
-
#to_h ⇒ Hash
The state machine definition as a Hash.
-
#to_json ⇒ String
The state machine definition as JSON.
-
#to_yaml ⇒ String
The state machine definition as YAML.
Constructor Details
#initialize(definition, format: :yaml) ⇒ StateMachine
Returns a new instance of StateMachine.
21 22 23 24 25 |
# File 'lib/ruby_slm/state_machine.rb', line 21 def initialize(definition, format: :yaml) @definition = parse_definition(definition, format) validate_definition! build_states end |
Instance Attribute Details
#comment ⇒ String? (readonly)
Returns the comment describing the state machine.
17 18 19 |
# File 'lib/ruby_slm/state_machine.rb', line 17 def comment @comment end |
#definition ⇒ Hash (readonly)
Returns the raw definition of the state machine.
9 10 11 |
# File 'lib/ruby_slm/state_machine.rb', line 9 def definition @definition end |
#start_state ⇒ String (readonly)
Returns the name of the starting state.
13 14 15 |
# File 'lib/ruby_slm/state_machine.rb', line 13 def start_state @start_state end |
#states ⇒ Hash<String, States::Base> (readonly)
Returns mapping of state names to state objects.
11 12 13 |
# File 'lib/ruby_slm/state_machine.rb', line 11 def states @states end |
#timeout_seconds ⇒ Integer? (readonly)
Returns the timeout in seconds for the entire state machine.
15 16 17 |
# File 'lib/ruby_slm/state_machine.rb', line 15 def timeout_seconds @timeout_seconds end |
Instance Method Details
#get_state(state_name) ⇒ States::Base
Get a state by name
40 41 42 |
# File 'lib/ruby_slm/state_machine.rb', line 40 def get_state(state_name) @states[state_name] || raise(StateNotFoundError, "State '#{state_name}' not found") end |
#start_execution(input = {}, execution_name = nil, context = {}) ⇒ Execution
Start a new execution of this state machine
32 33 34 |
# File 'lib/ruby_slm/state_machine.rb', line 32 def start_execution(input = {}, execution_name = nil, context = {}) Execution.new(self, input, execution_name, context) end |
#state_names ⇒ Array<String>
Returns the names of all states in the machine.
45 46 47 |
# File 'lib/ruby_slm/state_machine.rb', line 45 def state_names @states.keys end |
#to_h ⇒ Hash
Returns the state machine definition as a Hash.
50 51 52 |
# File 'lib/ruby_slm/state_machine.rb', line 50 def to_h @definition.dup end |
#to_json ⇒ String
Returns the state machine definition as JSON.
55 56 57 |
# File 'lib/ruby_slm/state_machine.rb', line 55 def to_json JSON.pretty_generate(@definition) end |
#to_yaml ⇒ String
Returns the state machine definition as YAML.
60 61 62 |
# File 'lib/ruby_slm/state_machine.rb', line 60 def to_yaml YAML.dump(@definition) end |