dry-types 1.5.1 → 1.7.2
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 +4 -4
- data/CHANGELOG.md +50 -0
- data/LICENSE +1 -1
- data/README.md +7 -13
- data/dry-types.gemspec +19 -18
- data/lib/dry/types/array/constructor.rb +0 -2
- data/lib/dry/types/array/member.rb +1 -3
- data/lib/dry/types/array.rb +0 -3
- data/lib/dry/types/builder.rb +43 -14
- data/lib/dry/types/builder_methods.rb +1 -1
- data/lib/dry/types/coercions/params.rb +4 -3
- data/lib/dry/types/compat.rb +1 -0
- data/lib/dry/types/compiler.rb +1 -3
- data/lib/dry/types/composition.rb +152 -0
- data/lib/dry/types/constrained.rb +0 -5
- data/lib/dry/types/constraints.rb +3 -7
- data/lib/dry/types/constructor/function.rb +8 -9
- data/lib/dry/types/constructor.rb +4 -10
- data/lib/dry/types/container.rb +1 -3
- data/lib/dry/types/core.rb +2 -3
- data/lib/dry/types/decorator.rb +0 -2
- data/lib/dry/types/default.rb +3 -6
- data/lib/dry/types/enum.rb +0 -3
- data/lib/dry/types/errors.rb +13 -1
- data/lib/dry/types/extensions/maybe.rb +8 -5
- data/lib/dry/types/extensions/monads.rb +7 -2
- data/lib/dry/types/fn_container.rb +0 -2
- data/lib/dry/types/hash/constructor.rb +2 -4
- data/lib/dry/types/hash.rb +1 -6
- data/lib/dry/types/implication.rb +66 -0
- data/lib/dry/types/intersection.rb +108 -0
- data/lib/dry/types/lax.rb +1 -4
- data/lib/dry/types/map.rb +9 -3
- data/lib/dry/types/module.rb +18 -9
- data/lib/dry/types/nominal.rb +2 -13
- data/lib/dry/types/predicate_inferrer.rb +8 -9
- data/lib/dry/types/predicate_registry.rb +7 -7
- data/lib/dry/types/primitive_inferrer.rb +0 -2
- data/lib/dry/types/printer/composition.rb +44 -0
- data/lib/dry/types/printer.rb +116 -131
- data/lib/dry/types/result.rb +0 -2
- data/lib/dry/types/schema/key.rb +1 -4
- data/lib/dry/types/schema.rb +6 -4
- data/lib/dry/types/sum.rb +3 -95
- data/lib/dry/types/type.rb +1 -3
- data/lib/dry/types/version.rb +1 -1
- data/lib/dry/types.rb +49 -22
- metadata +32 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12b751a4799c67217dd1540dacc45a70ce51dab53441ec64e575f74a520e0eb6
|
4
|
+
data.tar.gz: e50e85513f68af8f0b98f5af707c2e82826c7daed237fba9ca014b96fc4e4754
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7247fc6a64c97967a83de7613f298d25137c4d03ae9afa79929255cca792dcb6a9f7203f4cdefb942b662c7ec7a32d0386a005c8e6e19ecfa03d37d802d2877
|
7
|
+
data.tar.gz: 2a54fd10714b4a5a74e7026de501c9a74f4378890ed4e39f98f9b163eb23ebfd28d27d85165e18a68aa8603149df2c8d56eabef99f36d26c56f6e60676fa3e3b
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,55 @@
|
|
1
1
|
<!--- DO NOT EDIT THIS FILE - IT'S AUTOMATICALLY GENERATED VIA DEVTOOLS --->
|
2
2
|
|
3
|
+
## 1.7.2 2024-01-05
|
4
|
+
|
5
|
+
|
6
|
+
### Fixed
|
7
|
+
|
8
|
+
- Fixed BigDecimal warning due to not being required in gemspec (@bkuhlmann in #464)
|
9
|
+
|
10
|
+
|
11
|
+
[Compare v1.7.1...v1.7.2](https://github.com/dry-rb/dry-types/compare/v1.7.1...v1.7.2)
|
12
|
+
|
13
|
+
## 1.7.1 2023-02-17
|
14
|
+
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
|
18
|
+
- Warning from jruby about overwritten keyword (@flash-gordon + @klobuczek in #454)
|
19
|
+
|
20
|
+
|
21
|
+
[Compare v1.7.0...v1.7.1](https://github.com/dry-rb/dry-types/compare/v1.7.0...v1.7.1)
|
22
|
+
|
23
|
+
## 1.7.0 2022-11-04
|
24
|
+
|
25
|
+
|
26
|
+
### Changed
|
27
|
+
|
28
|
+
- This version is compatible with recently released dry-rb dependencies (@flash-gordon)
|
29
|
+
- Updated to dry-core 1.0 (@flash-gordon + @solnic)
|
30
|
+
- Dependency on dry-container was dropped (@flash-gordon)
|
31
|
+
|
32
|
+
[Compare v1.6.1...v1.7.0](https://github.com/dry-rb/dry-types/compare/v1.6.1...v1.7.0)
|
33
|
+
|
34
|
+
## 1.6.1 2022-10-15
|
35
|
+
|
36
|
+
|
37
|
+
### Changed
|
38
|
+
|
39
|
+
- Fix issues with internal const_missing and Inflector/Module constants (@flash-gordon + @solnic)
|
40
|
+
|
41
|
+
[Compare v1.6.0...v1.6.1](https://github.com/dry-rb/dry-types/compare/v1.6.0...v1.6.1)
|
42
|
+
|
43
|
+
## 1.6.0 2022-10-15
|
44
|
+
|
45
|
+
|
46
|
+
### Changed
|
47
|
+
|
48
|
+
- Optimize `PredicateRegistry` for Ruby 2.7+ (see #420 for more details) (@casperisfine)
|
49
|
+
- Use zeitwerk for auto-loading (@flash-gordon)
|
50
|
+
|
51
|
+
[Compare v1.5.1...v1.6.0](https://github.com/dry-rb/dry-types/compare/v1.5.1...v1.6.0)
|
52
|
+
|
3
53
|
## 1.5.1 2021-02-16
|
4
54
|
|
5
55
|
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,28 +1,22 @@
|
|
1
|
+
<!--- this file is synced from dry-rb/template-gem project -->
|
2
|
+
|
1
3
|
[gem]: https://rubygems.org/gems/dry-types
|
2
4
|
[actions]: https://github.com/dry-rb/dry-types/actions
|
3
|
-
[codacy]: https://www.codacy.com/gh/dry-rb/dry-types
|
4
|
-
[chat]: https://dry-rb.zulipchat.com
|
5
|
-
[inchpages]: http://inch-ci.org/github/dry-rb/dry-types
|
6
|
-
|
7
|
-
# dry-types [][chat]
|
8
5
|
|
9
|
-
[][gem]
|
10
|
-
[][actions]
|
11
|
-
[][codacy]
|
12
|
-
[][codacy]
|
13
|
-
[][inchpages]
|
6
|
+
# dry-types [][gem] [][actions]
|
14
7
|
|
15
8
|
## Links
|
16
9
|
|
17
|
-
* [User documentation](
|
10
|
+
* [User documentation](https://dry-rb.org/gems/dry-types)
|
18
11
|
* [API documentation](http://rubydoc.info/gems/dry-types)
|
12
|
+
* [Forum](https://discourse.dry-rb.org)
|
19
13
|
|
20
14
|
## Supported Ruby versions
|
21
15
|
|
22
16
|
This library officially supports the following Ruby versions:
|
23
17
|
|
24
|
-
* MRI
|
25
|
-
* jruby
|
18
|
+
* MRI `>= 3.0`
|
19
|
+
* jruby `>= 9.4` (not tested on CI)
|
26
20
|
|
27
21
|
## License
|
28
22
|
|
data/dry-types.gemspec
CHANGED
@@ -1,41 +1,42 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
# this file is managed by dry-rb/devtools project
|
3
2
|
|
4
|
-
|
3
|
+
# this file is synced from dry-rb/template-gem project
|
4
|
+
|
5
|
+
lib = File.expand_path("lib", __dir__)
|
5
6
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
-
require
|
7
|
+
require "dry/types/version"
|
7
8
|
|
8
9
|
Gem::Specification.new do |spec|
|
9
|
-
spec.name =
|
10
|
+
spec.name = "dry-types"
|
10
11
|
spec.authors = ["Piotr Solnica"]
|
11
12
|
spec.email = ["piotr.solnica@gmail.com"]
|
12
|
-
spec.license =
|
13
|
+
spec.license = "MIT"
|
13
14
|
spec.version = Dry::Types::VERSION.dup
|
14
15
|
|
15
16
|
spec.summary = "Type system for Ruby supporting coercions, constraints and complex types like structs, value objects, enums etc"
|
16
17
|
spec.description = spec.summary
|
17
|
-
spec.homepage =
|
18
|
+
spec.homepage = "https://dry-rb.org/gems/dry-types"
|
18
19
|
spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-types.gemspec", "lib/**/*"]
|
19
|
-
spec.bindir =
|
20
|
+
spec.bindir = "bin"
|
20
21
|
spec.executables = []
|
21
|
-
spec.require_paths = [
|
22
|
+
spec.require_paths = ["lib"]
|
22
23
|
|
23
|
-
spec.metadata[
|
24
|
-
spec.metadata[
|
25
|
-
spec.metadata[
|
26
|
-
spec.metadata[
|
24
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
25
|
+
spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-types/blob/main/CHANGELOG.md"
|
26
|
+
spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-types"
|
27
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-types/issues"
|
27
28
|
|
28
|
-
spec.required_ruby_version = ">=
|
29
|
+
spec.required_ruby_version = ">= 3.0"
|
29
30
|
|
30
31
|
# to update dependencies edit project.yml
|
32
|
+
spec.add_runtime_dependency "bigdecimal", "~> 3.0"
|
31
33
|
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
|
32
|
-
spec.add_runtime_dependency "dry-
|
33
|
-
spec.add_runtime_dependency "dry-
|
34
|
-
spec.add_runtime_dependency "dry-
|
35
|
-
spec.add_runtime_dependency "
|
34
|
+
spec.add_runtime_dependency "dry-core", "~> 1.0"
|
35
|
+
spec.add_runtime_dependency "dry-inflector", "~> 1.0"
|
36
|
+
spec.add_runtime_dependency "dry-logic", "~> 1.4"
|
37
|
+
spec.add_runtime_dependency "zeitwerk", "~> 2.6"
|
36
38
|
|
37
39
|
spec.add_development_dependency "bundler"
|
38
|
-
spec.add_development_dependency "dry-monads", "~> 1.0"
|
39
40
|
spec.add_development_dependency "rake"
|
40
41
|
spec.add_development_dependency "rspec"
|
41
42
|
spec.add_development_dependency "yard"
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/types/array/constructor"
|
4
|
-
|
5
3
|
module Dry
|
6
4
|
module Types
|
7
5
|
class Array < Nominal
|
@@ -72,7 +70,7 @@ module Dry
|
|
72
70
|
# @return [Result,Logic::Result]
|
73
71
|
#
|
74
72
|
# @api public
|
75
|
-
def try(input, &block)
|
73
|
+
def try(input, &block) # rubocop:disable Metrics/PerceivedComplexity
|
76
74
|
if primitive?(input)
|
77
75
|
output = []
|
78
76
|
|
data/lib/dry/types/array.rb
CHANGED
data/lib/dry/types/builder.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/core/deprecations"
|
4
|
-
|
5
3
|
module Dry
|
6
4
|
module Types
|
7
5
|
# Common API for building types and composition
|
8
6
|
#
|
9
7
|
# @api public
|
8
|
+
# rubocop:disable Metrics/ModuleLength
|
10
9
|
module Builder
|
11
10
|
include Dry::Core::Constants
|
12
11
|
|
@@ -32,8 +31,29 @@ module Dry
|
|
32
31
|
#
|
33
32
|
# @api private
|
34
33
|
def |(other)
|
35
|
-
|
36
|
-
|
34
|
+
compose(other, Sum)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Compose two types into an Intersection type
|
38
|
+
#
|
39
|
+
# @param [Type] other
|
40
|
+
#
|
41
|
+
# @return [Intersection, Intersection::Constrained]
|
42
|
+
#
|
43
|
+
# @api private
|
44
|
+
def &(other)
|
45
|
+
compose(other, Intersection)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Compose two types into an Implication type
|
49
|
+
#
|
50
|
+
# @param [Type] other
|
51
|
+
#
|
52
|
+
# @return [Implication, Implication::Constrained]
|
53
|
+
#
|
54
|
+
# @api private
|
55
|
+
def >(other)
|
56
|
+
compose(other, Implication)
|
37
57
|
end
|
38
58
|
|
39
59
|
# Turn a type into an optional type
|
@@ -76,7 +96,7 @@ module Dry
|
|
76
96
|
" value every time. Call `.freeze` when setting the default"\
|
77
97
|
" or pass `shared: true` to discard this warning."\
|
78
98
|
"\n#{where}",
|
79
|
-
tag: :
|
99
|
+
tag: :"dry-types"
|
80
100
|
)
|
81
101
|
end
|
82
102
|
|
@@ -105,7 +125,7 @@ module Dry
|
|
105
125
|
if values.length == 1 && values[0].is_a?(::Hash)
|
106
126
|
values[0]
|
107
127
|
else
|
108
|
-
|
128
|
+
values.zip(values).to_h
|
109
129
|
end
|
110
130
|
|
111
131
|
Enum.new(constrained(included_in: mapping.keys), mapping: mapping)
|
@@ -147,7 +167,7 @@ module Dry
|
|
147
167
|
# @return [Constructor]
|
148
168
|
#
|
149
169
|
# @api public
|
150
|
-
def fallback(value = Undefined, shared: false, &_fallback)
|
170
|
+
def fallback(value = Undefined, shared: false, &_fallback) # rubocop:disable Metrics/PerceivedComplexity
|
151
171
|
if Undefined.equal?(value) && !block_given?
|
152
172
|
raise ::ArgumentError, "fallback value or a block must be given"
|
153
173
|
end
|
@@ -167,7 +187,7 @@ module Dry
|
|
167
187
|
" value every time. Call `.freeze` when setting the fallback"\
|
168
188
|
" or pass `shared: true` to discard this warning."\
|
169
189
|
"\n#{where}",
|
170
|
-
tag: :
|
190
|
+
tag: :"dry-types"
|
171
191
|
)
|
172
192
|
end
|
173
193
|
|
@@ -181,12 +201,21 @@ module Dry
|
|
181
201
|
end
|
182
202
|
end
|
183
203
|
end
|
204
|
+
|
205
|
+
private
|
206
|
+
|
207
|
+
# @api private
|
208
|
+
def compose(other, composition_class)
|
209
|
+
klass =
|
210
|
+
if constrained? && other.constrained?
|
211
|
+
composition_class::Constrained
|
212
|
+
else
|
213
|
+
composition_class
|
214
|
+
end
|
215
|
+
|
216
|
+
klass.new(self, other)
|
217
|
+
end
|
184
218
|
end
|
219
|
+
# rubocop:enable Metrics/ModuleLength
|
185
220
|
end
|
186
221
|
end
|
187
|
-
|
188
|
-
require "dry/types/default"
|
189
|
-
require "dry/types/constrained"
|
190
|
-
require "dry/types/enum"
|
191
|
-
require "dry/types/lax"
|
192
|
-
require "dry/types/sum"
|
@@ -80,7 +80,7 @@ module Dry
|
|
80
80
|
# @param [#call,nil] block Value constructor
|
81
81
|
#
|
82
82
|
# @return [Dry::Types::Type]
|
83
|
-
def Constructor(klass, cons = nil, &block)
|
83
|
+
def Constructor(klass, cons = nil, &block) # rubocop:disable Metrics/PerceivedComplexity:
|
84
84
|
if klass.is_a?(Type)
|
85
85
|
if cons || block
|
86
86
|
klass.constructor(cons || block)
|
@@ -12,9 +12,10 @@ module Dry
|
|
12
12
|
module Params
|
13
13
|
TRUE_VALUES = %w[1 on On ON t true True TRUE T y yes Yes YES Y].freeze
|
14
14
|
FALSE_VALUES = %w[0 off Off OFF f false False FALSE F n no No NO N].freeze
|
15
|
-
BOOLEAN_MAP =
|
16
|
-
|
17
|
-
|
15
|
+
BOOLEAN_MAP = EMPTY_HASH.merge(
|
16
|
+
[true, *TRUE_VALUES].to_h { |v| [v, true] },
|
17
|
+
[false, *FALSE_VALUES].to_h { |v| [v, false] }
|
18
|
+
).freeze
|
18
19
|
|
19
20
|
extend Coercions
|
20
21
|
|
data/lib/dry/types/compat.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
# frozen_string_literal: true
|
data/lib/dry/types/compiler.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/core/deprecations"
|
4
|
-
|
5
3
|
module Dry
|
6
4
|
module Types
|
7
5
|
# @api private
|
8
6
|
class Compiler
|
9
|
-
extend ::Dry::Core::Deprecations[:
|
7
|
+
extend ::Dry::Core::Deprecations[:"dry-types"]
|
10
8
|
|
11
9
|
attr_reader :registry
|
12
10
|
|
@@ -0,0 +1,152 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/core/equalizer"
|
4
|
+
require "dry/types/options"
|
5
|
+
require "dry/types/meta"
|
6
|
+
|
7
|
+
module Dry
|
8
|
+
module Types
|
9
|
+
module Composition
|
10
|
+
include Type
|
11
|
+
include Builder
|
12
|
+
include Options
|
13
|
+
include Meta
|
14
|
+
include Printable
|
15
|
+
include Dry::Equalizer(:left, :right, :options, :meta, inspect: false, immutable: true)
|
16
|
+
|
17
|
+
# @return [Type]
|
18
|
+
attr_reader :left
|
19
|
+
|
20
|
+
# @return [Type]
|
21
|
+
attr_reader :right
|
22
|
+
|
23
|
+
module Constrained
|
24
|
+
def rule
|
25
|
+
left.rule.public_send(self.class.operator, right.rule)
|
26
|
+
end
|
27
|
+
|
28
|
+
def constrained?
|
29
|
+
true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.included(base)
|
34
|
+
composition_name = Inflector.demodulize(base)
|
35
|
+
ast_type = Inflector.underscore(composition_name).to_sym
|
36
|
+
base.define_singleton_method(:ast_type) { ast_type }
|
37
|
+
base.define_singleton_method(:composition_name) { composition_name }
|
38
|
+
base.const_set("Constrained", Class.new(base) { include Constrained })
|
39
|
+
end
|
40
|
+
|
41
|
+
# @param [Type] left
|
42
|
+
# @param [Type] right
|
43
|
+
# @param [Hash] options
|
44
|
+
#
|
45
|
+
# @api private
|
46
|
+
def initialize(left, right, **options)
|
47
|
+
super
|
48
|
+
@left, @right = left, right
|
49
|
+
freeze
|
50
|
+
end
|
51
|
+
|
52
|
+
# @return [String]
|
53
|
+
#
|
54
|
+
# @api public
|
55
|
+
def name
|
56
|
+
[left, right].map(&:name).join(" #{self.class.operator} ")
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [false]
|
60
|
+
#
|
61
|
+
# @api public
|
62
|
+
def default?
|
63
|
+
false
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [false]
|
67
|
+
#
|
68
|
+
# @api public
|
69
|
+
def constrained?
|
70
|
+
false
|
71
|
+
end
|
72
|
+
|
73
|
+
# @return [Boolean]
|
74
|
+
#
|
75
|
+
# @api public
|
76
|
+
def optional?
|
77
|
+
false
|
78
|
+
end
|
79
|
+
|
80
|
+
# @param [Object] input
|
81
|
+
#
|
82
|
+
# @return [Object]
|
83
|
+
#
|
84
|
+
# @api private
|
85
|
+
def call_unsafe(input)
|
86
|
+
raise NotImplementedError
|
87
|
+
end
|
88
|
+
|
89
|
+
# @param [Object] input
|
90
|
+
#
|
91
|
+
# @return [Object]
|
92
|
+
#
|
93
|
+
# @api private
|
94
|
+
def call_safe(input, &block)
|
95
|
+
raise NotImplementedError
|
96
|
+
end
|
97
|
+
|
98
|
+
# @param [Object] input
|
99
|
+
#
|
100
|
+
# @api public
|
101
|
+
def try(input)
|
102
|
+
raise NotImplementedError
|
103
|
+
end
|
104
|
+
|
105
|
+
# @api private
|
106
|
+
def success(input)
|
107
|
+
result = try(input)
|
108
|
+
if result.success?
|
109
|
+
result
|
110
|
+
else
|
111
|
+
raise ArgumentError, "Invalid success value '#{input}' for #{inspect}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# @api private
|
116
|
+
def failure(input, _error = nil)
|
117
|
+
result = try(input)
|
118
|
+
if result.failure?
|
119
|
+
result
|
120
|
+
else
|
121
|
+
raise ArgumentError, "Invalid failure value '#{input}' for #{inspect}"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# @param [Object] value
|
126
|
+
#
|
127
|
+
# @return [Boolean]
|
128
|
+
#
|
129
|
+
# @api private
|
130
|
+
def primitive?(value)
|
131
|
+
raise NotImplementedError
|
132
|
+
end
|
133
|
+
|
134
|
+
# @see Nominal#to_ast
|
135
|
+
#
|
136
|
+
# @api public
|
137
|
+
def to_ast(meta: true)
|
138
|
+
[self.class.ast_type,
|
139
|
+
[left.to_ast(meta: meta), right.to_ast(meta: meta), meta ? self.meta : EMPTY_HASH]]
|
140
|
+
end
|
141
|
+
|
142
|
+
# Wrap the type with a proc
|
143
|
+
#
|
144
|
+
# @return [Proc]
|
145
|
+
#
|
146
|
+
# @api public
|
147
|
+
def to_proc
|
148
|
+
proc { |value| self.(value) }
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -1,9 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/logic/rule_compiler"
|
4
|
-
require "dry/logic/predicates"
|
5
|
-
require "dry/logic/rule/predicate"
|
6
|
-
|
7
3
|
module Dry
|
8
4
|
# Helper methods for constraint types
|
9
5
|
#
|
@@ -17,8 +13,8 @@ module Dry
|
|
17
13
|
def self.Rule(options)
|
18
14
|
rule_compiler.(
|
19
15
|
options.map { |key, val|
|
20
|
-
Logic::Rule::Predicate.build(
|
21
|
-
Logic::Predicates[:"#{key}?"]
|
16
|
+
::Dry::Logic::Rule::Predicate.build(
|
17
|
+
::Dry::Logic::Predicates[:"#{key}?"]
|
22
18
|
).curry(val).to_ast
|
23
19
|
}
|
24
20
|
).reduce(:and)
|
@@ -28,7 +24,7 @@ module Dry
|
|
28
24
|
#
|
29
25
|
# @api private
|
30
26
|
def self.rule_compiler
|
31
|
-
@rule_compiler ||= Logic::RuleCompiler.new(Logic::Predicates)
|
27
|
+
@rule_compiler ||= ::Dry::Logic::RuleCompiler.new(::Dry::Logic::Predicates)
|
32
28
|
end
|
33
29
|
end
|
34
30
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/core/equalizer"
|
4
3
|
require "concurrent/map"
|
5
4
|
|
6
5
|
module Dry
|
@@ -62,17 +61,17 @@ module Dry
|
|
62
61
|
::Module.new do
|
63
62
|
if safe
|
64
63
|
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
65
|
-
def call(input, &block)
|
66
|
-
@target.#{method}(input, &block)
|
67
|
-
end
|
64
|
+
def call(input, &block) # def call(input, &block)
|
65
|
+
@target.#{method}(input, &block) # @target.coerve(input, &block)
|
66
|
+
end # end
|
68
67
|
RUBY
|
69
68
|
else
|
70
69
|
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
71
|
-
def call(input, &block)
|
72
|
-
@target.#{method}(input)
|
73
|
-
rescue ::NoMethodError, ::TypeError, ::ArgumentError => error
|
74
|
-
CoercionError.handle(error, &block)
|
75
|
-
end
|
70
|
+
def call(input, &block) # def call(input, &block)
|
71
|
+
@target.#{method}(input) # @target.coerce(input)
|
72
|
+
rescue ::NoMethodError, ::TypeError, ::ArgumentError => error # rescue ::NoMethodError, ::TypeError, ::ArgumentError => error
|
73
|
+
CoercionError.handle(error, &block) # CoercionError.handle(error, &block)
|
74
|
+
end # end
|
76
75
|
RUBY
|
77
76
|
end
|
78
77
|
end
|
@@ -1,10 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/core/equalizer"
|
4
|
-
require "dry/types/fn_container"
|
5
|
-
require "dry/types/constructor/function"
|
6
|
-
require "dry/types/constructor/wrapper"
|
7
|
-
|
8
3
|
module Dry
|
9
4
|
module Types
|
10
5
|
# Constructor types apply a function to the input that is supposed to return
|
@@ -27,9 +22,9 @@ module Dry
|
|
27
22
|
# @param [#call, nil] block
|
28
23
|
#
|
29
24
|
# @api public
|
30
|
-
def self.new(input, **options, &block)
|
25
|
+
def self.new(input, fn: Undefined, **options, &block)
|
31
26
|
type = input.is_a?(Builder) ? input : Nominal.new(input)
|
32
|
-
super(type, **options, fn: Function[
|
27
|
+
super(type, **options, fn: Function[Undefined.default(fn, block)])
|
33
28
|
end
|
34
29
|
|
35
30
|
# @param [Builder, Object] input
|
@@ -49,13 +44,12 @@ module Dry
|
|
49
44
|
|
50
45
|
# @api private
|
51
46
|
def self.wrapper_type
|
52
|
-
@wrapper_type ||=
|
47
|
+
@wrapper_type ||=
|
53
48
|
if self < Wrapper
|
54
49
|
self
|
55
50
|
else
|
56
51
|
const_set(:Wrapping, ::Class.new(self).include(Wrapper))
|
57
52
|
end
|
58
|
-
end
|
59
53
|
end
|
60
54
|
|
61
55
|
# Instantiate a new constructor type instance
|
@@ -191,7 +185,7 @@ module Dry
|
|
191
185
|
if type.respond_to?(method)
|
192
186
|
response = type.public_send(method, *args, &block)
|
193
187
|
|
194
|
-
if response.is_a?(Type) &&
|
188
|
+
if response.is_a?(Type) && response.instance_of?(type.class)
|
195
189
|
response.constructor_type[response, **options]
|
196
190
|
else
|
197
191
|
response
|
data/lib/dry/types/container.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/container"
|
4
|
-
|
5
3
|
module Dry
|
6
4
|
module Types
|
7
5
|
# Internal container for the built-in types
|
8
6
|
#
|
9
7
|
# @api private
|
10
8
|
class Container
|
11
|
-
include
|
9
|
+
include Core::Container::Mixin
|
12
10
|
end
|
13
11
|
end
|
14
12
|
end
|
data/lib/dry/types/core.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "dry/types/any"
|
4
|
-
|
5
3
|
module Dry
|
6
4
|
module Types
|
7
5
|
# Primitives with {Kernel} coercion methods
|
@@ -60,7 +58,8 @@ module Dry
|
|
60
58
|
|
61
59
|
# Register {KERNEL_COERCIBLE} types
|
62
60
|
KERNEL_COERCIBLE.each do |name, primitive|
|
63
|
-
register("coercible.#{name}",
|
61
|
+
register("coercible.#{name}",
|
62
|
+
self["nominal.#{name}"].constructor(Kernel.method(primitive.name)))
|
64
63
|
end
|
65
64
|
|
66
65
|
# Register {METHOD_COERCIBLE} types
|