Class: Kind::Maybe::Some
- Inherits:
-
Kind::Monad
- Object
- Kind::Monad
- Kind::Maybe::Some
- Defined in:
- lib/kind/maybe/some.rb
Constant Summary
Constants included from Kind::Monad
Class Method Summary collapse
Instance Method Summary collapse
- #check(method_name = UNDEFINED, &fn) ⇒ Object (also: #accept)
- #dig(*keys) ⇒ Object
- #inspect ⇒ Object
- #map(method_name = UNDEFINED, &fn) ⇒ Object (also: #then, #and_then)
- #map!(method_name = UNDEFINED, &fn) ⇒ Object (also: #then!, #and_then!)
- #none? ⇒ Boolean
- #presence ⇒ Object
- #reject(method_name = UNDEFINED, &fn) ⇒ Object
- #try(method_name = UNDEFINED, *args, &block) ⇒ Object
- #try!(method_name = UNDEFINED, *args, &block) ⇒ Object
- #value_or(_default = UNDEFINED, &block) ⇒ Object
Class Method Details
.[](value) ⇒ Object
13 14 15 16 17 |
# File 'lib/kind/maybe/some.rb', line 13 def self.[](value) return new(value) if !KIND.nil_or_undefined?(value) raise ArgumentError, VALUE_CANT_BE_NONE end |
Instance Method Details
#check(method_name = UNDEFINED, &fn) ⇒ Object Also known as: accept
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kind/maybe/some.rb', line 49 def check(method_name = UNDEFINED, &fn) result = if UNDEFINED != method_name if method_name.kind_of?(::Symbol) @value.respond_to?(method_name) ? @value.public_send(method_name) : NONE_INSTANCE else method_name === @value end else fn.call(@value) end !result || KIND.nil_or_undefined?(result) ? NONE_INSTANCE : self end |
#dig(*keys) ⇒ Object
97 98 99 |
# File 'lib/kind/maybe/some.rb', line 97 def dig(*keys) resolve(Dig.call!(value, keys)) end |
#inspect ⇒ Object
105 106 107 |
# File 'lib/kind/maybe/some.rb', line 105 def inspect '#<%s value=%s>' % ['Kind::Some', value.inspect] end |
#map(method_name = UNDEFINED, &fn) ⇒ Object Also known as: then, and_then
25 26 27 28 29 |
# File 'lib/kind/maybe/some.rb', line 25 def map(method_name = UNDEFINED, &fn) map!(method_name, &fn) rescue StandardError => exception None.new(exception) end |
#map!(method_name = UNDEFINED, &fn) ⇒ Object Also known as: then!, and_then!
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kind/maybe/some.rb', line 34 def map!(method_name = UNDEFINED, &fn) result = if UNDEFINED != method_name return NONE_INSTANCE unless @value.respond_to?(KindSymbol[method_name]) @value.public_send(method_name) else fn.call(@value) end resolve(result) end |
#presence ⇒ Object
101 102 103 |
# File 'lib/kind/maybe/some.rb', line 101 def presence resolve(Presence.(value)) end |
#reject(method_name = UNDEFINED, &fn) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kind/maybe/some.rb', line 65 def reject(method_name = UNDEFINED, &fn) result = if UNDEFINED != method_name if method_name.kind_of?(::Symbol) @value.respond_to?(method_name) ? @value.public_send(method_name) : NONE_INSTANCE else method_name === @value end else fn.call(@value) end result || KIND.nil_or_undefined?(result) ? NONE_INSTANCE : self end |
#try(method_name = UNDEFINED, *args, &block) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/kind/maybe/some.rb', line 87 def try(method_name = UNDEFINED, *args, &block) return __try_block__(block, args) if block return __try_method__(method_name, args) if value.respond_to?(method_name) NONE_INSTANCE rescue TypeError NONE_INSTANCE end |
#try!(method_name = UNDEFINED, *args, &block) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/kind/maybe/some.rb', line 79 def try!(method_name = UNDEFINED, *args, &block) return __try_block__(block, args) if block return __try_method__(method_name, args) if UNDEFINED != method_name raise ArgumentError, 'method name or a block must be present' end |
#value_or(_default = UNDEFINED, &block) ⇒ Object
19 20 21 |
# File 'lib/kind/maybe/some.rb', line 19 def value_or(_default = UNDEFINED, &block) @value end |