Class: Maybe
- Inherits:
-
Object
- Object
- Maybe
- Defined in:
- lib/maybe.rb
Defined Under Namespace
Constant Summary collapse
- IsBlank =
->(value) { value.nil? || value.to_s.strip&.empty? || !value }
Class Method Summary collapse
Instance Method Summary collapse
- #>(_other) ⇒ Object
- #>>(other) ⇒ Object
- #apply! ⇒ Object
- #dig ⇒ Object
- #dig! ⇒ Object
- #fmap(_) ⇒ Object
- #map(arg = nil, &block) ⇒ Object (also: #apply)
- #map!(&block) ⇒ Object
- #match(some:, none:) ⇒ Object
- #none? ⇒ Boolean
- #of(value) ⇒ Object
- #on_failure(&block) ⇒ Object
- #on_success(&block) ⇒ Object
- #or(value) ⇒ Object
- #some? ⇒ Boolean
- #type ⇒ Object
- #unwrap ⇒ Object
Class Method Details
.[] ⇒ Object
10 11 12 |
# File 'lib/maybe.rb', line 10 def self.[](...) of(...) end |
.of ⇒ Object
6 7 8 |
# File 'lib/maybe.rb', line 6 def self.of(...) new.of(...) end |
Instance Method Details
#>(_other) ⇒ Object
50 51 52 |
# File 'lib/maybe.rb', line 50 def >(_other) self end |
#>>(other) ⇒ Object
42 43 44 |
# File 'lib/maybe.rb', line 42 def >>(other) self > other end |
#apply! ⇒ Object
73 74 75 |
# File 'lib/maybe.rb', line 73 def apply!(...) apply(...).unwrap end |
#dig ⇒ Object
77 78 79 |
# File 'lib/maybe.rb', line 77 def dig(...) Maybe[@value&.dig(...)] end |
#dig! ⇒ Object
81 82 83 |
# File 'lib/maybe.rb', line 81 def dig!(...) dig(...).unwrap end |
#fmap(_) ⇒ Object
46 47 48 |
# File 'lib/maybe.rb', line 46 def fmap(_) self end |
#map(arg = nil, &block) ⇒ Object Also known as: apply
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/maybe.rb', line 54 def map(arg = nil, &block) return Maybe[block.call(@value)] if block_given? return Maybe[arg.arity > 1 ? arg.curry.call(@value) : arg.call(@value)] if arg.respond_to?(:call) case arg in None then self in Symbol | String then dig(arg) end rescue StandardError => e None.new(e.) end |
#map!(&block) ⇒ Object
67 68 69 70 71 |
# File 'lib/maybe.rb', line 67 def map!(&block) @value = block.call(@value) Maybe[@value] end |
#match(some:, none:) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/maybe.rb', line 85 def match(some:, none:) case self in Some then some.call(@value) else none.call end end |
#none? ⇒ Boolean
30 31 32 |
# File 'lib/maybe.rb', line 30 def none? type == :none end |
#of(value) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/maybe.rb', line 14 def of(value) return None.new if IsBlank[value] Some.new(value) rescue StandardError None.new end |
#on_failure(&block) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/maybe.rb', line 100 def on_failure(&block) return self if some? block.call(None[@value]) self end |
#on_success(&block) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/maybe.rb', line 92 def on_success(&block) return self if none? block.call(Some[@value]) self end |
#or(value) ⇒ Object
38 39 40 |
# File 'lib/maybe.rb', line 38 def or(value) IsBlank[@value] ? value : @value end |
#some? ⇒ Boolean
26 27 28 |
# File 'lib/maybe.rb', line 26 def some? type == :some end |
#type ⇒ Object
22 23 24 |
# File 'lib/maybe.rb', line 22 def type to_s.downcase.to_sym end |
#unwrap ⇒ Object
34 35 36 |
# File 'lib/maybe.rb', line 34 def unwrap @value end |