Class: Errant::Failure

Inherits:
Result
  • Object
show all
Defined in:
lib/errant/failure.rb

Constant Summary

Constants inherited from Result

Result::DEFAULT_EXCEPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Result

#flatten

Constructor Details

#initialize(value, exc = Result::DEFAULT_EXCEPTIONS) ⇒ Failure

Returns a new instance of Failure.



5
6
7
8
# File 'lib/errant/failure.rb', line 5

def initialize(value, exc = Result::DEFAULT_EXCEPTIONS)
  @value = value
  @exceptions = exc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



10
11
12
# File 'lib/errant/failure.rb', line 10

def method_missing(name, *args, &block)
  self
end

Instance Attribute Details

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



3
4
5
# File 'lib/errant/failure.rb', line 3

def exceptions
  @exceptions
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/errant/failure.rb', line 3

def value
  @value
end

Class Method Details

.[](value, exc = Result::DEFAULT_EXCEPTIONS) ⇒ Object



46
47
48
# File 'lib/errant/failure.rb', line 46

def self.[](value, exc = Result::DEFAULT_EXCEPTIONS)
  Failure.new(value, exc)
end

Instance Method Details

#each_error {|value| ... } ⇒ Object

Perform side effects using the error. This is useful for logging and debugging, although it should generally be avoided in production code, in favor of error handling at a single location.

Yields:



23
24
25
26
# File 'lib/errant/failure.rb', line 23

def each_error(&blk)
  yield value
  self
end

#map_error(&blk) ⇒ Object

Pass the wrapped error into the given block and then rewrap the return value in a new Failure. This is useful for normalization.



30
31
32
# File 'lib/errant/failure.rb', line 30

def map_error(&blk)
  Failure.new(yield(value))
end

#or_else {|value| ... } ⇒ Object

Pass the wrapped error into the given block, returning an unwrapped value from the block. This is useful for providing defaults.

Yields:



16
17
18
# File 'lib/errant/failure.rb', line 16

def or_else(&blk)
  yield value
end

#successful?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/errant/failure.rb', line 34

def successful?
  false
end

#to_aObject



38
39
40
# File 'lib/errant/failure.rb', line 38

def to_a
  []
end

#to_aryObject



42
43
44
# File 'lib/errant/failure.rb', line 42

def to_ary
  signal
end