Class: Decoding::Decoders::Map

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

Overview

A decoder that decodes a value and then applies a transformation to it, if successful.

See Also:

Instance Method Summary collapse

Methods inherited from Decoding::Decoder

#failure, #to_decoder

Constructor Details

#initialize(decoder, *rest) {|value| ... } ⇒ Map

Returns a new instance of Map.

Parameters:

Yield Parameters:

  • value (a)

Yield Returns:

  • (b)


15
16
17
18
19
# File 'lib/decoding/decoders/map.rb', line 15

def initialize(decoder, *rest, &block)
  @decoders = [decoder, *rest].map(&:to_decoder)
  @block = block
  super()
end

Instance Method Details

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

Parameters:

  • value (Object)

Returns:



23
24
25
26
27
# File 'lib/decoding/decoders/map.rb', line 23

def call(value)
  Result
    .all(@decoders.map { _1.call(value) })
    .map { @block.call(*_1) }
end