Class: Decoding::Decoders::Any
- Inherits:
-
Decoding::Decoder
- Object
- Decoding::Decoder
- Decoding::Decoders::Any
- Defined in:
- lib/decoding/decoders/any.rb
Overview
A decoder wrapping any number of decoders, finding the first one that matches the given value and returning its result.
Instance Method Summary collapse
- #call(value) ⇒ Decoding::Result<Object>
-
#initialize(decoder, *decoders) ⇒ Any
constructor
A new instance of Any.
Methods inherited from Decoding::Decoder
Constructor Details
#initialize(decoder, *decoders) ⇒ Any
Returns a new instance of Any.
15 16 17 18 |
# File 'lib/decoding/decoders/any.rb', line 15 def initialize(decoder, *decoders) @decoders = [decoder, *decoders].map(&:to_decoder) super() end |
Instance Method Details
#call(value) ⇒ Decoding::Result<Object>
22 23 24 25 |
# File 'lib/decoding/decoders/any.rb', line 22 def call(value) err_proc = -> { err(failure("None of the decoders matched")) } @decoders.lazy.map { _1.call(value) }.find(err_proc, &:ok?) end |