Class: StatesLanguageMachine::State

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

Overview

Base state class that provides common functionality for all states

Direct Known Subclasses

StatesLanguageMachine::States::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, definition) ⇒ State

Returns a new instance of State.

Parameters:

  • name (String)

    the name of the state

  • definition (Hash)

    the state definition



17
18
19
20
21
22
23
# File 'lib/ruby_slm/state.rb', line 17

def initialize(name, definition)
  @name = name
  @type = definition["Type"]
  @next_state = definition["Next"]
  @end_state = definition.key?("End") && definition["End"]
  @definition = definition
end

Instance Attribute Details

#end_stateBoolean (readonly)

Returns whether this is an end state.

Returns:

  • (Boolean)

    whether this is an end state



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

def end_state
  @end_state
end

#nameString (readonly)

Returns the name of the state.

Returns:

  • (String)

    the name of the state



7
8
9
# File 'lib/ruby_slm/state.rb', line 7

def name
  @name
end

#next_stateString? (readonly)

Returns the next state name.

Returns:

  • (String, nil)

    the next state name



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

def next_state
  @next_state
end

#typeString (readonly)

Returns the type of the state.

Returns:

  • (String)

    the type of the state



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

def type
  @type
end

Instance Method Details

#end_state?Boolean

Returns whether this state is an end state.

Returns:

  • (Boolean)

    whether this state is an end state



34
35
36
# File 'lib/ruby_slm/state.rb', line 34

def end_state?
  @end_state
end

#execute(execution, input) ⇒ Hash

Execute the state (to be implemented by subclasses)

Parameters:

  • execution (Execution)

    the current execution

  • input (Hash)

    the input data

Returns:

  • (Hash)

    the output data

Raises:

  • (NotImplementedError)

    if not implemented by subclass



43
44
45
# File 'lib/ruby_slm/state.rb', line 43

def execute(execution, input)
  raise NotImplementedError, "Subclasses must implement execute method"
end

#next_state_name(input = nil) ⇒ String?

Get the next state name (can be overridden by subclasses that need input)

Parameters:

  • input (Hash, nil) (defaults to: nil)

    the input data (optional, for choice states)

Returns:

  • (String, nil)

    the next state name



28
29
30
31
# File 'lib/ruby_slm/state.rb', line 28

def next_state_name(input = nil)
  return nil if end_state?
  @next_state
end