dry-types 0.12.3 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +9 -6
- data/CHANGELOG.md +92 -3
- data/CONTRIBUTING.md +1 -1
- data/Gemfile +0 -2
- data/Rakefile +4 -2
- data/benchmarks/hash_schemas.rb +2 -2
- data/dry-types.gemspec +2 -3
- data/lib/dry/types.rb +12 -18
- data/lib/dry/types/array.rb +1 -3
- data/lib/dry/types/array/member.rb +6 -2
- data/lib/dry/types/builder.rb +9 -2
- data/lib/dry/types/builder_methods.rb +22 -1
- data/lib/dry/types/coercions/{form.rb → params.rb} +1 -1
- data/lib/dry/types/compat.rb +2 -0
- data/lib/dry/types/compat/form_types.rb +27 -0
- data/lib/dry/types/compat/int.rb +13 -0
- data/lib/dry/types/compiler.rb +30 -7
- data/lib/dry/types/constructor.rb +5 -0
- data/lib/dry/types/core.rb +4 -3
- data/lib/dry/types/decorator.rb +5 -0
- data/lib/dry/types/default.rb +2 -2
- data/lib/dry/types/definition.rb +2 -12
- data/lib/dry/types/enum.rb +42 -14
- data/lib/dry/types/errors.rb +6 -2
- data/lib/dry/types/extensions/maybe.rb +18 -28
- data/lib/dry/types/fn_container.rb +9 -8
- data/lib/dry/types/hash.rb +83 -28
- data/lib/dry/types/hash/schema.rb +98 -179
- data/lib/dry/types/hash/schema_builder.rb +75 -0
- data/lib/dry/types/inflector.rb +7 -0
- data/lib/dry/types/map.rb +93 -0
- data/lib/dry/types/options.rb +3 -3
- data/lib/dry/types/params.rb +53 -0
- data/lib/dry/types/safe.rb +3 -2
- data/lib/dry/types/spec/types.rb +10 -0
- data/lib/dry/types/sum.rb +12 -6
- data/lib/dry/types/version.rb +1 -1
- metadata +29 -37
- data/lib/dry/types/form.rb +0 -53
data/lib/dry/types/options.rb
CHANGED
@@ -6,9 +6,9 @@ module Dry
|
|
6
6
|
|
7
7
|
# @see Definition#initialize
|
8
8
|
def initialize(*args, meta: EMPTY_HASH, **options)
|
9
|
-
@__args__ = args
|
10
|
-
@options = options
|
11
|
-
@meta = meta
|
9
|
+
@__args__ = args.freeze
|
10
|
+
@options = options.freeze
|
11
|
+
@meta = meta.freeze
|
12
12
|
end
|
13
13
|
|
14
14
|
# @param [Hash] new_options
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'dry/types/coercions/params'
|
2
|
+
|
3
|
+
module Dry
|
4
|
+
module Types
|
5
|
+
register('params.nil') do
|
6
|
+
self['nil'].constructor(Coercions::Params.method(:to_nil))
|
7
|
+
end
|
8
|
+
|
9
|
+
register('params.date') do
|
10
|
+
self['date'].constructor(Coercions::Params.method(:to_date))
|
11
|
+
end
|
12
|
+
|
13
|
+
register('params.date_time') do
|
14
|
+
self['date_time'].constructor(Coercions::Params.method(:to_date_time))
|
15
|
+
end
|
16
|
+
|
17
|
+
register('params.time') do
|
18
|
+
self['time'].constructor(Coercions::Params.method(:to_time))
|
19
|
+
end
|
20
|
+
|
21
|
+
register('params.true') do
|
22
|
+
self['true'].constructor(Coercions::Params.method(:to_true))
|
23
|
+
end
|
24
|
+
|
25
|
+
register('params.false') do
|
26
|
+
self['false'].constructor(Coercions::Params.method(:to_false))
|
27
|
+
end
|
28
|
+
|
29
|
+
register('params.bool') do
|
30
|
+
(self['params.true'] | self['params.false']).safe
|
31
|
+
end
|
32
|
+
|
33
|
+
register('params.integer') do
|
34
|
+
self['integer'].constructor(Coercions::Params.method(:to_int))
|
35
|
+
end
|
36
|
+
|
37
|
+
register('params.float') do
|
38
|
+
self['float'].constructor(Coercions::Params.method(:to_float))
|
39
|
+
end
|
40
|
+
|
41
|
+
register('params.decimal') do
|
42
|
+
self['decimal'].constructor(Coercions::Params.method(:to_decimal))
|
43
|
+
end
|
44
|
+
|
45
|
+
register('params.array') do
|
46
|
+
self['array'].constructor(Coercions::Params.method(:to_ary)).safe
|
47
|
+
end
|
48
|
+
|
49
|
+
register('params.hash') do
|
50
|
+
self['hash'].constructor(Coercions::Params.method(:to_hash)).safe
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/dry/types/safe.rb
CHANGED
@@ -4,9 +4,10 @@ module Dry
|
|
4
4
|
module Types
|
5
5
|
class Safe
|
6
6
|
include Type
|
7
|
-
include Dry::Equalizer(:type
|
7
|
+
include Dry::Equalizer(:type)
|
8
8
|
include Decorator
|
9
9
|
include Builder
|
10
|
+
private :options, :meta
|
10
11
|
|
11
12
|
# @param [Object] input
|
12
13
|
# @return [Object]
|
@@ -37,7 +38,7 @@ module Dry
|
|
37
38
|
#
|
38
39
|
# @see Definition#to_ast
|
39
40
|
def to_ast(meta: true)
|
40
|
-
[:safe, [type.to_ast
|
41
|
+
[:safe, [type.to_ast(meta: meta), EMPTY_HASH]]
|
41
42
|
end
|
42
43
|
|
43
44
|
# @api public
|
data/lib/dry/types/spec/types.rb
CHANGED
@@ -56,6 +56,16 @@ RSpec.shared_examples_for 'Dry::Types::Definition#meta' do
|
|
56
56
|
it 'equalizes on filled meta' do
|
57
57
|
expect(type).to_not eql(type.meta(i_am: 'different'))
|
58
58
|
end
|
59
|
+
|
60
|
+
it 'is locally immutable' do
|
61
|
+
expect(type.meta).to be_a ::Hash
|
62
|
+
expect(type.meta).to be_frozen
|
63
|
+
expect(type.meta).not_to have_key :immutable_test
|
64
|
+
derived = type.with(meta: {immutable_test: 1})
|
65
|
+
expect(derived.meta).to be_frozen
|
66
|
+
expect(derived.meta).to eql({immutable_test: 1})
|
67
|
+
expect(type.meta).not_to have_key :immutable_test
|
68
|
+
end
|
59
69
|
end
|
60
70
|
|
61
71
|
describe '#pristine' do
|
data/lib/dry/types/sum.rb
CHANGED
@@ -55,11 +55,6 @@ module Dry
|
|
55
55
|
false
|
56
56
|
end
|
57
57
|
|
58
|
-
# @return [false]
|
59
|
-
def maybe?
|
60
|
-
false
|
61
|
-
end
|
62
|
-
|
63
58
|
# @return [false]
|
64
59
|
def constrained?
|
65
60
|
false
|
@@ -101,7 +96,7 @@ module Dry
|
|
101
96
|
end
|
102
97
|
end
|
103
98
|
|
104
|
-
def failure(input)
|
99
|
+
def failure(input, _error = nil)
|
105
100
|
if !left.valid?(input)
|
106
101
|
left.failure(input, left.try(input).error)
|
107
102
|
else
|
@@ -128,6 +123,17 @@ module Dry
|
|
128
123
|
def to_ast(meta: true)
|
129
124
|
[:sum, [left.to_ast(meta: meta), right.to_ast(meta: meta), meta ? self.meta : EMPTY_HASH]]
|
130
125
|
end
|
126
|
+
|
127
|
+
# @param [Hash] options
|
128
|
+
# @return [Constrained,Sum]
|
129
|
+
# @see Builder#constrained
|
130
|
+
def constrained(options)
|
131
|
+
if optional?
|
132
|
+
right.constrained(options).optional
|
133
|
+
else
|
134
|
+
super
|
135
|
+
end
|
136
|
+
end
|
131
137
|
end
|
132
138
|
end
|
133
139
|
end
|
data/lib/dry/types/version.rb
CHANGED
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: 0.
|
4
|
+
version: 0.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -30,62 +30,68 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.4'
|
34
34
|
- - ">="
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 0.
|
36
|
+
version: 0.4.4
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
41
|
- - "~>"
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: '0.
|
43
|
+
version: '0.4'
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 0.
|
46
|
+
version: 0.4.4
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name: dry-
|
48
|
+
name: dry-inflector
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0.
|
53
|
+
version: '0.1'
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 0.1.2
|
54
57
|
type: :runtime
|
55
58
|
prerelease: false
|
56
59
|
version_requirements: !ruby/object:Gem::Requirement
|
57
60
|
requirements:
|
58
61
|
- - "~>"
|
59
62
|
- !ruby/object:Gem::Version
|
60
|
-
version: '0.
|
63
|
+
version: '0.1'
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 0.1.2
|
61
67
|
- !ruby/object:Gem::Dependency
|
62
|
-
name: dry-
|
68
|
+
name: dry-container
|
63
69
|
requirement: !ruby/object:Gem::Requirement
|
64
70
|
requirements:
|
65
71
|
- - "~>"
|
66
72
|
- !ruby/object:Gem::Version
|
67
|
-
version: '0.
|
73
|
+
version: '0.3'
|
68
74
|
type: :runtime
|
69
75
|
prerelease: false
|
70
76
|
version_requirements: !ruby/object:Gem::Requirement
|
71
77
|
requirements:
|
72
78
|
- - "~>"
|
73
79
|
- !ruby/object:Gem::Version
|
74
|
-
version: '0.
|
80
|
+
version: '0.3'
|
75
81
|
- !ruby/object:Gem::Dependency
|
76
|
-
name: dry-
|
82
|
+
name: dry-equalizer
|
77
83
|
requirement: !ruby/object:Gem::Requirement
|
78
84
|
requirements:
|
79
85
|
- - "~>"
|
80
86
|
- !ruby/object:Gem::Version
|
81
|
-
version: '0.
|
87
|
+
version: '0.2'
|
82
88
|
type: :runtime
|
83
89
|
prerelease: false
|
84
90
|
version_requirements: !ruby/object:Gem::Requirement
|
85
91
|
requirements:
|
86
92
|
- - "~>"
|
87
93
|
- !ruby/object:Gem::Version
|
88
|
-
version: '0.
|
94
|
+
version: '0.2'
|
89
95
|
- !ruby/object:Gem::Dependency
|
90
96
|
name: dry-logic
|
91
97
|
requirement: !ruby/object:Gem::Requirement
|
@@ -106,26 +112,6 @@ dependencies:
|
|
106
112
|
- - ">="
|
107
113
|
- !ruby/object:Gem::Version
|
108
114
|
version: 0.4.2
|
109
|
-
- !ruby/object:Gem::Dependency
|
110
|
-
name: inflecto
|
111
|
-
requirement: !ruby/object:Gem::Requirement
|
112
|
-
requirements:
|
113
|
-
- - "~>"
|
114
|
-
- !ruby/object:Gem::Version
|
115
|
-
version: 0.0.0
|
116
|
-
- - ">="
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
version: 0.0.2
|
119
|
-
type: :runtime
|
120
|
-
prerelease: false
|
121
|
-
version_requirements: !ruby/object:Gem::Requirement
|
122
|
-
requirements:
|
123
|
-
- - "~>"
|
124
|
-
- !ruby/object:Gem::Version
|
125
|
-
version: 0.0.0
|
126
|
-
- - ">="
|
127
|
-
- !ruby/object:Gem::Version
|
128
|
-
version: 0.0.2
|
129
115
|
- !ruby/object:Gem::Dependency
|
130
116
|
name: bundler
|
131
117
|
requirement: !ruby/object:Gem::Requirement
|
@@ -224,8 +210,11 @@ files:
|
|
224
210
|
- lib/dry/types/builder.rb
|
225
211
|
- lib/dry/types/builder_methods.rb
|
226
212
|
- lib/dry/types/coercions.rb
|
227
|
-
- lib/dry/types/coercions/form.rb
|
228
213
|
- lib/dry/types/coercions/json.rb
|
214
|
+
- lib/dry/types/coercions/params.rb
|
215
|
+
- lib/dry/types/compat.rb
|
216
|
+
- lib/dry/types/compat/form_types.rb
|
217
|
+
- lib/dry/types/compat/int.rb
|
229
218
|
- lib/dry/types/compiler.rb
|
230
219
|
- lib/dry/types/constrained.rb
|
231
220
|
- lib/dry/types/constrained/coercible.rb
|
@@ -241,11 +230,14 @@ files:
|
|
241
230
|
- lib/dry/types/extensions.rb
|
242
231
|
- lib/dry/types/extensions/maybe.rb
|
243
232
|
- lib/dry/types/fn_container.rb
|
244
|
-
- lib/dry/types/form.rb
|
245
233
|
- lib/dry/types/hash.rb
|
246
234
|
- lib/dry/types/hash/schema.rb
|
235
|
+
- lib/dry/types/hash/schema_builder.rb
|
236
|
+
- lib/dry/types/inflector.rb
|
247
237
|
- lib/dry/types/json.rb
|
238
|
+
- lib/dry/types/map.rb
|
248
239
|
- lib/dry/types/options.rb
|
240
|
+
- lib/dry/types/params.rb
|
249
241
|
- lib/dry/types/result.rb
|
250
242
|
- lib/dry/types/safe.rb
|
251
243
|
- lib/dry/types/spec/types.rb
|
data/lib/dry/types/form.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'dry/types/coercions/form'
|
2
|
-
|
3
|
-
module Dry
|
4
|
-
module Types
|
5
|
-
register('form.nil') do
|
6
|
-
self['nil'].constructor(Coercions::Form.method(:to_nil))
|
7
|
-
end
|
8
|
-
|
9
|
-
register('form.date') do
|
10
|
-
self['date'].constructor(Coercions::Form.method(:to_date))
|
11
|
-
end
|
12
|
-
|
13
|
-
register('form.date_time') do
|
14
|
-
self['date_time'].constructor(Coercions::Form.method(:to_date_time))
|
15
|
-
end
|
16
|
-
|
17
|
-
register('form.time') do
|
18
|
-
self['time'].constructor(Coercions::Form.method(:to_time))
|
19
|
-
end
|
20
|
-
|
21
|
-
register('form.true') do
|
22
|
-
self['true'].constructor(Coercions::Form.method(:to_true))
|
23
|
-
end
|
24
|
-
|
25
|
-
register('form.false') do
|
26
|
-
self['false'].constructor(Coercions::Form.method(:to_false))
|
27
|
-
end
|
28
|
-
|
29
|
-
register('form.bool') do
|
30
|
-
(self['form.true'] | self['form.false']).safe
|
31
|
-
end
|
32
|
-
|
33
|
-
register('form.int') do
|
34
|
-
self['int'].constructor(Coercions::Form.method(:to_int))
|
35
|
-
end
|
36
|
-
|
37
|
-
register('form.float') do
|
38
|
-
self['float'].constructor(Coercions::Form.method(:to_float))
|
39
|
-
end
|
40
|
-
|
41
|
-
register('form.decimal') do
|
42
|
-
self['decimal'].constructor(Coercions::Form.method(:to_decimal))
|
43
|
-
end
|
44
|
-
|
45
|
-
register('form.array') do
|
46
|
-
self['array'].constructor(Coercions::Form.method(:to_ary)).safe
|
47
|
-
end
|
48
|
-
|
49
|
-
register('form.hash') do
|
50
|
-
self['hash'].constructor(Coercions::Form.method(:to_hash)).safe
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|