Module: Decoding::Decoders
- Defined in:
- lib/decoding/decoders.rb,
lib/decoding/decoders/at.rb,
lib/decoding/decoders/any.rb,
lib/decoding/decoders/map.rb,
lib/decoding/decoders/hash.rb,
lib/decoding/decoders/pass.rb,
lib/decoding/decoders/array.rb,
lib/decoding/decoders/field.rb,
lib/decoding/decoders/index.rb,
lib/decoding/decoders/match.rb,
lib/decoding/decoders/and_then.rb
Overview
Decoders are composable functions for deconstructing unknown input values into known output values.
Defined Under Namespace
Classes: AndThen, Any, Array, At, Field, Hash, Index, Map, Match, Pass
Simple decoders collapse
-
.boolean ⇒ Decoding::Decoder<Boolean>
Decode a boolean value (either
trueorfalse). -
.false ⇒ Decoding::Decoder<FalseClass>
Decode a
falsevalue. -
.float ⇒ Decoding::Decoder<Float>
Decode any float value.
-
.integer ⇒ Decoding::Decoder<Integer>
Decode any integer value.
-
.nil ⇒ Decoding::Decoder<NilClass>
Decode a
nilvalue. -
.numeric ⇒ Decoding::Decoder<Numeric>
Decode any numeric value (includes both integers and floats).
-
.regexp(regex) ⇒ Decoding::Decoder<String>
Decode any string value that matches a regular expression.
-
.string ⇒ Decoding::Decoder<String>
Decode any string value.
-
.symbol ⇒ Decoding::Decoder<Symbol>
Decode a String value into a symbol.
-
.true ⇒ Decoding::Decoder<TrueClass>
Decode a
truevalue.
Utility decoders collapse
-
.fail(value) ⇒ Decoding::Decoder<String>
A decoder that always fails with the given value.
-
.original ⇒ Decoding::Decoder<Object>
A decoder that returns the input value, unaltered.
-
.succeed(value) ⇒ Decoding::Decoder<String>
A decoder that always succeeds with the given value.
Compound decoders collapse
-
.and_then(deocder) {|value| ... } ⇒ Decoding::Decoder<b>
Create a decoder that depends on a previously decoded value.
-
.any(decoder, *decoders) ⇒ Decoding::Decoder<a>
Decode a value by trying many different decoders in order, using the first matching result -- or a failure when none of the given decoders succeed.
-
.array(decoder) ⇒ Decoding::Decoder<Array<a>>
Decode an array of values using a given decoder.
-
.at(*fields, decoder) ⇒ Decoding::Decoder<a>
Decode deeply-nested fields.
-
.decode_hash(decoders) ⇒ Object
Decode a value into a hash using multiple decoders.
-
.field(key, decoder) ⇒ Decoding::Decoder<a>
Decode a value from a given key in a hash.
-
.hash(key_decoder, value_decoder) ⇒ Decoding::Decoder<Hash<a, b>>
Decode a Hash with arbitrary contents using two decoders for the keys and the pairs.
-
.index(integer, decoder) ⇒ Decoding::Decoder<a>
Decode an array element by index using a given decoder.
-
.map(decoder, *decoders) {|value| ... } ⇒ Decoding::Decoder<b>
Decode a value with the given decoder and, if successful, apply a block to the decoded result.
-
.optional(decoder) ⇒ Decoding::Decoder<a, nil>
Decode a value that may or may not be
nil.
Class Method Details
.and_then(deocder) {|value| ... } ⇒ Decoding::Decoder<b>
Create a decoder that depends on a previously decoded value.
259 |
# File 'lib/decoding/decoders.rb', line 259 def and_then(...) = Decoders::AndThen.new(...) |
.any(decoder, *decoders) ⇒ Decoding::Decoder<a>
Decode a value by trying many different decoders in order, using the first matching result -- or a failure when none of the given decoders succeed.
162 |
# File 'lib/decoding/decoders.rb', line 162 def any(...) = Decoders::Any.new(...) |
.array(decoder) ⇒ Decoding::Decoder<Array<a>>
Decode an array of values using a given decoder.
192 |
# File 'lib/decoding/decoders.rb', line 192 def array(...) = Decoders::Array.new(...) |
.at(*fields, decoder) ⇒ Decoding::Decoder<a>
Decode deeply-nested fields.
274 275 276 |
# File 'lib/decoding/decoders.rb', line 274 def at(...) Decoders::At.new(...) end |
.boolean ⇒ Decoding::Decoder<Boolean>
Decode a boolean value (either true or false).
93 |
# File 'lib/decoding/decoders.rb', line 93 def boolean = any(self.true, self.false) |
.decode_hash(decoders) ⇒ Object
Decode a value into a hash using multiple decoders.
This is a shortcut for:
deocde(map(field("id", integer), field("name", string)) { |id, name| { id:, name: } }, { "id" => 1, "name" => "John" }) # => Decoding::Ok({ id: 1, name: "John" })
233 234 235 236 237 |
# File 'lib/decoding/decoders.rb', line 233 def decode_hash(decoders) map(*decoders.values) do |*values| decoders.keys.zip(values).to_h end end |
.fail(value) ⇒ Decoding::Decoder<String>
A decoder that always fails with the given value.
116 |
# File 'lib/decoding/decoders.rb', line 116 def fail(value) = ->(_) { Result.err(Decoding::Failure.new(value)) } |
.false ⇒ Decoding::Decoder<FalseClass>
Decode a false value.
85 |
# File 'lib/decoding/decoders.rb', line 85 def false = Decoders::Match.new(FalseClass) |
.field(key, decoder) ⇒ Decoding::Decoder<a>
Decode a value from a given key in a hash.
182 |
# File 'lib/decoding/decoders.rb', line 182 def field(...) = Decoders::Field.new(...) |
.float ⇒ Decoding::Decoder<Float>
Decode any float value.
52 |
# File 'lib/decoding/decoders.rb', line 52 def float = Decoders::Match.new(Float) |
.hash(key_decoder, value_decoder) ⇒ Decoding::Decoder<Hash<a, b>>
Decode a Hash with arbitrary contents using two decoders for the keys and the pairs.
216 |
# File 'lib/decoding/decoders.rb', line 216 def hash(...) = Decoders::Hash.new(...) |
.index(integer, decoder) ⇒ Decoding::Decoder<a>
Decode an array element by index using a given decoder.
203 |
# File 'lib/decoding/decoders.rb', line 203 def index(...) = Decoders::Index.new(...) |
.integer ⇒ Decoding::Decoder<Integer>
Decode any integer value.
44 |
# File 'lib/decoding/decoders.rb', line 44 def integer = Decoders::Match.new(Integer) |
.map(decoder, *decoders) {|value| ... } ⇒ Decoding::Decoder<b>
Decode a value with the given decoder and, if successful, apply a block to the decoded result.
Given multiple decoders, apply them all to the same value and, if all succeeded, create a single output value from them.
150 |
# File 'lib/decoding/decoders.rb', line 150 def map(...) = Decoders::Map.new(...) |
.nil ⇒ Decoding::Decoder<NilClass>
Decode a nil value.
69 |
# File 'lib/decoding/decoders.rb', line 69 def nil = Decoders::Match.new(NilClass) |
.numeric ⇒ Decoding::Decoder<Numeric>
Decode any numeric value (includes both integers and floats).
61 |
# File 'lib/decoding/decoders.rb', line 61 def numeric = Decoders::Match.new(Numeric) |
.optional(decoder) ⇒ Decoding::Decoder<a, nil>
Decode a value that may or may not be nil.
171 |
# File 'lib/decoding/decoders.rb', line 171 def optional(decoder) = any(decoder, self.nil) |
.original ⇒ Decoding::Decoder<Object>
A decoder that returns the input value, unaltered.
123 |
# File 'lib/decoding/decoders.rb', line 123 def original = Decoders::Pass.new |
.regexp(regex) ⇒ Decoding::Decoder<String>
Decode any string value that matches a regular expression.
36 |
# File 'lib/decoding/decoders.rb', line 36 def regexp(regex) = Decoders::Match.new(Regexp.new(regex)) |
.string ⇒ Decoding::Decoder<String>
Decode any string value.
29 |
# File 'lib/decoding/decoders.rb', line 29 def string = Decoders::Match.new(String) |
.succeed(value) ⇒ Decoding::Decoder<String>
A decoder that always succeeds with the given value.
109 |
# File 'lib/decoding/decoders.rb', line 109 def succeed(value) = ->(_) { Result.ok(value) } |
.symbol ⇒ Decoding::Decoder<Symbol>
Decode a String value into a symbol.
100 |
# File 'lib/decoding/decoders.rb', line 100 def symbol = map(string, &:to_sym) |
.true ⇒ Decoding::Decoder<TrueClass>
Decode a true value.
77 |
# File 'lib/decoding/decoders.rb', line 77 def true = Decoders::Match.new(TrueClass) |