dry-data 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +6 -0
- data/lib/dry/data/type.rb +0 -5
- data/lib/dry/data/type/constrained.rb +31 -0
- data/lib/dry/data/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8a5fe08504f98f2f3e85ae1f07ac1773cd53b18
|
4
|
+
data.tar.gz: e22495f0d07c2fe8f933ebc777aacbbc417b8fd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e72b078024b770b78a6b8bde42570314d57fedd55b1517a49d73b7b2f4f37d453fc36cd3db661f5b38c13f6340f4339c59fd5743fa57f9dcba651f5f1a89c2ef
|
7
|
+
data.tar.gz: 9518ca648f2fa8665c95742c3faaf18d138aab32af1d1c0ed2d9a9fe40fda47ca27369f0ff6e8799cde999c0fe041ea8c83261d42233b9eda0c52eccbeea7241
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -179,6 +179,12 @@ and all of its predicates are supported.
|
|
179
179
|
IMPORTANT: `dry-data` does not have a runtime dependency on `dry-validation` so
|
180
180
|
if you want to use contrained types you need to add it to your Gemfile
|
181
181
|
|
182
|
+
If you want to use constrained type you need to require it explicitly:
|
183
|
+
|
184
|
+
``` ruby
|
185
|
+
require "dry/data/type/constrained"
|
186
|
+
```
|
187
|
+
|
182
188
|
``` ruby
|
183
189
|
string = Dry::Data["strict.string"].constrained(min_size: 3)
|
184
190
|
|
data/lib/dry/data/type.rb
CHANGED
@@ -4,7 +4,6 @@ require 'dry/data/type/array'
|
|
4
4
|
require 'dry/data/type/enum'
|
5
5
|
|
6
6
|
require 'dry/data/sum_type'
|
7
|
-
require 'dry/data/constraints'
|
8
7
|
|
9
8
|
module Dry
|
10
9
|
module Data
|
@@ -59,10 +58,6 @@ module Dry
|
|
59
58
|
@primitive = primitive
|
60
59
|
end
|
61
60
|
|
62
|
-
def constrained(options)
|
63
|
-
Constrained.new(constructor, primitive, Data.Rule(primitive, options))
|
64
|
-
end
|
65
|
-
|
66
61
|
def enum(*values)
|
67
62
|
Enum.new(values, constrained(inclusion: values))
|
68
63
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'dry/data/constraints'
|
2
|
+
|
3
|
+
module Dry
|
4
|
+
module Data
|
5
|
+
class Type
|
6
|
+
class Constrained < Type
|
7
|
+
attr_reader :rule
|
8
|
+
|
9
|
+
def initialize(constructor, primitive, rule)
|
10
|
+
super(constructor, primitive)
|
11
|
+
@rule = rule
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(input)
|
15
|
+
result = super(input)
|
16
|
+
|
17
|
+
if rule.(result).success?
|
18
|
+
result
|
19
|
+
else
|
20
|
+
raise ConstraintError, "#{input.inspect} violates constraints"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
alias_method :[], :call
|
24
|
+
end
|
25
|
+
|
26
|
+
def constrained(options)
|
27
|
+
Constrained.new(constructor, primitive, Data.Rule(primitive, options))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/dry/data/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
@@ -158,6 +158,7 @@ files:
|
|
158
158
|
- lib/dry/data/sum_type.rb
|
159
159
|
- lib/dry/data/type.rb
|
160
160
|
- lib/dry/data/type/array.rb
|
161
|
+
- lib/dry/data/type/constrained.rb
|
161
162
|
- lib/dry/data/type/enum.rb
|
162
163
|
- lib/dry/data/type/hash.rb
|
163
164
|
- lib/dry/data/type/optional.rb
|
@@ -185,9 +186,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
186
|
version: '0'
|
186
187
|
requirements: []
|
187
188
|
rubyforge_project:
|
188
|
-
rubygems_version: 2.4.5
|
189
|
+
rubygems_version: 2.4.5.1
|
189
190
|
signing_key:
|
190
191
|
specification_version: 4
|
191
192
|
summary: Simple type-system for Ruby
|
192
193
|
test_files: []
|
193
|
-
has_rdoc:
|