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/either.rb,
lib/dry/monads/validated.rb

Overview

Represents a value of a failed operation.

Constant Summary

Constants inherited from Dry::Monads::Result

Left, Right

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

#apply, #bind, #discard, #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



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

#traceString (readonly)

Line where the value was constructed

Returns:

  • (String)


138
139
140
# File 'lib/dry/monads/result.rb', line 138

def trace
  @trace
end

Class Method Details

.to_procProc

Returns a constructor proc

Returns:

  • (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

Parameters:

Returns:

  • (Boolean)


223
224
225
# File 'lib/dry/monads/result.rb', line 223

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

#failureObject



148
149
150
# File 'lib/dry/monads/result.rb', line 148

def failure
  @value
end

#failure?Boolean

Returns true

Returns:

  • (Boolean)


160
161
162
# File 'lib/dry/monads/result.rb', line 160

def failure?
  true
end

#flipResult::Success

Transform to a Success instance

Returns:



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.

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)


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.

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:



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

Returns:

  • (Boolean)


165
166
167
# File 'lib/dry/monads/result.rb', line 165

def success?
  false
end

#to_maybeMaybe::None

Returns:



245
246
247
# File 'lib/dry/monads/maybe.rb', line 245

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

#to_sString Also known as: inspect

Returns:

  • (String)


200
201
202
# File 'lib/dry/monads/result.rb', line 200

def to_s
  "Failure(#{ @value.inspect })"
end

#to_validatedValidated::Valid

Transforms to Validated

Returns:



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