Module: Philiprehberger::Maybe
- Defined in:
- lib/philiprehberger/maybe.rb,
lib/philiprehberger/maybe/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
'0.3.0'
Class Method Summary collapse
-
.all?(*maybes) ⇒ Boolean
Check if all arguments are Some.
-
.first_some(*maybes) ⇒ Some, None
Return the first Some from the arguments, or None if all are None.
-
.from_bool(condition, value = nil) { ... } ⇒ Some, None
Build a Maybe from a boolean gate.
-
.try(*error_classes) { ... } ⇒ Some, None
Execute a block, converting raised exceptions and nil results into None.
-
.wrap(value) ⇒ Some, None
Wrap a value in a Maybe container.
Class Method Details
.all?(*maybes) ⇒ Boolean
Check if all arguments are Some
26 27 28 |
# File 'lib/philiprehberger/maybe.rb', line 26 def self.all?(*maybes) maybes.all?(&:some?) end |
.first_some(*maybes) ⇒ Some, None
Return the first Some from the arguments, or None if all are None
34 35 36 |
# File 'lib/philiprehberger/maybe.rb', line 34 def self.first_some(*maybes) maybes.find(&:some?) || None.instance end |
.from_bool(condition, value = nil) { ... } ⇒ Some, None
Build a Maybe from a boolean gate
If condition is truthy, wraps the given value (or the block’s result if a block is provided). If condition is falsy, returns None and the block is never invoked.
47 48 49 50 51 |
# File 'lib/philiprehberger/maybe.rb', line 47 def self.from_bool(condition, value = nil, &block) return None.instance unless condition wrap(block ? block.call : value) end |
.try(*error_classes) { ... } ⇒ Some, None
Execute a block, converting raised exceptions and nil results into None
58 59 60 61 62 63 |
# File 'lib/philiprehberger/maybe.rb', line 58 def self.try(*error_classes, &block) error_classes = [StandardError] if error_classes.empty? wrap(block.call) rescue *error_classes None.instance end |