Class: Cuprum::MapCommand
Overview
Calls the command implementation with each item in the given enumerable.
A regular Command is called with a set of parameters, calls the command implementation once with those parameters, and returns the Result. In contrast, a MapCommand is called with an Enumerable object, such as an Array, a Hash, or an Enumerator (e.g. by calling #each without a block). The MapCommand implementation is then called with each item in the Enumerable - for example, if called with an Array with three items, the MapCommand implementation would be called three times, once with each item. Finally, the Results returned by calling the implementation with each item are aggregated together into a Cuprum::ResultList. A ResultList behaves like a Result, and provides the standard methods (such as #status, #error, and #value), but also includes a reference to the #results used to create the ResultList, and their respective #errors and #values as Arrays.
Like a standard Command, a MapCommand can be defined either by passing a block to the constructor, or by defining a subclass of MapCommand and implementing the #process method. If the given block or the #process method accepts more than one argument, the enumerable item is destructured using the splat operator (*); this enables using a MapCommand to map over the keys and values of a Hash. This is the same behavior seen when passing a block with multiple arguments to a native #each method.
If a MapCommand is initialized with the :allow_partial keyword, the ResultList will be passing as long as there is at least one passing Result (or if the MapCommand is called with an empty Enumerable). See ResultList#allow_partial? for details.
Instance Method Summary collapse
-
#allow_partial? ⇒ true, false
If true, allows for some failing results as long as there is at least one passing result.
-
#call(enumerable) ⇒ Cuprum::ResultList
Calls the command implementation for each item in the given Enumerable.
-
#initialize(allow_partial: false, &implementation) ⇒ MapCommand
constructor
A new instance of MapCommand.
Methods inherited from Command
Methods included from Steps
Methods included from Currying
Methods included from Processing
Constructor Details
#initialize(allow_partial: false) ⇒ MapCommand #initialize(allow_partial: false) {|item| ... } ⇒ MapCommand #initialize(allow_partial: false) {|key, value| ... } ⇒ MapCommand
Returns a new instance of MapCommand.
191 192 193 194 195 |
# File 'lib/cuprum/map_command.rb', line 191 def initialize(allow_partial: false, &implementation) super(&implementation) @allow_partial = allow_partial end |
Instance Method Details
#allow_partial? ⇒ true, false
Returns if true, allows for some failing results as long as there is at least one passing result. Defaults to false.
199 200 201 |
# File 'lib/cuprum/map_command.rb', line 199 def allow_partial? @allow_partial end |
#call(enumerable) ⇒ Cuprum::ResultList
Calls the command implementation for each item in the given Enumerable.
209 210 211 212 213 |
# File 'lib/cuprum/map_command.rb', line 209 def call(enumerable) build_result_list( enumerable.map { |item| splat_items? ? super(*item) : super(item) } ) end |