class Dry::Monads::Result::Failure
Represents a value of a failed operation.
@api public
Attributes
Line where the value was constructed
@return [String] @api public
Public Class Methods
Shortcut for Failure()
@example include Dry::Monads[:result] def call Failure[:error, :not_found] # => Failure([:error, :not_found]) end
@api public
# File lib/dry/monads/result.rb, line 171 def self.[](*value) new(value, RightBiased::Left.trace_caller) end
@param value [Object] failure value @param trace [String] caller line
# File lib/dry/monads/result.rb, line 190 def initialize(value, trace = RightBiased::Left.trace_caller) super() @value = value @trace = trace end
Returns a constructor proc
@return [Proc]
# File lib/dry/monads/result.rb, line 178 def self.to_proc @to_proc ||= method(:new).to_proc end
Public Instance Methods
@param other [Result] @return [Boolean]
# File lib/dry/monads/result.rb, line 278 def ===(other) Failure === other && failure === other.failure end
Lifts a block/proc over Failure
@overload alt_map
(proc)
@param proc [#call] @return [Result::Failure]
@overload alt_map
@param block [Proc] @return [Result::Failure]
# File lib/dry/monads/result.rb, line 304 def alt_map(proc = Undefined, &block) f = Undefined.default(proc, block) self.class.new(f.(failure), RightBiased::Left.trace_caller) end
Returns result of applying second function to the internal value.
@example
Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3
@param _ [#call] Ignored @param g [#call] Function to call @return [Any] Return value of ‘g`
# File lib/dry/monads/result.rb, line 290 def either(_, g) g.(failure) end
@private
# File lib/dry/monads/result.rb, line 197 def failure @value end
Returns true
# File lib/dry/monads/result.rb, line 209 def failure? true end
Transform to a Success
instance
@return [Result::Success]
# File lib/dry/monads/result.rb, line 263 def flip Success.new(@value) end
If a block is given passes internal value to it and returns the result, otherwise simply returns the first argument.
@example
Dry::Monads.Failure(ArgumentError.new('error message')).or(&:message) # => "error message"
@param args [Array<Object>] arguments that will be passed to a block
if one was given, otherwise the first value will be returned
@return [Object]
# File lib/dry/monads/result.rb, line 229 def or(*args) if block_given? yield(@value, *args) else args[0] end end
A lifted version of ‘#or`. Wraps the passed value or the block result with Result::Success
.
@example
Dry::Monads.Failure.new('no value').or_fmap('value') # => Success("value") Dry::Monads.Failure.new('no value').or_fmap { 'value' } # => Success("value")
@param args [Array<Object>] arguments will be passed to the underlying ‘#or` call @return [Result::Success] Wrapped value
# File lib/dry/monads/result.rb, line 246 def or_fmap(...) Success.new(self.or(...)) end
Apply the first function to value.
@api public
# File lib/dry/monads/result.rb, line 204 def result(f, _) f.(@value) end
Returns false
# File lib/dry/monads/result.rb, line 214 def success? false end
@return [Maybe::None]
# File lib/dry/monads/maybe.rb, line 396 def to_maybe Maybe::None.new(trace) end
@return [String]
# File lib/dry/monads/result.rb, line 251 def to_s if Unit.equal?(@value) "Failure()" else "Failure(#{@value.inspect})" end end
Transforms to Validated
@return [Validated::Invalid]
# File lib/dry/monads/validated.rb, line 301 def to_validated Validated::Invalid.new(failure, trace) end
@see RightBiased::Left#value_or
# File lib/dry/monads/result.rb, line 268 def value_or(val = nil) if block_given? yield(@value) else val end end