decoding 0.2.5 → 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: 91611249210837a0a3b5140bd19bd53d5862bf4ab7f56249797da3e9348c5712
4
- data.tar.gz: 518d2eee3ff13717a2170b2c008d297a7c60986886e3281ef0b5d883e7b077b8
3
+ metadata.gz: e42aa2335c51607c8da6110d0bec04cfef6b2af82365e003b51d54db365e091a
4
+ data.tar.gz: 50d36892d40a7a3ca152125a0265cd7c4ac2f87da78c236f7ca98844f83bd81a
5
5
  SHA512:
6
- metadata.gz: 01d88efba9c04b704763945e87c70a18e437a64313b944382eac83fd795fa293bb20618d92c513babfd17f6a1c51efdb2c039fa2aab7d914608a8115ef30501c
7
- data.tar.gz: a02ca210632cdb66dd15e0f0b14aae449b43aace345988a38a94abd626f87c1f37bd5b66dfdd2d29202ff0ac2ce850012b18d522844d31239839d73c66626cf4
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,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0]
4
+
5
+ * Add `Decoding::Data` for creating decodable data classes
6
+
7
+ ## [0.2.6]
8
+
9
+ * Add `Decoding::Result#unwrap!`
10
+
3
11
  ## [0.2.5]
4
12
 
5
13
  * Report all error messages in the `any` decoder
@@ -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,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decoding
4
+ # Raised when calling {Result#unwrap!} on an `Err` value.
5
+ class UnwrapError < StandardError; end
6
+
4
7
  # A result represent the outcome of some computation that can succeed or fail.
5
8
  # The results are represented with two subclasses of `Result`: `Ok` and `Err`.
6
9
  # Each hold a single result value.
@@ -83,6 +86,14 @@ module Decoding
83
86
  # @return [Object]
84
87
  def unwrap(default_value) = default_value
85
88
 
89
+ # Extract the value out of a `Result` value. In case of an `Ok`, this
90
+ # returns the result's value. In case of an `Err`, a {UnwrapError} is
91
+ # raised with the error value as its message.
92
+ #
93
+ # @raise [Decoding::UnwrapError] if the result is an `Err` value.
94
+ # @return [Object]
95
+ def unwrap! = raise(UnwrapError, value.to_s)
96
+
86
97
  # Extract the error value out of a `Result` value. In case of an `Err`, this
87
98
  # returns the result's value. In case of an `Ok`, the given `default_value`
88
99
  # is returned.
@@ -142,6 +153,7 @@ module Decoding
142
153
 
143
154
  def ok? = true
144
155
  def unwrap(_) = value
156
+ def unwrap! = value
145
157
  def map = self.class.new(yield value)
146
158
 
147
159
  def and(other)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decoding
4
- VERSION = "0.2.5"
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.5
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