dry-types 1.7.2 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/README.md +1 -1
  4. data/dry-types.gemspec +12 -16
  5. data/lib/dry/types/any.rb +3 -9
  6. data/lib/dry/types/array/constructor.rb +3 -9
  7. data/lib/dry/types/array/member.rb +1 -3
  8. data/lib/dry/types/array.rb +1 -1
  9. data/lib/dry/types/builder.rb +19 -35
  10. data/lib/dry/types/builder_methods.rb +5 -15
  11. data/lib/dry/types/coercions/json.rb +3 -3
  12. data/lib/dry/types/coercions/params.rb +13 -13
  13. data/lib/dry/types/coercions.rb +13 -15
  14. data/lib/dry/types/compiler.rb +5 -7
  15. data/lib/dry/types/composition.rb +17 -31
  16. data/lib/dry/types/constrained.rb +31 -23
  17. data/lib/dry/types/constructor/function.rb +19 -31
  18. data/lib/dry/types/constructor/wrapper.rb +5 -11
  19. data/lib/dry/types/constructor.rb +9 -17
  20. data/lib/dry/types/core.rb +14 -6
  21. data/lib/dry/types/decorator.rb +7 -18
  22. data/lib/dry/types/default.rb +12 -16
  23. data/lib/dry/types/enum.rb +33 -13
  24. data/lib/dry/types/errors.rb +4 -8
  25. data/lib/dry/types/extensions/maybe.rb +4 -10
  26. data/lib/dry/types/fn_container.rb +3 -3
  27. data/lib/dry/types/hash/constructor.rb +3 -9
  28. data/lib/dry/types/hash.rb +8 -6
  29. data/lib/dry/types/implication.rb +3 -5
  30. data/lib/dry/types/inflector.rb +1 -1
  31. data/lib/dry/types/intersection.rb +3 -9
  32. data/lib/dry/types/lax.rb +4 -10
  33. data/lib/dry/types/map.rb +33 -41
  34. data/lib/dry/types/meta.rb +2 -6
  35. data/lib/dry/types/module.rb +3 -5
  36. data/lib/dry/types/nominal.rb +13 -35
  37. data/lib/dry/types/predicate_inferrer.rb +3 -5
  38. data/lib/dry/types/predicate_registry.rb +1 -3
  39. data/lib/dry/types/primitive_inferrer.rb +4 -12
  40. data/lib/dry/types/printable.rb +1 -3
  41. data/lib/dry/types/printer/composition.rb +3 -3
  42. data/lib/dry/types/printer.rb +6 -9
  43. data/lib/dry/types/result.rb +5 -15
  44. data/lib/dry/types/schema/key.rb +11 -26
  45. data/lib/dry/types/schema.rb +10 -12
  46. data/lib/dry/types/spec/types.rb +2 -2
  47. data/lib/dry/types/sum.rb +4 -8
  48. data/lib/dry/types/type.rb +2 -2
  49. data/lib/dry/types/version.rb +1 -1
  50. data/lib/dry/types.rb +2 -2
  51. metadata +8 -63
@@ -25,20 +25,14 @@ module Dry
25
25
  end
26
26
 
27
27
  # @api private
28
- def visit_hash(_)
29
- ::Hash
30
- end
28
+ def visit_hash(_) = ::Hash
31
29
  alias_method :visit_schema, :visit_hash
32
30
 
33
31
  # @api private
34
- def visit_array(_)
35
- ::Array
36
- end
32
+ def visit_array(_) = ::Array
37
33
 
38
34
  # @api private
39
- def visit_lax(node)
40
- visit(node)
41
- end
35
+ def visit_lax(node) = visit(node)
42
36
 
43
37
  # @api private
44
38
  def visit_constructor(node)
@@ -66,9 +60,7 @@ module Dry
66
60
  end
67
61
 
68
62
  # @api private
69
- def visit_any(_)
70
- ::Object
71
- end
63
+ def visit_any(_) = ::Object
72
64
  end
73
65
 
74
66
  # @return [Compiler]
@@ -7,9 +7,7 @@ module Dry
7
7
  # @return [String]
8
8
  #
9
9
  # @api private
10
- def to_s
11
- PRINTER.(self) { super }
12
- end
10
+ def to_s = PRINTER.(self) { super }
13
11
  alias_method :inspect, :to_s
14
12
  end
15
13
  end
@@ -30,12 +30,12 @@ module Dry
30
30
  end
31
31
  end
32
32
 
33
- def visit_constructor(type, &block)
33
+ def visit_constructor(type, &)
34
34
  case type
35
35
  when @composition_class
36
- visit_constructors(type, &block)
36
+ visit_constructors(type, &)
37
37
  else
38
- @printer.visit(type, &block)
38
+ @printer.visit(type, &)
39
39
  end
40
40
  end
41
41
  end
@@ -46,7 +46,7 @@ module Dry
46
46
  "#<Dry::Types[#{output}]>"
47
47
  end
48
48
 
49
- def visit(type, &block)
49
+ def visit(type, &)
50
50
  print_with = MAPPING.fetch(type.class) do
51
51
  if type.class < Constructor
52
52
  :visit_constructor
@@ -56,12 +56,10 @@ module Dry
56
56
  raise ArgumentError, "Do not know how to print #{type.class}"
57
57
  end
58
58
  end
59
- send(print_with, type, &block)
59
+ send(print_with, type, &)
60
60
  end
61
61
 
62
- def visit_any(_)
63
- yield "Any"
64
- end
62
+ def visit_any(_) = yield "Any"
65
63
 
66
64
  def visit_array(type)
67
65
  visit_options(EMPTY_HASH, type.meta) do |opts|
@@ -101,10 +99,10 @@ module Dry
101
99
  end
102
100
  end
103
101
 
104
- def visit_composition(composition, &block)
102
+ def visit_composition(composition, &)
105
103
  klass = composition.class
106
104
  @composition_printers[klass] = Composition.new(self, klass)
107
- @composition_printers[klass].visit(composition, &block)
105
+ @composition_printers[klass].visit(composition, &)
108
106
  end
109
107
 
110
108
  def visit_enum(enum)
@@ -114,8 +112,7 @@ module Dry
114
112
 
115
113
  visit_options(options) do |opts|
116
114
  if mapping == enum.inverted_mapping
117
- values = mapping.values.map(&:inspect).join(", ")
118
- yield "Enum<#{type} values={#{values}}#{opts}>"
115
+ yield "Enum(#{enum.joined_values})<#{type}#{opts}>"
119
116
  else
120
117
  mapping_str = mapping.map { |key, value|
121
118
  "#{key.inspect}=>#{value.inspect}"
@@ -25,16 +25,12 @@ module Dry
25
25
  # @return [true]
26
26
  #
27
27
  # @api public
28
- def success?
29
- true
30
- end
28
+ def success? = true
31
29
 
32
30
  # @return [false]
33
31
  #
34
32
  # @api public
35
- def failure?
36
- false
37
- end
33
+ def failure? = false
38
34
  end
39
35
 
40
36
  # Failure result
@@ -59,23 +55,17 @@ module Dry
59
55
  # @return [String]
60
56
  #
61
57
  # @api private
62
- def to_s
63
- error.to_s
64
- end
58
+ def to_s = error.to_s
65
59
 
66
60
  # @return [false]
67
61
  #
68
62
  # @api public
69
- def success?
70
- false
71
- end
63
+ def success? = false
72
64
 
73
65
  # @return [true]
74
66
  #
75
67
  # @api public
76
- def failure?
77
- true
78
- end
68
+ def failure? = true
79
69
  end
80
70
  end
81
71
  end
@@ -29,38 +29,31 @@ module Dry
29
29
  end
30
30
 
31
31
  unless name.is_a?(::Symbol)
32
- raise ArgumentError, "Schemas can only contain symbol keys, #{name.inspect} given"
32
+ raise ::ArgumentError,
33
+ "Schemas can only contain symbol keys, #{name.inspect} given"
33
34
  end
34
35
 
35
- super(type, name, required: required, **options)
36
+ super
36
37
  @name = name
37
38
  end
38
39
 
39
40
  # @api private
40
- def call_safe(input, &block)
41
- type.call_safe(input, &block)
42
- end
41
+ def call_safe(input, &) = type.call_safe(input, &)
43
42
 
44
43
  # @api private
45
- def call_unsafe(input)
46
- type.call_unsafe(input)
47
- end
44
+ def call_unsafe(input) = type.call_unsafe(input)
48
45
 
49
46
  # @see Dry::Types::Nominal#try
50
47
  #
51
48
  # @api public
52
- def try(input, &block)
53
- type.try(input, &block)
54
- end
49
+ def try(input, &) = type.try(input, &)
55
50
 
56
51
  # Whether the key is required in schema input
57
52
  #
58
53
  # @return [Boolean]
59
54
  #
60
55
  # @api public
61
- def required?
62
- options.fetch(:required)
63
- end
56
+ def required? = options.fetch(:required)
64
57
 
65
58
  # Control whether the key is required
66
59
  #
@@ -87,27 +80,21 @@ module Dry
87
80
  # @return [Dry::Types::Schema::Key]
88
81
  #
89
82
  # @api public
90
- def omittable
91
- required(false)
92
- end
83
+ def omittable = required(false)
93
84
 
94
85
  # Turn key into a lax type. Lax types are not strict hence such keys are not required
95
86
  #
96
87
  # @return [Lax]
97
88
  #
98
89
  # @api public
99
- def lax
100
- __new__(type.lax).required(false)
101
- end
90
+ def lax = __new__(type.lax).required(false)
102
91
 
103
92
  # Make wrapped type optional
104
93
  #
105
94
  # @return [Key]
106
95
  #
107
96
  # @api public
108
- def optional
109
- __new__(type.optional)
110
- end
97
+ def optional = __new__(type.optional)
111
98
 
112
99
  # Dump to internal AST representation
113
100
  #
@@ -144,9 +131,7 @@ module Dry
144
131
  private
145
132
 
146
133
  # @api private
147
- def decorate?(response)
148
- response.is_a?(Type)
149
- end
134
+ def decorate?(response) = response.is_a?(Type)
150
135
  end
151
136
  end
152
137
  end
@@ -17,8 +17,8 @@ module Dry
17
17
  #
18
18
  # @api public
19
19
  class Schema < Hash
20
- NO_TRANSFORM = Dry::Types::FnContainer.register { |x| x }
21
- SYMBOLIZE_KEY = Dry::Types::FnContainer.register(:to_sym.to_proc)
20
+ NO_TRANSFORM = ::Dry::Types::FnContainer.register { |x| x }
21
+ SYMBOLIZE_KEY = ::Dry::Types::FnContainer.register(:to_sym.to_proc)
22
22
 
23
23
  include ::Enumerable
24
24
 
@@ -46,7 +46,7 @@ module Dry
46
46
 
47
47
  key_fn = options.fetch(:key_transform_fn, NO_TRANSFORM)
48
48
 
49
- @transform_key = Dry::Types::FnContainer[key_fn]
49
+ @transform_key = ::Dry::Types::FnContainer[key_fn]
50
50
 
51
51
  super
52
52
  end
@@ -77,9 +77,7 @@ module Dry
77
77
  # @return [Hash{Symbol => Object}]
78
78
  #
79
79
  # @api public
80
- def apply(hash, options = EMPTY_HASH)
81
- call_unsafe(hash, options)
82
- end
80
+ def apply(hash, options = EMPTY_HASH) = call_unsafe(hash, options)
83
81
 
84
82
  # @param input [Hash] hash
85
83
  #
@@ -184,9 +182,9 @@ module Dry
184
182
  def with_key_transform(proc = nil, &block)
185
183
  fn = proc || block
186
184
 
187
- raise ArgumentError, "a block or callable argument is required" if fn.nil?
185
+ raise ::ArgumentError, "a block or callable argument is required" if fn.nil?
188
186
 
189
- handle = Dry::Types::FnContainer.register(fn)
187
+ handle = ::Dry::Types::FnContainer.register(fn)
190
188
  with(key_transform_fn: handle)
191
189
  end
192
190
 
@@ -226,8 +224,8 @@ module Dry
226
224
  # @return [Array<Dry::Types::Schema::Key>,Enumerator]
227
225
  #
228
226
  # @api public
229
- def each(&block)
230
- keys.each(&block)
227
+ def each(&)
228
+ keys.each(&)
231
229
  end
232
230
 
233
231
  # Whether the schema has the given key
@@ -256,9 +254,9 @@ module Dry
256
254
  # @return [Dry::Types::Schema::Key,Object] key type or block value if key is not in schema
257
255
  #
258
256
  # @api public
259
- def key(name, fallback = Undefined, &block)
257
+ def key(name, fallback = Undefined, &)
260
258
  if Undefined.equal?(fallback)
261
- name_key_map.fetch(name, &block)
259
+ name_key_map.fetch(name, &)
262
260
  else
263
261
  name_key_map.fetch(name, fallback)
264
262
  end
@@ -31,7 +31,7 @@ RSpec.shared_examples_for "Dry::Types::Nominal without primitive" do
31
31
 
32
32
  describe "#==" do
33
33
  it "has #== defined" do
34
- expect(type).to eq(type)
34
+ expect(type).to eql(type)
35
35
  end
36
36
  end
37
37
 
@@ -74,7 +74,7 @@ RSpec.shared_examples_for "Dry::Types::Nominal#meta" do
74
74
  end
75
75
 
76
76
  it "is locally immutable" do
77
- expect(type.meta).to be_a ::Hash
77
+ expect(type.meta).to be_a Hash
78
78
  expect(type.meta).to be_frozen
79
79
  expect(type.meta).not_to have_key :immutable_test
80
80
  derived = type.meta(immutable_test: 1)
data/lib/dry/types/sum.rb CHANGED
@@ -8,16 +8,12 @@ module Dry
8
8
  class Sum
9
9
  include Composition
10
10
 
11
- def self.operator
12
- :|
13
- end
11
+ def self.operator = :|
14
12
 
15
13
  # @return [Boolean]
16
14
  #
17
15
  # @api public
18
- def optional?
19
- primitive?(nil)
20
- end
16
+ def optional? = primitive?(nil)
21
17
 
22
18
  # @param [Object] input
23
19
  #
@@ -84,9 +80,9 @@ module Dry
84
80
  # @see Builder#constrained
85
81
  #
86
82
  # @api public
87
- def constrained(options)
83
+ def constrained(...)
88
84
  if optional?
89
- right.constrained(options).optional
85
+ right.constrained(...).optional
90
86
  else
91
87
  super
92
88
  end
@@ -40,9 +40,9 @@ module Dry
40
40
  # @return [Object]
41
41
  #
42
42
  # @api public
43
- def call(input = Undefined, &block)
43
+ def call(input = Undefined, &)
44
44
  if block_given?
45
- call_safe(input, &block)
45
+ call_safe(input, &)
46
46
  else
47
47
  call_unsafe(input)
48
48
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Types
5
- VERSION = "1.7.2"
5
+ VERSION = "1.8.0"
6
6
  end
7
7
  end
data/lib/dry/types.rb CHANGED
@@ -165,8 +165,8 @@ module Dry
165
165
 
166
166
  if container.keys.any? { |key| key.split(".")[0] == underscored }
167
167
  raise ::NameError,
168
- "dry-types does not define constants for default types. "\
169
- 'You can access the predefined types with [], e.g. Dry::Types["integer"] '\
168
+ "dry-types does not define constants for default types. " \
169
+ "You can access the predefined types with [], e.g. Dry::Types['integer'] " \
170
170
  "or generate a module with types using Dry.Types()"
171
171
  else
172
172
  super
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-types
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.2
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-05 00:00:00.000000000 Z
11
+ date: 2025-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bigdecimal
@@ -94,62 +94,6 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '2.6'
97
- - !ruby/object:Gem::Dependency
98
- name: bundler
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: rake
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
- - !ruby/object:Gem::Dependency
126
- name: rspec
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
- - !ruby/object:Gem::Dependency
140
- name: yard
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - ">="
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - ">="
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
97
  description: Type system for Ruby supporting coercions, constraints and complex types
154
98
  like structs, value objects, enums etc
155
99
  email:
@@ -226,7 +170,8 @@ metadata:
226
170
  changelog_uri: https://github.com/dry-rb/dry-types/blob/main/CHANGELOG.md
227
171
  source_code_uri: https://github.com/dry-rb/dry-types
228
172
  bug_tracker_uri: https://github.com/dry-rb/dry-types/issues
229
- post_install_message:
173
+ rubygems_mfa_required: 'true'
174
+ post_install_message:
230
175
  rdoc_options: []
231
176
  require_paths:
232
177
  - lib
@@ -234,15 +179,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
234
179
  requirements:
235
180
  - - ">="
236
181
  - !ruby/object:Gem::Version
237
- version: '3.0'
182
+ version: '3.1'
238
183
  required_rubygems_version: !ruby/object:Gem::Requirement
239
184
  requirements:
240
185
  - - ">="
241
186
  - !ruby/object:Gem::Version
242
187
  version: '0'
243
188
  requirements: []
244
- rubygems_version: 3.3.26
245
- signing_key:
189
+ rubygems_version: 3.3.27
190
+ signing_key:
246
191
  specification_version: 4
247
192
  summary: Type system for Ruby supporting coercions, constraints and complex types
248
193
  like structs, value objects, enums etc