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
-
.[](*value) ⇒ Object
Shortcut for Failure([...]).
-
.to_proc ⇒ Proc
Returns a constructor proc.
Instance Method Summary collapse
- #===(other) ⇒ Boolean
-
#either(_, g) ⇒ Any
Returns result of applying second function to the internal value.
- #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
#and, #apply, #bind, #deconstruct, #discard, #flatten, #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.
188 189 190 191 |
# File 'lib/dry/monads/result.rb', line 188 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
184 185 186 |
# File 'lib/dry/monads/result.rb', line 184 def trace @trace end |
Class Method Details
.[](*value) ⇒ Object
Shortcut for Failure([...])
@example include Dry::Monads[:result]
def call Failure[:error, :not_found] # => Failure([:error, :not_found]) end
169 170 171 |
# File 'lib/dry/monads/result.rb', line 169 def self.[](*value) new(value, RightBiased::Left.trace_caller) end |
.to_proc ⇒ Proc
Returns a constructor proc
176 177 178 |
# File 'lib/dry/monads/result.rb', line 176 def self.to_proc @to_proc ||= method(:new).to_proc end |
Instance Method Details
#===(other) ⇒ Boolean
273 274 275 |
# File 'lib/dry/monads/result.rb', line 273 def ===(other) Failure === other && failure === other.failure end |
#either(_, g) ⇒ Any
Returns result of applying second function to the internal value.
285 286 287 |
# File 'lib/dry/monads/result.rb', line 285 def either(_, g) g.(failure) end |
#failure ⇒ Object
194 195 196 |
# File 'lib/dry/monads/result.rb', line 194 def failure @value end |
#failure? ⇒ Boolean
Returns true
206 207 208 |
# File 'lib/dry/monads/result.rb', line 206 def failure? true end |
#flip ⇒ Result::Success
Transform to a Success instance
258 259 260 |
# File 'lib/dry/monads/result.rb', line 258 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.
225 226 227 228 229 230 231 |
# File 'lib/dry/monads/result.rb', line 225 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.
241 242 243 |
# File 'lib/dry/monads/result.rb', line 241 def or_fmap(*args, &block) Success.new(self.or(*args, &block)) end |
#result(f, _) ⇒ Object
Apply the first function to value.
201 202 203 |
# File 'lib/dry/monads/result.rb', line 201 def result(f, _) f.(@value) end |
#success? ⇒ Boolean
Returns false
211 212 213 |
# File 'lib/dry/monads/result.rb', line 211 def success? false end |
#to_maybe ⇒ Maybe::None
336 337 338 |
# File 'lib/dry/monads/maybe.rb', line 336 def to_maybe Maybe::None.new(trace) end |
#to_s ⇒ String Also known as: inspect
246 247 248 249 250 251 252 |
# File 'lib/dry/monads/result.rb', line 246 def to_s if Unit.equal?(@value) 'Failure()' else "Failure(#{@value.inspect})" end end |
#to_validated ⇒ Validated::Valid
Transforms to Validated
298 299 300 |
# File 'lib/dry/monads/validated.rb', line 298 def to_validated Validated::Invalid.new(failure, trace) end |
#value_or(val = nil) ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/dry/monads/result.rb', line 263 def value_or(val = nil) if block_given? yield(@value) else val end end |