dry-types 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +10 -0
  3. data/.github/ISSUE_TEMPLATE/---bug-report.md +34 -0
  4. data/.github/ISSUE_TEMPLATE/---feature-request.md +18 -0
  5. data/.travis.yml +10 -4
  6. data/CHANGELOG.md +68 -0
  7. data/Gemfile +8 -6
  8. data/README.md +2 -2
  9. data/docsite/source/array-with-member.html.md +13 -0
  10. data/docsite/source/built-in-types.html.md +116 -0
  11. data/docsite/source/constraints.html.md +31 -0
  12. data/docsite/source/custom-types.html.md +93 -0
  13. data/docsite/source/default-values.html.md +91 -0
  14. data/docsite/source/enum.html.md +69 -0
  15. data/docsite/source/getting-started.html.md +57 -0
  16. data/docsite/source/hash-schemas.html.md +169 -0
  17. data/docsite/source/index.html.md +155 -0
  18. data/docsite/source/map.html.md +17 -0
  19. data/docsite/source/optional-values.html.md +96 -0
  20. data/docsite/source/sum.html.md +21 -0
  21. data/lib/dry/types.rb +7 -2
  22. data/lib/dry/types/array/member.rb +1 -1
  23. data/lib/dry/types/builder_methods.rb +18 -8
  24. data/lib/dry/types/constructor.rb +2 -29
  25. data/lib/dry/types/decorator.rb +1 -1
  26. data/lib/dry/types/extensions.rb +4 -0
  27. data/lib/dry/types/extensions/monads.rb +29 -0
  28. data/lib/dry/types/hash.rb +2 -1
  29. data/lib/dry/types/lax.rb +1 -1
  30. data/lib/dry/types/params.rb +5 -0
  31. data/lib/dry/types/predicate_inferrer.rb +197 -0
  32. data/lib/dry/types/predicate_registry.rb +34 -0
  33. data/lib/dry/types/primitive_inferrer.rb +97 -0
  34. data/lib/dry/types/printer.rb +7 -3
  35. data/lib/dry/types/version.rb +1 -1
  36. metadata +48 -28
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/logic/predicates'
4
+
5
+ module Dry
6
+ module Types
7
+ # A registry with predicate objects from `Dry::Logic::Predicates`
8
+ #
9
+ # @api private
10
+ class PredicateRegistry
11
+ # @api private
12
+ attr_reader :predicates
13
+
14
+ # @api private
15
+ attr_reader :has_predicate
16
+
17
+ # @api private
18
+ def initialize(predicates = Logic::Predicates)
19
+ @predicates = predicates
20
+ @has_predicate = ::Kernel.instance_method(:respond_to?).bind(@predicates)
21
+ end
22
+
23
+ # @api private
24
+ def [](name)
25
+ predicates[name]
26
+ end
27
+
28
+ # @api private
29
+ def key?(name)
30
+ has_predicate.(name)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/core/cache'
4
+
5
+ module Dry
6
+ module Types
7
+ # PrimitiveInferrer returns the list of classes matching a type.
8
+ #
9
+ # @api public
10
+ class PrimitiveInferrer
11
+ extend Core::Cache
12
+
13
+ # Compiler reduces type AST into a list of primitives
14
+ #
15
+ # @api private
16
+ class Compiler
17
+ # @api private
18
+ def visit(node)
19
+ meth, rest = node
20
+ public_send(:"visit_#{meth}", rest)
21
+ end
22
+
23
+ # @api private
24
+ def visit_nominal(node)
25
+ type, _ = node
26
+ type
27
+ end
28
+
29
+ # @api private
30
+ def visit_hash(_)
31
+ ::Hash
32
+ end
33
+ alias_method :visit_schema, :visit_hash
34
+
35
+ # @api private
36
+ def visit_array(_)
37
+ ::Array
38
+ end
39
+
40
+ # @api private
41
+ def visit_lax(node)
42
+ visit(node)
43
+ end
44
+
45
+ # @api private
46
+ def visit_constructor(node)
47
+ other, * = node
48
+ visit(other)
49
+ end
50
+
51
+ # @api private
52
+ def visit_enum(node)
53
+ other, * = node
54
+ visit(other)
55
+ end
56
+
57
+ # @api private
58
+ def visit_sum(node)
59
+ left, right = node
60
+
61
+ [visit(left), visit(right)].flatten(1)
62
+ end
63
+
64
+ # @api private
65
+ def visit_constrained(node)
66
+ other, * = node
67
+ visit(other)
68
+ end
69
+
70
+ # @api private
71
+ def visit_any(_)
72
+ ::Object
73
+ end
74
+ end
75
+
76
+ # @return [Compiler]
77
+ # @api private
78
+ attr_reader :compiler
79
+
80
+ # @api private
81
+ def initialize
82
+ @compiler = Compiler.new
83
+ end
84
+
85
+ # Infer primitives from the provided type
86
+ #
87
+ # @return [Array[Class]]
88
+ #
89
+ # @api private
90
+ def [](type)
91
+ self.class.fetch_or_store(type) do
92
+ Array(compiler.visit(type.to_ast)).freeze
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -47,13 +47,17 @@ module Dry
47
47
  yield 'Any'
48
48
  end
49
49
 
50
- def visit_array(_)
51
- yield 'Array'
50
+ def visit_array(type)
51
+ visit_options(EMPTY_HASH, type.meta) do |opts|
52
+ yield "Array#{opts}"
53
+ end
52
54
  end
53
55
 
54
56
  def visit_array_member(array)
55
57
  visit(array.member) do |type|
56
- yield "Array<#{type}>"
58
+ visit_options(EMPTY_HASH, array.meta) do |opts|
59
+ yield "Array<#{type}#{opts}>"
60
+ end
57
61
  end
58
62
  end
59
63
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Types
5
- VERSION = '1.1.0'
5
+ VERSION = '1.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,45 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-types
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-02 00:00:00.000000000 Z
11
+ date: 2019-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: concurrent-ruby
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
16
  - - "~>"
18
17
  - !ruby/object:Gem::Version
19
18
  version: '1.0'
20
- type: :runtime
19
+ name: concurrent-ruby
21
20
  prerelease: false
21
+ type: :runtime
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: dry-container
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
30
  - - "~>"
32
31
  - !ruby/object:Gem::Version
33
32
  version: '0.3'
34
- type: :runtime
33
+ name: dry-container
35
34
  prerelease: false
35
+ type: :runtime
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.3'
41
41
  - !ruby/object:Gem::Dependency
42
- name: dry-core
43
42
  requirement: !ruby/object:Gem::Requirement
44
43
  requirements:
45
44
  - - "~>"
@@ -48,8 +47,9 @@ dependencies:
48
47
  - - ">="
49
48
  - !ruby/object:Gem::Version
50
49
  version: 0.4.4
51
- type: :runtime
50
+ name: dry-core
52
51
  prerelease: false
52
+ type: :runtime
53
53
  version_requirements: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - "~>"
@@ -59,7 +59,6 @@ dependencies:
59
59
  - !ruby/object:Gem::Version
60
60
  version: 0.4.4
61
61
  - !ruby/object:Gem::Dependency
62
- name: dry-equalizer
63
62
  requirement: !ruby/object:Gem::Requirement
64
63
  requirements:
65
64
  - - "~>"
@@ -68,8 +67,9 @@ dependencies:
68
67
  - - ">="
69
68
  - !ruby/object:Gem::Version
70
69
  version: 0.2.2
71
- type: :runtime
70
+ name: dry-equalizer
72
71
  prerelease: false
72
+ type: :runtime
73
73
  version_requirements: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - "~>"
@@ -79,7 +79,6 @@ dependencies:
79
79
  - !ruby/object:Gem::Version
80
80
  version: 0.2.2
81
81
  - !ruby/object:Gem::Dependency
82
- name: dry-inflector
83
82
  requirement: !ruby/object:Gem::Requirement
84
83
  requirements:
85
84
  - - "~>"
@@ -88,8 +87,9 @@ dependencies:
88
87
  - - ">="
89
88
  - !ruby/object:Gem::Version
90
89
  version: 0.1.2
91
- type: :runtime
90
+ name: dry-inflector
92
91
  prerelease: false
92
+ type: :runtime
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - "~>"
@@ -99,7 +99,6 @@ dependencies:
99
99
  - !ruby/object:Gem::Version
100
100
  version: 0.1.2
101
101
  - !ruby/object:Gem::Dependency
102
- name: dry-logic
103
102
  requirement: !ruby/object:Gem::Requirement
104
103
  requirements:
105
104
  - - "~>"
@@ -108,8 +107,9 @@ dependencies:
108
107
  - - ">="
109
108
  - !ruby/object:Gem::Version
110
109
  version: 1.0.2
111
- type: :runtime
110
+ name: dry-logic
112
111
  prerelease: false
112
+ type: :runtime
113
113
  version_requirements: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
@@ -119,70 +119,70 @@ dependencies:
119
119
  - !ruby/object:Gem::Version
120
120
  version: 1.0.2
121
121
  - !ruby/object:Gem::Dependency
122
- name: bundler
123
122
  requirement: !ruby/object:Gem::Requirement
124
123
  requirements:
125
124
  - - ">="
126
125
  - !ruby/object:Gem::Version
127
126
  version: '0'
128
- type: :development
127
+ name: bundler
129
128
  prerelease: false
129
+ type: :development
130
130
  version_requirements: !ruby/object:Gem::Requirement
131
131
  requirements:
132
132
  - - ">="
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
135
  - !ruby/object:Gem::Dependency
136
- name: dry-monads
137
136
  requirement: !ruby/object:Gem::Requirement
138
137
  requirements:
139
138
  - - "~>"
140
139
  - !ruby/object:Gem::Version
141
140
  version: '0.2'
142
- type: :development
141
+ name: dry-monads
143
142
  prerelease: false
143
+ type: :development
144
144
  version_requirements: !ruby/object:Gem::Requirement
145
145
  requirements:
146
146
  - - "~>"
147
147
  - !ruby/object:Gem::Version
148
148
  version: '0.2'
149
149
  - !ruby/object:Gem::Dependency
150
- name: rake
151
150
  requirement: !ruby/object:Gem::Requirement
152
151
  requirements:
153
152
  - - "~>"
154
153
  - !ruby/object:Gem::Version
155
154
  version: '11.0'
156
- type: :development
155
+ name: rake
157
156
  prerelease: false
157
+ type: :development
158
158
  version_requirements: !ruby/object:Gem::Requirement
159
159
  requirements:
160
160
  - - "~>"
161
161
  - !ruby/object:Gem::Version
162
162
  version: '11.0'
163
163
  - !ruby/object:Gem::Dependency
164
- name: rspec
165
164
  requirement: !ruby/object:Gem::Requirement
166
165
  requirements:
167
166
  - - "~>"
168
167
  - !ruby/object:Gem::Version
169
168
  version: '3.3'
170
- type: :development
169
+ name: rspec
171
170
  prerelease: false
171
+ type: :development
172
172
  version_requirements: !ruby/object:Gem::Requirement
173
173
  requirements:
174
174
  - - "~>"
175
175
  - !ruby/object:Gem::Version
176
176
  version: '3.3'
177
177
  - !ruby/object:Gem::Dependency
178
- name: yard
179
178
  requirement: !ruby/object:Gem::Requirement
180
179
  requirements:
181
180
  - - "~>"
182
181
  - !ruby/object:Gem::Version
183
182
  version: 0.9.5
184
- type: :development
183
+ name: yard
185
184
  prerelease: false
185
+ type: :development
186
186
  version_requirements: !ruby/object:Gem::Requirement
187
187
  requirements:
188
188
  - - "~>"
@@ -197,6 +197,9 @@ extensions: []
197
197
  extra_rdoc_files: []
198
198
  files:
199
199
  - ".codeclimate.yml"
200
+ - ".github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md"
201
+ - ".github/ISSUE_TEMPLATE/---bug-report.md"
202
+ - ".github/ISSUE_TEMPLATE/---feature-request.md"
200
203
  - ".gitignore"
201
204
  - ".rspec"
202
205
  - ".rubocop.yml"
@@ -215,6 +218,18 @@ files:
215
218
  - benchmarks/profile_valid_input.rb
216
219
  - benchmarks/schema_valid_vs_invalid.rb
217
220
  - benchmarks/setup.rb
221
+ - docsite/source/array-with-member.html.md
222
+ - docsite/source/built-in-types.html.md
223
+ - docsite/source/constraints.html.md
224
+ - docsite/source/custom-types.html.md
225
+ - docsite/source/default-values.html.md
226
+ - docsite/source/enum.html.md
227
+ - docsite/source/getting-started.html.md
228
+ - docsite/source/hash-schemas.html.md
229
+ - docsite/source/index.html.md
230
+ - docsite/source/map.html.md
231
+ - docsite/source/optional-values.html.md
232
+ - docsite/source/sum.html.md
218
233
  - dry-types.gemspec
219
234
  - lib/dry-types.rb
220
235
  - lib/dry/types.rb
@@ -242,6 +257,7 @@ files:
242
257
  - lib/dry/types/errors.rb
243
258
  - lib/dry/types/extensions.rb
244
259
  - lib/dry/types/extensions/maybe.rb
260
+ - lib/dry/types/extensions/monads.rb
245
261
  - lib/dry/types/fn_container.rb
246
262
  - lib/dry/types/hash.rb
247
263
  - lib/dry/types/hash/constructor.rb
@@ -254,6 +270,9 @@ files:
254
270
  - lib/dry/types/nominal.rb
255
271
  - lib/dry/types/options.rb
256
272
  - lib/dry/types/params.rb
273
+ - lib/dry/types/predicate_inferrer.rb
274
+ - lib/dry/types/predicate_registry.rb
275
+ - lib/dry/types/primitive_inferrer.rb
257
276
  - lib/dry/types/printable.rb
258
277
  - lib/dry/types/printer.rb
259
278
  - lib/dry/types/result.rb
@@ -272,7 +291,7 @@ metadata:
272
291
  changelog_uri: https://github.com/dry-rb/dry-types/blob/master/CHANGELOG.md
273
292
  source_code_uri: https://github.com/dry-rb/dry-types
274
293
  bug_tracker_uri: https://github.com/dry-rb/dry-types/issues
275
- post_install_message:
294
+ post_install_message:
276
295
  rdoc_options: []
277
296
  require_paths:
278
297
  - lib
@@ -287,8 +306,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
287
306
  - !ruby/object:Gem::Version
288
307
  version: '0'
289
308
  requirements: []
290
- rubygems_version: 3.0.3
291
- signing_key:
309
+ rubyforge_project:
310
+ rubygems_version: 2.7.9
311
+ signing_key:
292
312
  specification_version: 4
293
313
  summary: Type system for Ruby supporting coercions, constraints and complex types
294
314
  like structs, value objects, enums etc.