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
Class Method Summary collapse
-
.[](*value) ⇒ Object
Shortcut for Success([...]).
Instance Method Summary collapse
-
#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(*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
#===, #and, #apply, #bind, #deconstruct, #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.
86 87 88 |
# File 'lib/dry/monads/result.rb', line 86 def initialize(value) @value = value end |
Class Method Details
.[](*value) ⇒ Object
Shortcut for Success([...])
@example include Dry::Monads[:result]
def call Success[200, {}, ['ok']] # => Success([200, {}, ['ok']]) end
79 80 81 |
# File 'lib/dry/monads/result.rb', line 79 def self.[](*value) new(value) end |
Instance Method Details
#either(f, _) ⇒ Any
Returns result of applying first function to the internal value.
128 129 130 |
# File 'lib/dry/monads/result.rb', line 128 def either(f, _) f.(success) end |
#failure? ⇒ Boolean
Returns false
98 99 100 |
# File 'lib/dry/monads/result.rb', line 98 def failure? false end |
#flip ⇒ Result::Failure
Transforms to a Failure instance
145 146 147 |
# File 'lib/dry/monads/result.rb', line 145 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.
116 117 118 |
# File 'lib/dry/monads/result.rb', line 116 def fmap(*args, &block) Success.new(bind(*args, &block)) end |
#result(_, f) ⇒ Object
Apply the second function to value.
93 94 95 |
# File 'lib/dry/monads/result.rb', line 93 def result(_, f) f.(@value) end |
#success? ⇒ Boolean
Returns true
103 104 105 |
# File 'lib/dry/monads/result.rb', line 103 def success? true end |
#to_maybe ⇒ Maybe::Some
328 329 330 331 |
# File 'lib/dry/monads/maybe.rb', line 328 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
133 134 135 136 137 138 139 |
# File 'lib/dry/monads/result.rb', line 133 def to_s if Unit.equal?(@value) 'Success()' else "Success(#{@value.inspect})" end end |
#to_validated ⇒ Validated::Valid
Transforms to Validated
289 290 291 |
# File 'lib/dry/monads/validated.rb', line 289 def to_validated Validated::Valid.new(value!) end |