Class: StatesLanguageMachine::States::Fail

Inherits:
Base show all
Defined in:
lib/ruby_slm/states/fail.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#comment, #definition

Attributes inherited from StatesLanguageMachine::State

#end_state, #name, #next_state, #type

Instance Method Summary collapse

Constructor Details

#initialize(name, definition) ⇒ Fail

Returns a new instance of Fail.

Parameters:

  • name (String)

    the name of the state

  • definition (Hash)

    the state definition



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_slm/states/fail.rb', line 13

def initialize(name, definition)
  # Don't call super - we need to handle this differently for Fail states
  @name = name
  @type = definition["Type"]
  @cause = definition["Cause"]
  @error = definition["Error"]
  @definition = definition
  @end_state = true  # Fail states are always end states
  @next_state = nil  # Fail states never have next states

  validate!
end

Instance Attribute Details

#causeString (readonly)

Returns the cause of the failure.

Returns:

  • (String)

    the cause of the failure



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

def cause
  @cause
end

#errorString (readonly)

Returns the error type.

Returns:

  • (String)

    the error type



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

def error
  @error
end

Instance Method Details

#end_state?Boolean

Fail states are always end states

Returns:

  • (Boolean)

    always true for fail states



44
45
46
# File 'lib/ruby_slm/states/fail.rb', line 44

def end_state?
  true
end

#execute(execution, input) ⇒ Hash

Returns the output data from the state.

Parameters:

  • execution (Execution)

    the current execution

  • input (Hash)

    the input data for the state

Returns:

  • (Hash)

    the output data from the state



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_slm/states/fail.rb', line 29

def execute(execution, input)
  execution.logger&.info("Executing fail state: #{@name}")

  # Set execution status to failed
  execution.status = :failed
  execution.error = @error
  execution.cause = @cause

  process_result(execution, input)

  input
end

#next_state_name(input = nil) ⇒ nil

Fail states don’t have next states

Returns:

  • (nil)

    always nil for fail states



50
51
52
# File 'lib/ruby_slm/states/fail.rb', line 50

def next_state_name(input = nil)
  nil
end

#validate!Object

Validate the fail state definition

Raises:



56
57
58
59
# File 'lib/ruby_slm/states/fail.rb', line 56

def validate!
  raise DefinitionError, "Fail state '#{@name}' must have a Cause" unless @cause
  raise DefinitionError, "Fail state '#{@name}' must have an Error" unless @error
end