Module: Philiprehberger::Result
- Defined in:
- lib/philiprehberger/result.rb,
lib/philiprehberger/result/ok.rb,
lib/philiprehberger/result/err.rb,
lib/philiprehberger/result/version.rb,
lib/philiprehberger/result/tappable.rb,
lib/philiprehberger/result/filterable.rb
Defined Under Namespace
Modules: Filterable, Tappable Classes: Err, Error, Ok, UnwrapError
Constant Summary collapse
- VERSION =
'0.6.0'
Class Method Summary collapse
-
.all(results) ⇒ Ok, Err
Combine an array of results into a single result.
-
.any(results) ⇒ Ok, Err
Return the first Ok result, or Err with all errors if all fail.
-
.err(error) ⇒ Err
Create a failure result.
-
.flatten(result) ⇒ Ok, Err
Flatten a nested Result.
-
.ok(value) ⇒ Ok
Create a success result.
-
.partition(results) ⇒ Array(Array, Array)
Split an array of results into success values and error values.
-
.try(*exceptions) { ... } ⇒ Ok, Err
Wrap a block, capturing exceptions as Err.
Class Method Details
.all(results) ⇒ Ok, Err
Combine an array of results into a single result.
44 45 46 47 48 49 50 51 52 |
# File 'lib/philiprehberger/result.rb', line 44 def self.all(results) values = [] results.each do |result| return result if result.err? values << result.value end Ok.new(values) end |
.any(results) ⇒ Ok, Err
Return the first Ok result, or Err with all errors if all fail
69 70 71 72 |
# File 'lib/philiprehberger/result.rb', line 69 def self.any(results) results.each { |r| return r if r.ok? } err(results.select(&:err?).map { |r| r.instance_variable_get(:@error) }) end |
.err(error) ⇒ Err
Create a failure result.
26 |
# File 'lib/philiprehberger/result.rb', line 26 def self.err(error) = Err.new(error) |
.flatten(result) ⇒ Ok, Err
Flatten a nested Result. Ok(Ok(v)) becomes Ok(v), Ok(Err(e)) becomes Err(e).
58 59 60 61 62 63 |
# File 'lib/philiprehberger/result.rb', line 58 def self.flatten(result) return result if result.err? return result.value if result.value.is_a?(Ok) || result.value.is_a?(Err) result end |
.ok(value) ⇒ Ok
Create a success result.
20 |
# File 'lib/philiprehberger/result.rb', line 20 def self.ok(value) = Ok.new(value) |
.partition(results) ⇒ Array(Array, Array)
Split an array of results into success values and error values.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/philiprehberger/result.rb', line 78 def self.partition(results) values = [] errors = [] results.each do |result| if result.ok? values << result.value else errors << result.instance_variable_get(:@error) end end [values, errors] end |