Class: Roast::SystemCogs::Map::Input

Inherits:
Cog::Input show all
Defined in:
lib/roast/system_cogs/map.rb

Overview

Input for the ‘map` cog

Provides the collection of items to iterate over and an optional starting index. Each item will be passed to the execution scope along with its index.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInput

Initialize the input with default values

: () -> void



135
136
137
138
139
# File 'lib/roast/system_cogs/map.rb', line 135

def initialize
  super
  @items = []
  @initial_index = 0
end

Instance Attribute Details

#initial_indexObject

The starting index for the first iteration

Defaults to ‘0`. This affects the index value passed to each iteration but does not change which items are processed.

: Integer



130
131
132
# File 'lib/roast/system_cogs/map.rb', line 130

def initial_index
  @initial_index
end

#itemsObject

The collection of items to iterate over

This can be any enumerable collection. Each item will be passed as the value to the execution scope. Required.

: Array



122
123
124
# File 'lib/roast/system_cogs/map.rb', line 122

def items
  @items
end

Instance Method Details

#coerce(input_return_value) ⇒ Object

Coerce the input from the return value of the input block

Sets the items from the input block’s return value if not already set directly. Converts enumerable objects to arrays and wraps non-enumerable single values in an array.

: (Array) -> void



155
156
157
158
159
160
# File 'lib/roast/system_cogs/map.rb', line 155

def coerce(input_return_value)
  super
  return if @items.present?

  @items = input_return_value.respond_to?(:each) ? input_return_value.to_a : Array.wrap(input_return_value)
end

#validate!Object

Validate that required input values are present

: () -> void



144
145
146
147
# File 'lib/roast/system_cogs/map.rb', line 144

def validate!
  raise Cog::Input::InvalidInputError, "'items' is required" if items.nil?
  raise Cog::Input::InvalidInputError if items.empty? && !coerce_ran?
end