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
Overview
Represents a value of a successful operation.
Constant Summary
Constants inherited from Dry::Monads::Result
Instance Attribute Summary
Attributes inherited from Dry::Monads::Result
Instance Method Summary collapse
-
#failure? ⇒ Boolean
(also: #left?)
Returns false.
-
#flip ⇒ Result::Failure
Transform to a Failure instance.
-
#fmap(*args, &block) ⇒ 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
(also: #right?)
Returns true.
- #to_maybe ⇒ Maybe::Some
- #to_s ⇒ String (also: #inspect)
Methods included from Dry::Monads::RightBiased::Right
#===, #apply, #bind, #or, #or_fmap, #tee, #value!, #value_or
Methods inherited from Dry::Monads::Result
Methods included from Transformer
Constructor Details
#initialize(value) ⇒ Success
Returns a new instance of Success.
54 55 56 |
# File 'lib/dry/monads/result.rb', line 54 def initialize(value) @value = value end |
Instance Method Details
#failure? ⇒ Boolean Also known as: left?
Returns false
66 67 68 |
# File 'lib/dry/monads/result.rb', line 66 def failure? false end |
#flip ⇒ Result::Failure
Transform to a Failure instance
105 106 107 |
# File 'lib/dry/monads/result.rb', line 105 def flip Failure.new(@value) end |
#fmap(*args, &block) ⇒ 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.
86 87 88 |
# File 'lib/dry/monads/result.rb', line 86 def fmap(*args, &block) Success.new(bind(*args, &block)) end |
#result(_, f) ⇒ Object
Apply the second function to value.
61 62 63 |
# File 'lib/dry/monads/result.rb', line 61 def result(_, f) f.(@value) end |
#success? ⇒ Boolean Also known as: right?
Returns true
72 73 74 |
# File 'lib/dry/monads/result.rb', line 72 def success? true end |
#to_maybe ⇒ Maybe::Some
97 98 99 100 |
# File 'lib/dry/monads/result.rb', line 97 def to_maybe Kernel.warn 'Success(nil) transformed to None' if @value.nil? Dry::Monads::Maybe(@value) end |
#to_s ⇒ String Also known as: inspect
91 92 93 |
# File 'lib/dry/monads/result.rb', line 91 def to_s "Success(#{ @value.inspect })" end |