Module: Resulting::Runner

Defined in:
lib/resulting/runner.rb

Class Method Summary collapse

Class Method Details

.run_all(result, method:, failure_case: -> { false }, wrapper: ->(&blk) { blk.call }) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/resulting/runner.rb', line 3

def self.run_all(result, method:, failure_case: -> { false }, wrapper: ->(&blk) { blk.call })
  Resulting::Handler.handle(result, wrapper: wrapper) do
    new_result = result.values.reduce(true) do |success, v|
      v.send(method) ? success : false
    end

    if block_given?
      block_result = yield
      new_result &&= block_result
    end

    new_result ? true : failure_case.call
  end
end

.run_until_failure(result, method:, failure_case: -> { false }, wrapper: ->(&blk) { blk.call }) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/resulting/runner.rb', line 18

def self.run_until_failure(result, method:, failure_case: -> { false }, wrapper: ->(&blk) { blk.call })
  Resulting::Handler.handle(result, wrapper: wrapper) do
    result = result.values.all?(&method)

    result &&= yield if block_given?

    result ? true : failure_case.call
  end
end