Class: Decoding::Decoders::Match

Inherits:
Decoding::Decoder show all
Defined in:
lib/decoding/decoders/match.rb

Overview

Decoder that matches values using the === operator. This will work with regular expressions or classes.

Instance Method Summary collapse

Methods inherited from Decoding::Decoder

#failure, #to_decoder

Constructor Details

#initialize(pattern) ⇒ Match

Returns a new instance of Match.

Parameters:

  • pattern (#===)


11
12
13
14
# File 'lib/decoding/decoders/match.rb', line 11

def initialize(pattern)
  @pattern = pattern
  super()
end

Instance Method Details

#call(value) ⇒ Decoding::Result<a>

Parameters:

  • value (Object)

Returns:



18
19
20
21
22
23
24
25
26
# File 'lib/decoding/decoders/match.rb', line 18

def call(value)
  if @pattern === value
    ok(value)
  elsif @pattern.is_a?(Class)
    err(failure("expected #{@pattern}, got #{value.class}"))
  else
    err(failure("expected value matching #{@pattern.inspect}, got: #{value.inspect}"))
  end
end