dry-types-tuple 0.3.2 → 0.4.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: 37df5ee0299ce0e65a395a458f0284fa540f1f4c3cbc5f09ca7424a9b5ca1ca3
4
- data.tar.gz: 0d83445514c90cf9381c49fb805b98d49ef2c9c6a00c776fc921ba39fda6a5d0
3
+ metadata.gz: 79f57bc1de003bce8abb343b5335af356aaec2a8a2b292ea07d5738ab26a6f34
4
+ data.tar.gz: cb76860dc5f9c0625fe1f31ab9977c4417329c5a9a93126bd8974d5d7dab4e09
5
5
  SHA512:
6
- metadata.gz: e1c4fc70ed670e992613b22444d68fef3a4fcddb6459405657272099dc54caec158f3a77e20ed00df352de869e922188dcac6ba2241aecaec36d5995ae2dd234
7
- data.tar.gz: 1a077702745b571a3c0c5d8ec8dd00aa1885971b054dc1b96e23a026b1b24798756e496f653eebdaad611b034ab3e0607934e4c5b75440a930e394eebeb7214c
6
+ metadata.gz: beb9c8e509014a76d91327e3eeba59d35fb3e66f833384480ef05a63a426497e479ba0493c057fa7134cdc87117e794e88de4e84329bfed0f5ff347774f9035b
7
+ data.tar.gz: 882be3b1c04acaa62d974eed786521083e07ae3969c549b8612f7448dbfde6da995c1f24c048dea16bca5c60b7439053bb67a86701d0a324010ab7b35ad0c57d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.4.0 2025-07-02
2
+
3
+ - Better matching of sums and complex tuples by `Dry::Types[…]` inferrer.
4
+ - Add more specs for inferrer; bit of rework in specs in general.
5
+
1
6
  ## 0.3.1 2025-06-26
2
7
 
3
8
  - Change minimal ruby version to 3.1
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Tuple
5
- VERSION = '0.3.2'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
@@ -223,35 +223,56 @@ module Dry
223
223
  register "coercible.tuple", nominal_tuple.constructor(Kernel.method(:Array))
224
224
  register "params.tuple", nominal_tuple.constructor(Coercions::Params.method(:to_ary))
225
225
 
226
- TUPLE_PREFIX_REGEX = /(?:(?:coercible|nominal|params|strict)\.)?tuple(?=\<)/
227
- TUPLE_MEMBERS_REGEX = /(?<=tuple<).+(?=>)/
228
- TUPLE_MEMBERS_SCAN_REGEX = /(tuple\<(?:\g<1>\,)*\g<1>\>|\[(\g<1>)\](?=$)|[^,]+)(?=,|$)/
229
- SUM_MATCH_REGEX = /((?:(?:\A|\|)(?:[^\|]+))*)\|([^\|]+)\z/
226
+ TUPLE_TYPE_SPEC_REGEX = /\A((?:(?:coercible|nominal|params|strict)\.)?tuple)\<(.+)\>\z/
227
+ TUPLE_MEMBERS_SCAN_REGEX = /(?<=\A|,)(tuple\<(?:\g<1>\,)*\g<1>\>|\[(\g<1>)\](?=$)|[^,]+)(?=,|\z)/
230
228
 
231
229
  # @api private
232
- module ReferenceHook
230
+ # Patch for {Dry::Types.[]} inferrer, that adds support for `tuple<type,types…,[rest]>` syntax
231
+ # to compose {Dry::Types::Tuple} types.
232
+ module ReferenceHookToMatchTuple
233
+ # Wraps over the original method.
234
+ # @see Dry::Types.[]
235
+ # @return [Dry::Types::Type]
233
236
  def [](name)
234
- case name
235
- when TUPLE_PREFIX_REGEX
236
- type_map.fetch_or_store(name) do
237
- key = Regexp.last_match[0]
238
- types =
239
- name[TUPLE_MEMBERS_REGEX].
240
- scan(TUPLE_MEMBERS_SCAN_REGEX).
241
- map { |(type, rest)| rest.nil? ? self[type] : [self[rest]] }
242
- super(key).of(*types)
237
+ type_map.fetch_or_store(name) do
238
+ if tuple_match = name.match(TUPLE_TYPE_SPEC_REGEX)
239
+ type_id, members = tuple_match[1..2]
240
+ types = members.scan(TUPLE_MEMBERS_SCAN_REGEX).map do |(type, rest)|
241
+ rest.nil? ? self[type] : [self[rest]]
242
+ end
243
+
244
+ super(type_id).of(*types)
245
+ else
246
+ super
243
247
  end
244
- when SUM_MATCH_REGEX
245
- type_map.fetch_or_store(name) do
246
- left, right = Regexp.last_match.captures
247
- self[left] | super(right)
248
+ end
249
+ end
250
+ end
251
+
252
+ singleton_class.prepend ReferenceHookToMatchTuple
253
+
254
+ SUM_MATCH_REGEX = /(?<=\A)(.+)\|(?!\w+[\>\],])([^\|]+)\z/
255
+
256
+ # @api private
257
+ # Patch for {Dry::Types.[]} inferrer, that adds support for `type|type|…` syntax
258
+ # to compose {Dry::Types::Sum} types.
259
+ module ReferenceHookToMatchSum
260
+ # Wraps over the previously hooked method.
261
+ # @see Dry::Types::ReferenceHookToMatchTuple#[]
262
+ # @see Dry::Types.[]
263
+ # @return [Dry::Types::Type]
264
+ def [](name)
265
+ type_map.fetch_or_store(name) do
266
+ if sum_match = name.match(SUM_MATCH_REGEX)
267
+ left, right = sum_match.captures
268
+ self[left] | self[right]
269
+ else
270
+ super
248
271
  end
249
- else
250
- super(name)
251
272
  end
252
273
  end
253
274
  end
254
275
 
255
- singleton_class.prepend ReferenceHook
276
+ singleton_class.prepend ReferenceHookToMatchSum
256
277
  end
257
278
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-types-tuple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Semenov