Module: Decoding
- Defined in:
- lib/decoding.rb,
lib/decoding/env.rb,
lib/decoding/data.rb,
lib/decoding/error.rb,
lib/decoding/result.rb,
lib/decoding/decoder.rb,
lib/decoding/failure.rb,
lib/decoding/version.rb,
lib/decoding/decoders.rb,
lib/decoding/decoders/at.rb,
lib/decoding/decoders/any.rb,
lib/decoding/decoders/map.rb,
lib/decoding/decoders/uri.rb,
lib/decoding/decoders/date.rb,
lib/decoding/decoders/enum.rb,
lib/decoding/decoders/fail.rb,
lib/decoding/decoders/hash.rb,
lib/decoding/decoders/lazy.rb,
lib/decoding/decoders/pass.rb,
lib/decoding/decoders/time.rb,
lib/decoding/decoders/array.rb,
lib/decoding/decoders/field.rb,
lib/decoding/decoders/index.rb,
lib/decoding/decoders/match.rb,
lib/decoding/matcher_helpers.rb,
lib/decoding/decoders/map_err.rb,
lib/decoding/decoders/succeed.rb,
lib/decoding/decoders/and_then.rb,
lib/decoding/decoders/optional.rb,
lib/decoding/decoders/big_decimal.rb,
lib/decoding/decoders/optional_field.rb
Overview
Decoding is a library to help transform unknown external data into neat values with known shapes.
Defined Under Namespace
Modules: Data, Decoders, MatcherHelpers Classes: Decoder, Err, Error, Failure, Ok, Result, UnwrapError
Constant Summary collapse
- ENV_TYPES =
The types env accepts by name, mapped to the decoder that reads them.
Environment variables are always strings, so these are all decoders that parse a string. Pass a decoder directly for anything else.
{ string: :string, symbol: :symbol, integer: :parsed_integer, float: :parsed_float, boolean: :parsed_boolean }.freeze
- VERSION =
"0.5.0"
Class Method Summary collapse
-
.decode(decoder, value) ⇒ Decoding::Result<a>
Run a given
decoderon the given inputvalue. -
.decode!(decoder, value) ⇒ Object
Run a given
decoderon the given inputvalue, returning the decoded value or raising an error when decoding failed. -
.env(name, type = :string, default: NO_DEFAULT, from: ENV) ⇒ Object
Read a single environment variable, decoding its value.
Class Method Details
.decode(decoder, value) ⇒ Decoding::Result<a>
Run a given decoder on the given input value.
121 |
# File 'lib/decoding.rb', line 121 def decode(decoder, value) = decoder.call(value).map_err(&:to_s) |
.decode!(decoder, value) ⇒ Object
Run a given decoder on the given input value, returning the decoded
value or raising an error when decoding failed.
135 |
# File 'lib/decoding.rb', line 135 def decode!(...) = decode(...).unwrap! |
.env(name, type = :string, default: NO_DEFAULT, from: ENV) ⇒ Object
Read a single environment variable, decoding its value.
This is meant for configuration read at boot time, such as a Rails initializer: it returns the decoded value itself and raises when the variable is missing or its value cannot be decoded, rather than letting a misconfigured application start.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/decoding/env.rb', line 48 def env(name, type = :string, default: NO_DEFAULT, from: ENV) decoder = env_decoder(type) return default if !from.key?(name) && !default.equal?(NO_DEFAULT) decode!( Decoders.map_err(decoder) do |, value| value.nil? ? "ENV[#{name.inspect}] is not set" : "ENV[#{name.inspect}]: #{message}" end, from.fetch(name, nil) ) end |