Class: Dry::Monads::Result::Failure
- Inherits:
-
Dry::Monads::Result
- Object
- Dry::Monads::Result
- Dry::Monads::Result::Failure
- Includes:
- Dry::Monads::RightBiased::Left
- 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 failed operation.
Constant Summary
Constants inherited from Dry::Monads::Result
Instance Attribute Summary collapse
-
#trace ⇒ String
readonly
Line where the value was constructed.
Attributes inherited from Dry::Monads::Result
Class Method Summary collapse
-
.to_proc ⇒ Proc
Returns a constructor proc.
Instance Method Summary collapse
- #===(other) ⇒ Boolean
- #failure ⇒ Object
-
#failure? ⇒ Boolean
Returns true.
-
#flip ⇒ Result::Success
Transform to a Success instance.
-
#initialize(value, trace = RightBiased::Left.trace_caller) ⇒ Failure
constructor
A new instance of Failure.
-
#or(*args) ⇒ Object
If a block is given passes internal value to it and returns the result, otherwise simply returns the first argument.
-
#or_fmap(*args, &block) ⇒ Result::Success
A lifted version of
#or
. -
#result(f, _) ⇒ Object
Apply the first function to value.
-
#success? ⇒ Boolean
Returns false.
- #to_maybe ⇒ Maybe::None
- #to_s ⇒ String (also: #inspect)
-
#to_validated ⇒ Validated::Valid
Transforms to Validated.
- #value_or(val = nil) ⇒ Object
Methods included from Dry::Monads::RightBiased::Left
#apply, #bind, #discard, #fmap, #tee, trace_caller, #value!
Methods inherited from Dry::Monads::Result
#monad, pure, #to_monad, #to_result
Methods included from Transformer
Constructor Details
#initialize(value, trace = RightBiased::Left.trace_caller) ⇒ Failure
Returns a new instance of Failure.
142 143 144 145 |
# File 'lib/dry/monads/result.rb', line 142 def initialize(value, trace = RightBiased::Left.trace_caller) @value = value @trace = trace end |
Instance Attribute Details
#trace ⇒ String (readonly)
Line where the value was constructed
138 139 140 |
# File 'lib/dry/monads/result.rb', line 138 def trace @trace end |
Class Method Details
.to_proc ⇒ Proc
Returns a constructor proc
130 131 132 |
# File 'lib/dry/monads/result.rb', line 130 def self.to_proc @to_proc ||= method(:new).to_proc end |
Instance Method Details
#===(other) ⇒ Boolean
223 224 225 |
# File 'lib/dry/monads/result.rb', line 223 def ===(other) Failure === other && failure === other.failure end |
#failure ⇒ Object
148 149 150 |
# File 'lib/dry/monads/result.rb', line 148 def failure @value end |
#failure? ⇒ Boolean
Returns true
160 161 162 |
# File 'lib/dry/monads/result.rb', line 160 def failure? true end |
#flip ⇒ Result::Success
Transform to a Success instance
208 209 210 |
# File 'lib/dry/monads/result.rb', line 208 def flip Success.new(@value) end |
#or(*args) ⇒ Object
If a block is given passes internal value to it and returns the result, otherwise simply returns the first argument.
179 180 181 182 183 184 185 |
# File 'lib/dry/monads/result.rb', line 179 def or(*args) if block_given? yield(@value, *args) else args[0] end end |
#or_fmap(*args, &block) ⇒ Result::Success
A lifted version of #or
. Wraps the passed value or the block result with Result::Success.
195 196 197 |
# File 'lib/dry/monads/result.rb', line 195 def or_fmap(*args, &block) Success.new(self.or(*args, &block)) end |
#result(f, _) ⇒ Object
Apply the first function to value.
155 156 157 |
# File 'lib/dry/monads/result.rb', line 155 def result(f, _) f.(@value) end |
#success? ⇒ Boolean
Returns false
165 166 167 |
# File 'lib/dry/monads/result.rb', line 165 def success? false end |
#to_maybe ⇒ Maybe::None
245 246 247 |
# File 'lib/dry/monads/maybe.rb', line 245 def to_maybe Maybe::None.new(trace) end |
#to_s ⇒ String Also known as: inspect
200 201 202 |
# File 'lib/dry/monads/result.rb', line 200 def to_s "Failure(#{ @value.inspect })" end |
#to_validated ⇒ Validated::Valid
Transforms to Validated
294 295 296 |
# File 'lib/dry/monads/validated.rb', line 294 def to_validated Validated::Invalid.new(failure, trace) end |
#value_or(val = nil) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/dry/monads/result.rb', line 213 def value_or(val = nil) if block_given? yield(@value) else val end end |