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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d613b53b493863ecae1fcbc778f5684acbbc628ad51f88fca0953889cd138d0
4
- data.tar.gz: cfc4039a5bfcf182c174b0d1f037d2cbd3b610fa746904906a2209b680fb4cad
3
+ metadata.gz: e42aa2335c51607c8da6110d0bec04cfef6b2af82365e003b51d54db365e091a
4
+ data.tar.gz: 50d36892d40a7a3ca152125a0265cd7c4ac2f87da78c236f7ca98844f83bd81a
5
5
  SHA512:
6
- metadata.gz: cbe2df98d6a2b6b9588cc5ecfe5543cae21575488a09d99768f10791fdb66332f8cda35483b6e61045a8bbdb5949f900cdd69ab94cfb602e456fb05f5dbfa14e
7
- data.tar.gz: e0eaddb3e027ba796f126aa500564507481847c196d139b16a51c390cb28990706be3d959bde8700857abbf2c18fc69765f79c03cd7cd16cbfd5e7a7be03be92
6
+ metadata.gz: 561b13185e19d9402fc8b416e8cb851f8dea4eb0bf0ad337151e5e43eb6eb97fe76211b2d85aebbbb44ce7111aa852a1bb61015585dab1af68fc4afe250d055d
7
+ data.tar.gz: 356c32052eba205c3ff07395d20afd87d5b4c6c564ce50e7727a2bffba6cb1a43f537e1a92ca33bd628ab819ad01ac643ad30f700387f78914d056e9b8246ce7
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- require:
2
+ plugins:
3
3
  - rubocop-rspec
4
4
  - rubocop-performance
5
5
  - rubocop-rake
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0]
4
+
5
+ * Add `Decoding::Data` for creating decodable data classes
6
+
3
7
  ## [0.2.6]
4
8
 
5
9
  * Add `Decoding::Result#unwrap!`
@@ -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
@@ -233,6 +233,8 @@ module Decoding
233
233
  # # => Decode::Ok({ id: 1 })
234
234
  # @return Decoding::Decoder
235
235
  def decode_hash(decoders)
236
+ return succeed({}) if decoders.empty?
237
+
236
238
  map(*decoders.values) do |*values|
237
239
  decoders.keys.zip(values).to_h
238
240
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decoding
4
- VERSION = "0.2.6"
4
+ VERSION = "0.3.0"
5
5
  end
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.2.6
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