Class: Dry::Monads::Result::Success
- Inherits:
-
Dry::Monads::Result
- Object
- Dry::Monads::Result
- Dry::Monads::Result::Success
- Includes:
- Dry::Monads::RightBiased::Right
- Defined in:
- lib/dry/monads/result.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/validated.rb
Overview
Represents a value of a successful operation.
Instance Attribute Summary
Attributes inherited from Dry::Monads::Result
Class Method Summary collapse
-
.[](*value) ⇒ Object
Shortcut for Success().
Instance Method Summary collapse
-
#alt_map(_ = nil) ⇒ Result::Success
Ignores values and returns self, see Failure#alt_map.
-
#either(f, _) ⇒ Any
Returns result of applying first function to the internal value.
-
#failure? ⇒ Boolean
Returns false.
-
#flip ⇒ Result::Failure
Transforms to a Failure instance.
-
#fmap ⇒ Result::Success
Does the same thing as #bind except it also wraps the value in an instance of Result::Success monad.
-
#initialize(value) ⇒ Success
constructor
A new instance of Success.
-
#result(_, f) ⇒ Object
Apply the second function to value.
-
#success? ⇒ Boolean
Returns true.
- #to_maybe ⇒ Maybe
- #to_s ⇒ String (also: #inspect)
-
#to_validated ⇒ Validated::Valid
Transforms to Validated.
Methods included from Dry::Monads::RightBiased::Right
#===, #and, #apply, #bind, #deconstruct, #deconstruct_keys, #discard, #flatten, included, #or, #or_fmap, #tee, #value!, #value_or, #|
Methods inherited from Dry::Monads::Result
#monad, pure, #to_monad, #to_result
Methods included from Transformer
Constructor Details
#initialize(value) ⇒ Success
Returns a new instance of Success.
80 81 82 83 |
# File 'lib/dry/monads/result.rb', line 80 def initialize(value) super() @value = value end |
Class Method Details
Instance Method Details
#alt_map(_ = nil) ⇒ Result::Success
Ignores values and returns self, see Failure#alt_map
147 148 149 |
# File 'lib/dry/monads/result.rb', line 147 def alt_map(_ = nil) self end |
#either(f, _) ⇒ Any
Returns result of applying first function to the internal value.
123 124 125 |
# File 'lib/dry/monads/result.rb', line 123 def either(f, _) f.(success) end |
#failure? ⇒ Boolean
Returns false
93 94 95 |
# File 'lib/dry/monads/result.rb', line 93 def failure? false end |
#flip ⇒ Result::Failure
Transforms to a Failure instance
140 141 142 |
# File 'lib/dry/monads/result.rb', line 140 def flip Failure.new(@value, RightBiased::Left.trace_caller) end |
#fmap ⇒ Result::Success
Does the same thing as #bind except it also wraps the value in an instance of Result::Success monad. This allows for easier chaining of calls.
111 112 113 |
# File 'lib/dry/monads/result.rb', line 111 def fmap(...) Success.new(bind(...)) end |
#result(_, f) ⇒ Object
Apply the second function to value.
88 89 90 |
# File 'lib/dry/monads/result.rb', line 88 def result(_, f) f.(@value) end |
#success? ⇒ Boolean
Returns true
98 99 100 |
# File 'lib/dry/monads/result.rb', line 98 def success? true end |
#to_maybe ⇒ Maybe
388 389 390 391 |
# File 'lib/dry/monads/maybe.rb', line 388 def to_maybe warn "Success(nil) transformed to None" if @value.nil? ::Dry::Monads::Maybe(@value) end |
#to_s ⇒ String Also known as: inspect
128 129 130 131 132 133 134 |
# File 'lib/dry/monads/result.rb', line 128 def to_s if Unit.equal?(@value) "Success()" else "Success(#{@value.inspect})" end end |
#to_validated ⇒ Validated::Valid
Transforms to Validated
292 293 294 |
# File 'lib/dry/monads/validated.rb', line 292 def to_validated Validated::Valid.new(value!) end |