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/either.rb,
lib/dry/monads/validated.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
Returns false.
-
#flip ⇒ Result::Failure
Transforms 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
Returns true.
- #to_maybe ⇒ Maybe::Some
- #to_s ⇒ String (also: #inspect)
-
#to_validated ⇒ Validated::Valid
Transforms to Validated.
Methods included from Dry::Monads::RightBiased::Right
#===, #apply, #bind, #discard, 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.
72 73 74 |
# File 'lib/dry/monads/result.rb', line 72 def initialize(value) @value = value end |
Instance Method Details
#failure? ⇒ Boolean
Returns false
84 85 86 |
# File 'lib/dry/monads/result.rb', line 84 def failure? false end |
#flip ⇒ Result::Failure
Transforms to a Failure instance
115 116 117 |
# File 'lib/dry/monads/result.rb', line 115 def flip Failure.new(@value, RightBiased::Left.trace_caller) 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.
102 103 104 |
# File 'lib/dry/monads/result.rb', line 102 def fmap(*args, &block) Success.new(bind(*args, &block)) end |
#result(_, f) ⇒ Object
Apply the second function to value.
79 80 81 |
# File 'lib/dry/monads/result.rb', line 79 def result(_, f) f.(@value) end |
#success? ⇒ Boolean
Returns true
89 90 91 |
# File 'lib/dry/monads/result.rb', line 89 def success? true end |
#to_maybe ⇒ Maybe::Some
237 238 239 240 |
# File 'lib/dry/monads/maybe.rb', line 237 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
107 108 109 |
# File 'lib/dry/monads/result.rb', line 107 def to_s "Success(#{ @value.inspect })" end |
#to_validated ⇒ Validated::Valid
Transforms to Validated
285 286 287 |
# File 'lib/dry/monads/validated.rb', line 285 def to_validated Validated::Valid.new(value!) end |