Class: Roast::Cogs::Ruby::Output

Inherits:
Roast::Cog::Output show all
Defined in:
lib/roast/cogs/ruby.rb

Overview

Output from running the ruby cog

Contains the value that was provided to the input block, passed through unchanged. This allows Ruby values to be used directly in workflow steps.

The output provides convenient dynamic method dispatch with the following priority:

  1. If the value object responds to a method, it delegates to that method

  2. If the value is a Hash, methods correspond to hash keys

  3. Hash values that are Procs can be called directly as methods

Additional conveniences:

  • Use ‘[]` for direct hash key access when the value is a Hash

  • Use ‘call()` to invoke the value if it’s a Proc, or to call a Proc stored in a Hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Output

: (untyped) -> void



69
70
71
72
# File 'lib/roast/cogs/ruby.rb', line 69

def initialize(value)
  super()
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &blk) ⇒ Object

Handle dynamic method calls with intelligent dispatch

This method implements a multi-level dispatch strategy:

  1. **Value delegation**: If the value object responds to the method, delegates directly to it. This allows calling methods like ‘lines`, `length`, `upcase` on String values, or any method on the underlying object.

  2. **Hash key access**: If the value is a Hash and contains the method name as a key, returns the value at that key. If the value is a Proc, calls it with the provided arguments.

  3. Fallback: If neither condition is met, calls ‘super` to trigger standard Ruby behavior.

#### See Also

  • ‘respond_to_missing?`

  • ‘[]`

  • ‘call`

: (Symbol, *untyped, **untyped) ?{ (*untyped, **untyped) -> untyped } -> untyped



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/roast/cogs/ruby.rb', line 135

def method_missing(name, *args, **kwargs, &blk)
  return value.public_send(name, *args, **kwargs, &blk) if value.respond_to?(name, false)
  return super unless value.is_a?(Hash) && value.key?(name)

  if value[name].is_a?(Proc)
    proc = value[name] #: as untyped
    proc.call(*args, **kwargs, &blk)
  else
    value[name]
  end
end

Instance Attribute Details

#valueObject (readonly)

The value passed through from the input

: untyped



66
67
68
# File 'lib/roast/cogs/ruby.rb', line 66

def value
  @value
end

Instance Method Details

#[](key) ⇒ Object

Access a hash key directly when the value is a Hash

Provides direct bracket notation access to hash keys without going through method dispatch. This is useful when you need explicit hash key access.

#### See Also

  • ‘call`

  • ‘method_missing`

: (Symbol) -> untyped



84
85
86
# File 'lib/roast/cogs/ruby.rb', line 84

def [](key)
  value[key]
end

#call(*args, **kwargs, &blk) ⇒ Object

Call the value as a Proc, or call a Proc stored in a Hash

This method provides two calling patterns:

  • If the value is a Proc, calls it directly with the provided arguments

  • If the value is a Hash, expects the first argument to be a Symbol key, retrieves the Proc at that key, and calls it with the remaining arguments

Raises ‘ArgumentError` if called on a Hash without a Symbol key. Raises `NoMethodError` if the Hash key doesn’t contain a Proc.

#### See Also

  • ‘[]`

  • ‘method_missing`

: (*untyped, **untyped) ?{ (*untyped, **untyped) -> untyped } -> untyped

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/roast/cogs/ruby.rb', line 103

def call(*args, **kwargs, &blk)
  return value.call(*args, **kwargs, &blk) if value.is_a?(Proc)

  key = args.first
  raise ArgumentError unless key.is_a?(Symbol)

  proc = value[key]
  raise NoMethodError, key unless proc.is_a?(Proc)

  proc = proc #: as untyped
  proc.call(*args, **kwargs, &blk)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Check if a dynamic method should respond

Returns ‘true` if any of the following conditions are met:

  1. The value object responds to the method

  2. The value is a Hash and contains the method name as a key

  3. The parent class would respond to the method

#### See Also

  • ‘method_missing`

: (Symbol | String, ?bool) -> bool

Returns:

  • (Boolean)


158
159
160
# File 'lib/roast/cogs/ruby.rb', line 158

def respond_to_missing?(name, include_private = false)
  value.respond_to?(name, false) || value.is_a?(Hash) && value.key?(name) || super
end