decoding 0.2.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +4 -0
- data/lib/decoding/data.rb +53 -0
- data/lib/decoding/decoders.rb +2 -0
- data/lib/decoding/version.rb +1 -1
- data/lib/decoding.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e42aa2335c51607c8da6110d0bec04cfef6b2af82365e003b51d54db365e091a
|
|
4
|
+
data.tar.gz: 50d36892d40a7a3ca152125a0265cd7c4ac2f87da78c236f7ca98844f83bd81a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 561b13185e19d9402fc8b416e8cb851f8dea4eb0bf0ad337151e5e43eb6eb97fe76211b2d85aebbbb44ce7111aa852a1bb61015585dab1af68fc4afe250d055d
|
|
7
|
+
data.tar.gz: 356c32052eba205c3ff07395d20afd87d5b4c6c564ce50e7727a2bffba6cb1a43f537e1a92ca33bd628ab819ad01ac643ad30f700387f78914d056e9b8246ce7
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "decoders"
|
|
4
|
+
|
|
5
|
+
module Decoding
|
|
6
|
+
# Wrapper around Ruby's `Data` class, providing a way to both define a new
|
|
7
|
+
# data class as usual but including a decoder to decode a value into such a
|
|
8
|
+
# data value.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# User = Decoding::Data.define(
|
|
12
|
+
# name: field("postName", string),
|
|
13
|
+
# age: field("yearsOld", integer)
|
|
14
|
+
# ) do
|
|
15
|
+
# def greet
|
|
16
|
+
# "Hello, my name is #{name} and I am #{age} years old."
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
# payload = { "postName" => "John", "yearsOld" => 30 }
|
|
20
|
+
# Decoding.decode(User.decoder, payload) => Decoding::Ok(user)
|
|
21
|
+
# user.greet # => "Hello, my name is John and I am 30 years old."
|
|
22
|
+
# @return [Class]
|
|
23
|
+
module Data
|
|
24
|
+
# @param attributes [Hash<Symbol, Decoding::Decoder>] A hash of attribute
|
|
25
|
+
# names and decoders
|
|
26
|
+
# @see ::Data.define
|
|
27
|
+
# @return [Class] A new data class with a `decoder` method.
|
|
28
|
+
def self.define(attributes, &)
|
|
29
|
+
# First ensure we have a hash of attribute names and decoders
|
|
30
|
+
attributes = attributes.to_h do |key, value|
|
|
31
|
+
[key.to_sym, value.to_decoder]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Define the data class as usual
|
|
35
|
+
data_class = ::Data.define(*attributes.keys, &)
|
|
36
|
+
|
|
37
|
+
# Add a special `decode` method to use the provided decoders to decode a
|
|
38
|
+
# value as hash and build a new instance with it
|
|
39
|
+
data_class.define_singleton_method(:decoder) do
|
|
40
|
+
Decoding::Decoders.map(
|
|
41
|
+
Decoding::Decoders.decode_hash(attributes)
|
|
42
|
+
) { |attrs| new(**attrs) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Adapt to `to_decoder` protocol
|
|
46
|
+
data_class.define_singleton_method(:to_decoder) { decoder }
|
|
47
|
+
|
|
48
|
+
# Return the data class so regular assignment like
|
|
49
|
+
# `User = ::Data.define(...)` works.
|
|
50
|
+
data_class
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/lib/decoding/decoders.rb
CHANGED
data/lib/decoding/version.rb
CHANGED
data/lib/decoding.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "decoding/version"
|
|
4
4
|
require_relative "decoding/decoders"
|
|
5
|
+
require_relative "decoding/data"
|
|
5
6
|
|
|
6
7
|
# Decoding is a library to help transform unknown external data into neat values
|
|
7
8
|
# with known shapes. Consider calling an HTTP API: you might pull in whatever
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: decoding
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Arjan van der Gaag
|
|
@@ -28,6 +28,7 @@ files:
|
|
|
28
28
|
- README.md
|
|
29
29
|
- Rakefile
|
|
30
30
|
- lib/decoding.rb
|
|
31
|
+
- lib/decoding/data.rb
|
|
31
32
|
- lib/decoding/decoder.rb
|
|
32
33
|
- lib/decoding/decoders.rb
|
|
33
34
|
- lib/decoding/decoders/and_then.rb
|