Class: Roast::Workflow::CaseStep

Inherits:
BaseStep
  • Object
show all
Includes:
ExpressionEvaluator
Defined in:
lib/roast/workflow/case_step.rb

Instance Attribute Summary

Attributes inherited from BaseStep

#available_tools, #coerce_to, #context_path, #json, #model, #name, #params, #print_response, #resource, #workflow

Instance Method Summary collapse

Methods included from ExpressionEvaluator

#evaluate_bash_command, #evaluate_ruby_expression, #evaluate_step_or_value

Methods included from ExpressionUtils

#bash_command?, #extract_command, #extract_expression, #ruby_expression?

Constructor Details

#initialize(workflow, config:, name:, context_path:, workflow_executor:, **kwargs) ⇒ CaseStep

Returns a new instance of CaseStep.



8
9
10
11
12
13
14
15
16
# File 'lib/roast/workflow/case_step.rb', line 8

def initialize(workflow, config:, name:, context_path:, workflow_executor:, **kwargs)
  super(workflow, name: name, context_path: context_path, **kwargs)

  @config = config
  @case_expression = config["case"]
  @when_clauses = config["when"] || {}
  @else_steps = config["else"] || []
  @workflow_executor = workflow_executor
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/roast/workflow/case_step.rb', line 18

def call
  # Evaluate the case expression to get the value to match against
  case_value = evaluate_case_expression(@case_expression)

  # Find the matching when clause
  matched_key = find_matching_when_clause(case_value)

  # Determine which steps to execute
  steps_to_execute = if matched_key
    @when_clauses[matched_key]
  else
    @else_steps
  end

  # Execute the selected steps
  unless steps_to_execute.nil? || steps_to_execute.empty?
    @workflow_executor.execute_steps(steps_to_execute)
  end

  # Return a result indicating which branch was taken
  {
    case_value: case_value,
    matched_when: matched_key,
    branch_executed: matched_key || (steps_to_execute.empty? ? "none" : "else"),
  }
end