Class: Decoding::Failure

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

Instance Method Summary collapse

Constructor Details

#initialize(msg) ⇒ Failure

Returns a new instance of Failure.



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

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

Instance Method Details

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

Returns:

  • (Boolean)


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

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.

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.

Parameters:

  • segment (String)

Returns:



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

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

#to_sObject



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

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