Class: StatesLanguageMachine::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_slm/state_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, format: :yaml) ⇒ StateMachine

Returns a new instance of StateMachine.

Parameters:

  • definition (String, Hash)

    the state machine definition

  • format (Symbol) (defaults to: :yaml)

    the format of the definition (:yaml, :json, :hash)



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

#commentString? (readonly)

Returns the comment describing the state machine.

Returns:

  • (String, nil)

    the comment describing the state machine



17
18
19
# File 'lib/ruby_slm/state_machine.rb', line 17

def comment
  @comment
end

#definitionHash (readonly)

Returns the raw definition of the state machine.

Returns:

  • (Hash)

    the raw definition of the state machine



9
10
11
# File 'lib/ruby_slm/state_machine.rb', line 9

def definition
  @definition
end

#start_stateString (readonly)

Returns the name of the starting state.

Returns:

  • (String)

    the name of the starting state



13
14
15
# File 'lib/ruby_slm/state_machine.rb', line 13

def start_state
  @start_state
end

#statesHash<String, States::Base> (readonly)

Returns mapping of state names to state objects.

Returns:

  • (Hash<String, States::Base>)

    mapping of state names to state objects



11
12
13
# File 'lib/ruby_slm/state_machine.rb', line 11

def states
  @states
end

#timeout_secondsInteger? (readonly)

Returns the timeout in seconds for the entire state machine.

Returns:

  • (Integer, nil)

    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

Parameters:

  • state_name (String)

    the name of the state to retrieve

Returns:

Raises:



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

Parameters:

  • input (Hash) (defaults to: {})

    the input data for the execution

  • execution_name (String, nil) (defaults to: nil)

    the name of the execution

  • context (Hash) (defaults to: {})

    additional context for the execution

Returns:



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_namesArray<String>

Returns the names of all states in the machine.

Returns:

  • (Array<String>)

    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_hHash

Returns the state machine definition as a Hash.

Returns:

  • (Hash)

    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_jsonString

Returns the state machine definition as JSON.

Returns:

  • (String)

    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_yamlString

Returns the state machine definition as YAML.

Returns:

  • (String)

    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