Module: Philiprehberger::Result::Filterable

Included in:
Err, Ok
Defined in:
lib/philiprehberger/result/filterable.rb

Overview

Filtering support for Ok and Err results.

Instance Method Summary collapse

Instance Method Details

#filter(error_fn) {|value| ... } ⇒ Ok, Err

If Ok and predicate fails, convert to Err with the given error. If already Err, pass through unchanged.

Parameters:

  • error_fn (#call)

    callable returning the error value

Yields:

  • (value)

    predicate block receiving the success value

Returns:



13
14
15
16
17
18
# File 'lib/philiprehberger/result/filterable.rb', line 13

def filter(error_fn, &block)
  return self if err?
  return self if block.call(@value)

  Err.new(error_fn.call)
end