Class: Dry::Monads::Try::Value
- Inherits:
-
Dry::Monads::Try
- Object
- Dry::Monads::Try
- Dry::Monads::Try::Value
- Includes:
- RightBiased::Right
- Defined in:
- lib/dry/monads/try.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/result.rb
Overview
Represents a result of a successful execution.
Constant Summary
Constants inherited from Dry::Monads::Try
Instance Attribute Summary collapse
-
#catchable ⇒ Array<Exception>
readonly
List of exceptions to rescue.
Attributes inherited from Dry::Monads::Try
Instance Method Summary collapse
-
#bind ⇒ Object, Try::Error
Calls the passed in Proc object with value stored in self and returns the result.
-
#fmap ⇒ Try::Value, Try::Error
Does the same thing as #bind except it also wraps the value in an instance of a Try monad.
-
#initialize(exceptions, value) ⇒ Value
constructor
A new instance of Value.
-
#recover(*_errors) ⇒ Try::Value
Ignores values and returns self, see Error#recover.
- #to_maybe ⇒ Maybe
- #to_result ⇒ Result::Success
- #to_s ⇒ String (also: #inspect)
Methods included from RightBiased::Right
#===, #and, #apply, #deconstruct, #deconstruct_keys, #discard, #flatten, included, #or, #or_fmap, #tee, #value!, #value_or, #|
Methods inherited from Dry::Monads::Try
[], #error?, pure, run, #to_monad, #value?
Constructor Details
#initialize(exceptions, value) ⇒ Value
Returns a new instance of Value.
106 107 108 109 110 111 |
# File 'lib/dry/monads/try.rb', line 106 def initialize(exceptions, value) super() @catchable = exceptions @value = value end |
Instance Attribute Details
#catchable ⇒ Array<Exception> (readonly)
Returns List of exceptions to rescue.
102 103 104 |
# File 'lib/dry/monads/try.rb', line 102 def catchable @catchable end |
Instance Method Details
#bind ⇒ Object, Try::Error
Calls the passed in Proc object with value stored in self and returns the result.
If proc is nil, it expects a block to be given and will yield to it.
132 133 134 135 136 |
# File 'lib/dry/monads/try.rb', line 132 def bind(...) super rescue *catchable => e Error.new(e) end |
#fmap ⇒ Try::Value, Try::Error
Does the same thing as #bind except it also wraps the value in an instance of a Try monad. This allows for easier chaining of calls.
150 151 152 153 154 |
# File 'lib/dry/monads/try.rb', line 150 def fmap(...) Value.new(catchable, bind_call(...)) rescue *catchable => e Error.new(e) end |
#recover(*_errors) ⇒ Try::Value
Ignores values and returns self, see Error#recover
171 172 173 |
# File 'lib/dry/monads/try.rb', line 171 def recover(*_errors) self end |
#to_maybe ⇒ Maybe
418 419 420 |
# File 'lib/dry/monads/maybe.rb', line 418 def to_maybe Dry::Monads::Maybe(@value) end |
#to_result ⇒ Result::Success
445 446 447 |
# File 'lib/dry/monads/result.rb', line 445 def to_result Dry::Monads::Result::Success.new(@value) end |
#to_s ⇒ String Also known as: inspect
157 158 159 160 161 162 163 |
# File 'lib/dry/monads/try.rb', line 157 def to_s if Unit.equal?(@value) "Try::Value()" else "Try::Value(#{@value.inspect})" end end |