Class: Roast::Workflow::EachStep

Inherits:
BaseIterationStep show all
Defined in:
lib/roast/workflow/each_step.rb

Overview

Executes steps for each item in a collection

Constant Summary

Constants inherited from BaseIterationStep

BaseIterationStep::DEFAULT_MAX_ITERATIONS

Instance Attribute Summary collapse

Attributes inherited from BaseIterationStep

#config_hash, #steps

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 ExpressionUtils

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

Constructor Details

#initialize(workflow, collection_expr:, variable_name:, steps:, **kwargs) ⇒ EachStep

Returns a new instance of EachStep.



9
10
11
12
13
# File 'lib/roast/workflow/each_step.rb', line 9

def initialize(workflow, collection_expr:, variable_name:, steps:, **kwargs)
  super(workflow, steps: steps, **kwargs)
  @collection_expr = collection_expr
  @variable_name = variable_name
end

Instance Attribute Details

#collection_exprObject (readonly)

Returns the value of attribute collection_expr.



7
8
9
# File 'lib/roast/workflow/each_step.rb', line 7

def collection_expr
  @collection_expr
end

#variable_nameObject (readonly)

Returns the value of attribute variable_name.



7
8
9
# File 'lib/roast/workflow/each_step.rb', line 7

def variable_name
  @variable_name
end

Instance Method Details

#callObject



15
16
17
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
44
# File 'lib/roast/workflow/each_step.rb', line 15

def call
  # Process the collection expression with appropriate type coercion
  collection = process_iteration_input(@collection_expr, workflow, coerce_to: :iterable)

  unless collection.respond_to?(:each)
    $stderr.puts "Error: Collection '#{@collection_expr}' is not iterable"
    raise ArgumentError, "Collection '#{@collection_expr}' is not iterable"
  end

  results = []
  $stderr.puts "Starting each loop over collection with #{collection.size} items"

  # Iterate over the collection
  collection.each_with_index do |item, index|
    $stderr.puts "Each loop iteration #{index + 1} with #{@variable_name}=#{item.inspect}"

    # Create a context with the current item as a variable
    define_iteration_variable(item)

    # Execute the nested steps
    step_results = execute_nested_steps(@steps, workflow)
    results << step_results

    # Save state after each iteration if the workflow supports it
    save_iteration_state(index, item) if workflow.respond_to?(:session_name) && workflow.session_name
  end

  $stderr.puts "Each loop completed with #{collection.size} iterations"
  results
end