definition 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +13 -0
- data/LICENSE +21 -0
- data/README.md +60 -0
- data/definition.gemspec +0 -1
- data/lib/definition.rb +4 -0
- data/lib/definition/dsl/comparators.rb +118 -0
- data/lib/definition/dsl/nil.rb +15 -0
- data/lib/definition/types/and.rb +1 -1
- data/lib/definition/types/or.rb +4 -4
- data/lib/definition/version.rb +1 -1
- metadata +6 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdd5d27632b42a3f09476358740ee5fb279f2b0ebb51c03f5473fbf2c0fa8a45
|
4
|
+
data.tar.gz: 5f92025e394507329ec6b980ca01cd45756ad69673436e3cc3afd818c4aa479b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 760843a1773788716e0b4f1e3cce2e636ef2182a479450cdb18e6893efded1a14b7494e2ecd1521b8f3860881c5685871bf66348d639eed9430777ee422c00c9
|
7
|
+
data.tar.gz: 75f4329226b54d9dd9b930968ec17666f6cad29ad421e6456393317c2000b791a1e9ea4bc22ccb2a52890d2f4d060e646f03e07fd3db1ab016f66240937ae97d
|
data/Changelog.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [0.2.0] - 2019-01-13
|
8
|
+
### Added
|
9
|
+
- Added built in definitions for common usecases (e.g. string length validation)
|
10
|
+
|
11
|
+
## [0.1.0] - 2019-01-12
|
12
|
+
### Added
|
13
|
+
- Initial release
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Dominik Goltermann
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -225,6 +225,66 @@ order = Definition.Keys do
|
|
225
225
|
end
|
226
226
|
```
|
227
227
|
|
228
|
+
### Predefined Definitions
|
229
|
+
|
230
|
+
#### Strings and Arrays
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
Definition.MaxSize(5).conform("house") # => pass
|
234
|
+
Definition.MaxSize(5).conform([1,2,3,4,5]) # => pass
|
235
|
+
```
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
Definition.MinSize(5).conform("house") # => pass
|
239
|
+
Definition.MinSize(5).conform([1,2,3,4,5]) # => pass
|
240
|
+
```
|
241
|
+
|
242
|
+
#### Strings
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
Definition.NonEmptyString.conform("house") # => pass
|
246
|
+
```
|
247
|
+
|
248
|
+
```ruby
|
249
|
+
Definition.Regex(/^\d*$/).conform("123") # => pass
|
250
|
+
```
|
251
|
+
|
252
|
+
#### Numerics
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
Definition.GreaterThen(5).conform(5.1) # => pass
|
256
|
+
Definition.GreaterThenEqual(5).conform(5) # => pass
|
257
|
+
Definition.LessThen(5).conform(4) # => pass
|
258
|
+
Definition.LessThenEqual(5).conform(5) # => pass
|
259
|
+
```
|
260
|
+
|
261
|
+
#### Strings, Array, Hashes
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
Definition.Empty.conform("") # => pass
|
265
|
+
Definition.Empty.conform([]) # => pass
|
266
|
+
Definition.Empty.conform({}) # => pass
|
267
|
+
```
|
268
|
+
|
269
|
+
```ruby
|
270
|
+
Definition.NonEmpty.conform("Joe") # => pass
|
271
|
+
Definition.NonEmpty.conform([1]) # => pass
|
272
|
+
Definition.NonEmpty.conform({ a: 1 }) # => pass
|
273
|
+
```
|
274
|
+
|
275
|
+
#### Nil
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
Definition.Nil.conform(nil) # => pass
|
279
|
+
```
|
280
|
+
|
281
|
+
#### All types
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
Definition.Equal(5).conform(5) # => pass
|
285
|
+
Definition.Equal("foo").conform("foo") # => pass
|
286
|
+
```
|
287
|
+
|
228
288
|
### Examples
|
229
289
|
|
230
290
|
Check out the [integration specs](./spec/integration) for more usage examples.
|
data/definition.gemspec
CHANGED
@@ -24,7 +24,6 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency "approvals", "~> 0.0"
|
25
25
|
spec.add_development_dependency "awesome_print"
|
26
26
|
spec.add_development_dependency "benchmark-ips"
|
27
|
-
spec.add_development_dependency "bundler", "~> 1.13"
|
28
27
|
spec.add_development_dependency "fuubar"
|
29
28
|
spec.add_development_dependency "guard"
|
30
29
|
spec.add_development_dependency "guard-rspec"
|
data/lib/definition.rb
CHANGED
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Definition
|
4
|
+
module Dsl
|
5
|
+
module Comparators
|
6
|
+
# Example:
|
7
|
+
# MaxSize(5)
|
8
|
+
def MaxSize(max_size) # rubocop:disable Naming/MethodName
|
9
|
+
Types::Lambda.new(:max_size) do |value|
|
10
|
+
case value
|
11
|
+
when String, Enumerable
|
12
|
+
conform_with(value) if value.size <= max_size
|
13
|
+
else
|
14
|
+
value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Example:
|
20
|
+
# MinSize(5)
|
21
|
+
def MinSize(min_size) # rubocop:disable Naming/MethodName
|
22
|
+
Types::Lambda.new(:min_size) do |value|
|
23
|
+
case value
|
24
|
+
when String, Enumerable
|
25
|
+
conform_with(value) if value.size >= min_size
|
26
|
+
else
|
27
|
+
value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Example:
|
33
|
+
# NonEmptyString
|
34
|
+
def NonEmptyString # rubocop:disable Naming/MethodName
|
35
|
+
Types::And.new(:non_empty_string, Type(String), MinSize(1))
|
36
|
+
end
|
37
|
+
|
38
|
+
# Example:
|
39
|
+
# GreaterThen(5)
|
40
|
+
def GreaterThen(min_value) # rubocop:disable Naming/MethodName
|
41
|
+
Types::Lambda.new("greater_then_#{min_value}") do |value|
|
42
|
+
conform_with(value) if value.is_a?(Numeric) && value > min_value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Example:
|
47
|
+
# GreaterThenEqual(5)
|
48
|
+
def GreaterThenEqual(min_value) # rubocop:disable Naming/MethodName
|
49
|
+
Types::Lambda.new("greater_then_equal_#{min_value}") do |value|
|
50
|
+
conform_with(value) if value.is_a?(Numeric) && value >= min_value
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Example:
|
55
|
+
# LessThen(5)
|
56
|
+
def LessThen(max_value) # rubocop:disable Naming/MethodName
|
57
|
+
Types::Lambda.new("less_then_#{max_value}") do |value|
|
58
|
+
conform_with(value) if value.is_a?(Numeric) && value < max_value
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Example:
|
63
|
+
# LessThenEqual(5)
|
64
|
+
def LessThenEqual(max_value) # rubocop:disable Naming/MethodName
|
65
|
+
Types::Lambda.new("less_then_equal_#{max_value}") do |value|
|
66
|
+
conform_with(value) if value.is_a?(Numeric) && value <= max_value
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Example:
|
71
|
+
# Equal("value")
|
72
|
+
def Equal(expected_value) # rubocop:disable Naming/MethodName
|
73
|
+
Types::Lambda.new(:equal) do |value|
|
74
|
+
conform_with(value) if value == expected_value
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Example:
|
79
|
+
# Empty
|
80
|
+
def Empty # rubocop:disable Naming/MethodName
|
81
|
+
Types::Lambda.new(:empty) do |value|
|
82
|
+
case value
|
83
|
+
when String, Array, Hash
|
84
|
+
conform_with(value) if value.empty?
|
85
|
+
else
|
86
|
+
value
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Example:
|
92
|
+
# NonEmpty
|
93
|
+
def NonEmpty # rubocop:disable Naming/MethodName
|
94
|
+
Types::Lambda.new(:non_empty) do |value|
|
95
|
+
case value
|
96
|
+
when String, Array, Hash
|
97
|
+
conform_with(value) unless value.empty?
|
98
|
+
else
|
99
|
+
value
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Example:
|
105
|
+
# Regex
|
106
|
+
def Regex(regex) # rubocop:disable Naming/MethodName
|
107
|
+
Types::Lambda.new("regex #{regex.inspect}") do |value|
|
108
|
+
case value
|
109
|
+
when String
|
110
|
+
conform_with(value) unless regex.match(value).nil?
|
111
|
+
else
|
112
|
+
value
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Definition
|
4
|
+
module Dsl
|
5
|
+
module Nil
|
6
|
+
# Example:
|
7
|
+
# Nil
|
8
|
+
def Nil # rubocop:disable Naming/MethodName
|
9
|
+
Types::Lambda.new(:nil) do |value|
|
10
|
+
conform_with(value) if value.nil?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/definition/types/and.rb
CHANGED
@@ -35,7 +35,7 @@ module Definition
|
|
35
35
|
ConformResult.new(results.last.value)
|
36
36
|
else
|
37
37
|
ConformResult.new(value, errors: [
|
38
|
-
ConformError.new(definition, "Not all
|
38
|
+
ConformError.new(definition, "Not all definitions are valid for #{definition.name}",
|
39
39
|
sub_errors: results.map(&:errors).flatten)
|
40
40
|
])
|
41
41
|
end
|
data/lib/definition/types/or.rb
CHANGED
@@ -33,10 +33,10 @@ module Definition
|
|
33
33
|
if result.is_a?(ConformResult)
|
34
34
|
result
|
35
35
|
else
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
error = ConformError.new(definition,
|
37
|
+
"None of the definitions are valid for #{definition.name}",
|
38
|
+
sub_errors: result)
|
39
|
+
ConformResult.new(value, errors: [error])
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
data/lib/definition/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: definition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dominik Goltermann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: approvals
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: bundler
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '1.13'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '1.13'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: fuubar
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -232,8 +218,10 @@ files:
|
|
232
218
|
- ".rspec"
|
233
219
|
- ".rubocop.yml"
|
234
220
|
- ".travis.yml"
|
221
|
+
- Changelog.md
|
235
222
|
- Gemfile
|
236
223
|
- Guardfile
|
224
|
+
- LICENSE
|
237
225
|
- README.md
|
238
226
|
- Rakefile
|
239
227
|
- benchmark/coercion.rb
|
@@ -246,6 +234,8 @@ files:
|
|
246
234
|
- lib/definition/conform_error.rb
|
247
235
|
- lib/definition/conform_result.rb
|
248
236
|
- lib/definition/dsl.rb
|
237
|
+
- lib/definition/dsl/comparators.rb
|
238
|
+
- lib/definition/dsl/nil.rb
|
249
239
|
- lib/definition/types.rb
|
250
240
|
- lib/definition/types/and.rb
|
251
241
|
- lib/definition/types/base.rb
|