Class: Moku::Sequence

Inherits:
Object
  • Object
show all
Defined in:
lib/moku/sequence.rb

Overview

A utility for processing sequences of callables that return Moku::Status objects.

Class Method Summary collapse

Class Method Details

.do(items, *args) ⇒ Object

Call (via the #call method) each item, passing the given args, until either the collection is exhausted or one of the items returns a failing Status object. If no failures are encountered, this returns a successful Status object. Otherwise, it returns the failed status object.

Examples:

Pass a release to each task's #call method

Sequence.do(tasks, release)

19
20
21
22
23
24
25
# File 'lib/moku/sequence.rb', line 19

def self.do(items, *args)
  items.reduce(Status.success) do |last_result, item|
    break(last_result) unless last_result.success?

    item.call(*args)
  end
end

.for(items) ⇒ Object

Execute the block for each item, passing the item to the block, until either the collection is exhausted or one of the items returns a failing Status object. If no failures are encountered, this returns a successful Status object. Otherwise, it returns a failed status object.

Note that your block must return something that responds to #success?, such as a Status instance.

Examples:

Sequence.for(commands) do |command|
  runner.run(command)
end

39
40
41
42
43
44
45
# File 'lib/moku/sequence.rb', line 39

def self.for(items)
  items.reduce(OpenStruct.new(success?: true)) do |last_result, item|
    break(last_result) unless last_result.success?

    yield item
  end
end