Class: StatesLanguageMachine::States::Choice

Inherits:
Base show all
Defined in:
lib/ruby_slm/states/choice.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

Methods inherited from StatesLanguageMachine::State

#end_state?

Constructor Details

#initialize(name, definition) ⇒ Choice

Returns a new instance of Choice.



8
9
10
11
12
13
# File 'lib/ruby_slm/states/choice.rb', line 8

def initialize(name, definition)
  super
  @choices = definition["Choices"] || []
  @default = definition["Default"]
  @evaluated_next_state = nil
end

Instance Attribute Details

#choicesObject (readonly)

Returns the value of attribute choices.



6
7
8
# File 'lib/ruby_slm/states/choice.rb', line 6

def choices
  @choices
end

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/ruby_slm/states/choice.rb', line 6

def default
  @default
end

Instance Method Details

#execute(execution, input) ⇒ Object



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

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

  @evaluated_next_state = evaluate_choices(input, execution.logger)

  execution.logger&.info("Selected next state: #{@evaluated_next_state}")
  process_result(execution, input)
  input
end

#next_state_name(input = nil) ⇒ Object



25
26
27
28
29
# File 'lib/ruby_slm/states/choice.rb', line 25

def next_state_name(input = nil)
  return nil if end_state?
  return @evaluated_next_state if @evaluated_next_state
  input ? evaluate_choices(input) : @default
end