class Dry::Monads::Result::Failure

Represents a value of a failed operation.

@api public

Attributes

trace[R]

Line where the value was constructed

@return [String] @api public

Public Class Methods

[](*value) click to toggle source

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
new(value, trace = RightBiased::Left.trace_caller) click to toggle source

@param value [Object] failure value @param trace [String] caller line

Calls superclass method
# File lib/dry/monads/result.rb, line 190
def initialize(value, trace = RightBiased::Left.trace_caller)
  super()
  @value = value
  @trace = trace
end
to_proc() click to toggle source

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

===(other) click to toggle source

@param other [Result] @return [Boolean]

# File lib/dry/monads/result.rb, line 278
def ===(other)
  Failure === other && failure === other.failure
end
alt_map(proc = Undefined, &block) click to toggle source

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
either(_, g) click to toggle source

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
failure() click to toggle source

@private

# File lib/dry/monads/result.rb, line 197
def failure
  @value
end
failure?() click to toggle source

Returns true

# File lib/dry/monads/result.rb, line 209
def failure?
  true
end
flip() click to toggle source

Transform to a Success instance

@return [Result::Success]

# File lib/dry/monads/result.rb, line 263
def flip
  Success.new(@value)
end
inspect()
Alias for: to_s
or(*args) { |value, *args| ... } click to toggle source

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
or_fmap(...) click to toggle source

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
result(f, _) click to toggle source

Apply the first function to value.

@api public

# File lib/dry/monads/result.rb, line 204
def result(f, _)
  f.(@value)
end
success?() click to toggle source

Returns false

# File lib/dry/monads/result.rb, line 214
def success?
  false
end
to_maybe() click to toggle source

@return [Maybe::None]

# File lib/dry/monads/maybe.rb, line 396
def to_maybe
  Maybe::None.new(trace)
end
to_s() click to toggle source

@return [String]

# File lib/dry/monads/result.rb, line 251
def to_s
  if Unit.equal?(@value)
    "Failure()"
  else
    "Failure(#{@value.inspect})"
  end
end
Also aliased as: inspect
to_validated() click to toggle source

Transforms to Validated

@return [Validated::Invalid]

# File lib/dry/monads/validated.rb, line 301
def to_validated
  Validated::Invalid.new(failure, trace)
end
value_or(val = nil) { |value| ... } click to toggle source

@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