Class: Stupidedi::Either::Success
- Inherits:
-
Stupidedi::Either
- Object
- Stupidedi::Either
- Stupidedi::Either::Success
- Defined in:
- lib/stupidedi/either.rb
Direct Known Subclasses
Filtering the Value collapse
- #reject(reason = "reject") {|value| ... } ⇒ Either
- #select(reason = "select") {|value| ... } ⇒ Either
Transforming the Value collapse
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #copy(changes = {}) ⇒ Object
-
#defined? ⇒ Boolean
True.
- #fetch(default = nil) ⇒ Object
-
#initialize(value) ⇒ Success
constructor
A new instance of Success.
- #inspect ⇒ String
- #pretty_print(q) ⇒ void
- #tap(&block) ⇒ Object
Methods inherited from Stupidedi::Either
Constructor Details
#initialize(value) ⇒ Success
Returns a new instance of Success.
42 43 44 |
# File 'lib/stupidedi/either.rb', line 42 def initialize(value) @value = value end |
Instance Method Details
#==(other) ⇒ Boolean
129 130 131 |
# File 'lib/stupidedi/either.rb', line 129 def ==(other) other.is_a?(Either) and other.select{|x| x == @value }.defined? end |
#copy(changes = {}) ⇒ Object
46 47 48 49 |
# File 'lib/stupidedi/either.rb', line 46 def copy(changes = {}) Success.new \ changes.fetch(:value, @value) end |
#defined? ⇒ Boolean
Returns true.
52 53 54 |
# File 'lib/stupidedi/either.rb', line 52 def defined? true end |
#fetch(default = nil) ⇒ Object
56 57 58 |
# File 'lib/stupidedi/either.rb', line 56 def fetch(default = nil) @value end |
#flatmap {|value| ... } ⇒ Either
101 102 103 104 105 106 107 108 109 |
# File 'lib/stupidedi/either.rb', line 101 def flatmap(&block) result = deconstruct(block) if result.is_a?(Either) result else raise TypeError, "block did not return an instance of Either" end end |
#inspect ⇒ String
143 144 145 |
# File 'lib/stupidedi/either.rb', line 143 def inspect "Either.success(#{@value.inspect})" end |
#map {|value| ... } ⇒ Success
94 95 96 |
# File 'lib/stupidedi/either.rb', line 94 def map(&block) copy(:value => deconstruct(block)) end |
#pretty_print(q) ⇒ void
This method returns an undefined value.
134 135 136 137 138 139 140 |
# File 'lib/stupidedi/either.rb', line 134 def pretty_print(q) q.text("Either.success") q.group(2, "(", ")") do q.breakable "" q.pp @value end end |
#reject(reason = "reject") {|value| ... } ⇒ Either
77 78 79 80 81 82 83 |
# File 'lib/stupidedi/either.rb', line 77 def reject(reason = "reject", &block) if deconstruct(block) failure(reason) else self end end |
#select(reason = "select") {|value| ... } ⇒ Either
66 67 68 69 70 71 72 |
# File 'lib/stupidedi/either.rb', line 66 def select(reason = "select", &block) if deconstruct(block) self else failure(reason) end end |
#tap(&block) ⇒ Object
124 125 126 |
# File 'lib/stupidedi/either.rb', line 124 def tap(&block) deconstruct(block); self end |