Module: Roast::SystemCogs::Call::InputContext

Included in:
CogInputContext
Defined in:
lib/roast/system_cogs/call.rb

Overview

@requires_ancestor: Roast::CogInputContext

Instance Method Summary collapse

Instance Method Details

#from(call_cog_output, &block) ⇒ Object

Retrieve the output from a ‘call` cog’s execution scope

Extracts the final output from the execution scope that was invoked by the call cog. When called without a block, returns the final output directly. When called with a block, executes the block in the context of the called scope’s input context, receiving the final output as an argument.

This allows you to access the results of a called scope and optionally transform them and/or access other outputs from within that scope.

#### Usage “‘ruby # Get the final output directly result = from(call!(:my_call))

# Transform the output with a block transformed = from(call!(:my_call)) { |output| output.upcase }

# Access other cog outputs from within the called scope inner_result = from(call!(:my_call)) { inner_cog!(:some_step) } “‘

#### See Also

  • ‘Roast::SystemCogs::Call::Output` - the output type from call cogs



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/roast/system_cogs/call.rb', line 147

def from(call_cog_output, &block)
  em = call_cog_output.instance_variable_get(:@execution_manager)
  raise CogInputContext::ContextNotFoundError if em.nil?

  final_output = em.final_output
  scope_value = em.instance_variable_get(:@scope_value).deep_dup
  scope_index = em.instance_variable_get(:@scope_index)
  return em.cog_input_context.instance_exec(final_output, scope_value, scope_index, &block) if block_given?

  final_output
end