Class: Decoding::Failure

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

Overview

A failure is an error message, much like a string, but with an added stack of earlier messages.

This is useful to create clearer error messages when using compound decoders, such as array(string). If the string decoder fails with an error, the array decoder can push 3 to the stack to indicate that happened at index 3 in its input value.

Instance Method Summary collapse

Constructor Details

#initialize(msg) ⇒ Failure

Returns a new instance of Failure.



13
14
15
16
# File 'lib/decoding/failure.rb', line 13

def initialize(msg)
  @msg = msg
  @path = []
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/decoding/failure.rb', line 18

def eql?(other)
  other.is_a?(self.class) &&
    msg == other.msg &&
    path == other.path
end

#push(segment) ⇒ Decoding::Failure

Add segments to the stack of errors.

Parameters:

  • segment (String)

Returns:



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

def push(segment)
  @path << segment
  self
end

#to_sObject



34
35
36
37
38
39
40
# File 'lib/decoding/failure.rb', line 34

def to_s
  if @path.any?
    "Error at .#{@path.reverse.join(".")}: #{@msg}"
  else
    @msg
  end
end