Class: Dry::Monads::Result::Failure

Inherits:
Dry::Monads::Result show all
Includes:
Dry::Monads::RightBiased::Left
Defined in:
lib/dry/monads/result.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/validated.rb

Overview

Represents a value of a failed operation.

Instance Attribute Summary collapse

Attributes inherited from Dry::Monads::Result

#success

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dry::Monads::RightBiased::Left

#and, #apply, #bind, #deconstruct, #deconstruct_keys, #discard, #flatten, #fmap, #tee, trace_caller, #value!, #|

Methods inherited from Dry::Monads::Result

#monad, pure, #to_monad, #to_result

Methods included from Transformer

#fmap2, #fmap3

Constructor Details

#initialize(value, trace = RightBiased::Left.trace_caller) ⇒ Failure

Returns a new instance of Failure.

Parameters:

  • value (Object)

    failure value

  • trace (String) (defaults to: RightBiased::Left.trace_caller)

    caller line



190
191
192
193
194
# File 'lib/dry/monads/result.rb', line 190

def initialize(value, trace = RightBiased::Left.trace_caller)
  super()
  @value = value
  @trace = trace
end

Instance Attribute Details

#traceString (readonly)

Line where the value was constructed

Returns:

  • (String)


186
187
188
# File 'lib/dry/monads/result.rb', line 186

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


171
172
173
# File 'lib/dry/monads/result.rb', line 171

def self.[](*value)
  new(value, RightBiased::Left.trace_caller)
end

.to_procProc

Returns a constructor proc

Returns:

  • (Proc)


178
179
180
# File 'lib/dry/monads/result.rb', line 178

def self.to_proc
  @to_proc ||= method(:new).to_proc
end

Instance Method Details

#===(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


278
279
280
# File 'lib/dry/monads/result.rb', line 278

def ===(other)
  Failure === other && failure === other.failure
end

#alt_map(proc) ⇒ Result::Failure #alt_mapResult::Failure

Lifts a block/proc over Failure

Overloads:



304
305
306
307
# 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) ⇒ Any

Returns result of applying second function to the internal value.

Examples:

Dry::Monads.Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3

Parameters:

  • _ (#call)

    Ignored

  • g (#call)

    Function to call

Returns:

  • (Any)

    Return value of ‘g`



290
291
292
# File 'lib/dry/monads/result.rb', line 290

def either(_, g)
  g.(failure)
end

#failureObject



197
198
199
# File 'lib/dry/monads/result.rb', line 197

def failure
  @value
end

#failure?Boolean

Returns true

Returns:

  • (Boolean)


209
210
211
# File 'lib/dry/monads/result.rb', line 209

def failure?
  true
end

#flipResult::Success

Transform to a Success instance

Returns:



263
264
265
# File 'lib/dry/monads/result.rb', line 263

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.

Examples:

Dry::Monads.Failure(ArgumentError.new('error message')).or(&:message)
# => "error message"

Parameters:

  • args (Array<Object>)

    arguments that will be passed to a block if one was given, otherwise the first value will be returned

Returns:

  • (Object)


229
230
231
232
233
234
235
# File 'lib/dry/monads/result.rb', line 229

def or(*args)
  if block_given?
    yield(@value, *args)
  else
    args[0]
  end
end

#or_fmapResult::Success

A lifted version of ‘#or`. Wraps the passed value or the block result with Result::Success.

Examples:

Dry::Monads.Failure.new('no value').or_fmap('value') # => Success("value")
Dry::Monads.Failure.new('no value').or_fmap { 'value' } # => Success("value")

Parameters:

  • args (Array<Object>)

    arguments will be passed to the underlying ‘#or` call

Returns:



246
247
248
# File 'lib/dry/monads/result.rb', line 246

def or_fmap(...)
  Success.new(self.or(...))
end

#result(f, _) ⇒ Object

Apply the first function to value.



204
205
206
# File 'lib/dry/monads/result.rb', line 204

def result(f, _)
  f.(@value)
end

#success?Boolean

Returns false

Returns:

  • (Boolean)


214
215
216
# File 'lib/dry/monads/result.rb', line 214

def success?
  false
end

#to_maybeMaybe::None

Returns:



396
397
398
# File 'lib/dry/monads/maybe.rb', line 396

def to_maybe
  Maybe::None.new(trace)
end

#to_sString Also known as: inspect

Returns:

  • (String)


251
252
253
254
255
256
257
# File 'lib/dry/monads/result.rb', line 251

def to_s
  if Unit.equal?(@value)
    "Failure()"
  else
    "Failure(#{@value.inspect})"
  end
end

#to_validatedValidated::Invalid

Transforms to Validated

Returns:



301
302
303
# File 'lib/dry/monads/validated.rb', line 301

def to_validated
  Validated::Invalid.new(failure, trace)
end

#value_or(val = nil) ⇒ Object



268
269
270
271
272
273
274
# File 'lib/dry/monads/result.rb', line 268

def value_or(val = nil)
  if block_given?
    yield(@value)
  else
    val
  end
end