Class: Zx::Maybe
- Inherits:
-
Object
show all
- Defined in:
- lib/zx/maybe.rb
Defined Under Namespace
Modules: ClassMethods
Classes: None, Some
Constant Summary
collapse
- IsBlank =
->(value) { value.nil? || value.to_s.strip&.empty? || !value }
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
5
6
7
|
# File 'lib/zx/maybe.rb', line 5
def value
@value
end
|
Class Method Details
.[] ⇒ Object
13
14
15
|
# File 'lib/zx/maybe.rb', line 13
def self.[](...)
of(...)
end
|
.of ⇒ Object
9
10
11
|
# File 'lib/zx/maybe.rb', line 9
def self.of(...)
new.of(...)
end
|
Instance Method Details
#>(_other) ⇒ Object
54
55
56
|
# File 'lib/zx/maybe.rb', line 54
def >(_other)
self
end
|
#>>(other) ⇒ Object
Also known as:
|
45
46
47
|
# File 'lib/zx/maybe.rb', line 45
def >>(other)
self > other
end
|
#apply! ⇒ Object
77
78
79
|
# File 'lib/zx/maybe.rb', line 77
def apply!(...)
apply(...).unwrap
end
|
#dig ⇒ Object
81
82
83
|
# File 'lib/zx/maybe.rb', line 81
def dig(...)
Maybe[@value&.dig(...)]
end
|
#dig! ⇒ Object
85
86
87
|
# File 'lib/zx/maybe.rb', line 85
def dig!(...)
dig(...).unwrap
end
|
#fmap(_) ⇒ Object
50
51
52
|
# File 'lib/zx/maybe.rb', line 50
def fmap(_)
self
end
|
#map(arg = nil, &block) ⇒ Object
Also known as:
apply
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/zx/maybe.rb', line 58
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.message)
end
|
#map!(&block) ⇒ Object
71
72
73
74
75
|
# File 'lib/zx/maybe.rb', line 71
def map!(&block)
@value = block.call(@value)
Maybe[@value]
end
|
#match(some:, none:) ⇒ Object
89
90
91
92
93
94
|
# File 'lib/zx/maybe.rb', line 89
def match(some:, none:)
case self
in Some then some.call(@value)
else none.call
end
end
|
#none? ⇒ Boolean
33
34
35
|
# File 'lib/zx/maybe.rb', line 33
def none?
type == :none
end
|
#of(value) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/zx/maybe.rb', line 17
def of(value)
return None.new if IsBlank[value]
Some.new(value)
rescue StandardError
None.new
end
|
#on(ontype, &block) ⇒ Object
112
113
114
115
116
117
|
# File 'lib/zx/maybe.rb', line 112
def on(ontype, &block)
case ontype.to_sym
when :success then on_success(&block)
when :failure then on_failure(&block)
end
end
|
#on_failure(&block) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/zx/maybe.rb', line 104
def on_failure(&block)
return self if some?
block.call(None[@value])
self
end
|
#on_success(&block) ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/zx/maybe.rb', line 96
def on_success(&block)
return self if none?
block.call(Some[@value])
self
end
|
#or(value) ⇒ Object
41
42
43
|
# File 'lib/zx/maybe.rb', line 41
def or(value)
IsBlank[@value] ? value : @value
end
|
#some? ⇒ Boolean
29
30
31
|
# File 'lib/zx/maybe.rb', line 29
def some?
type == :some
end
|
#type ⇒ Object
25
26
27
|
# File 'lib/zx/maybe.rb', line 25
def type
to_s.downcase.to_sym
end
|
#unwrap ⇒ Object
37
38
39
|
# File 'lib/zx/maybe.rb', line 37
def unwrap
@value
end
|