sanatio 0.5.0 → 0.8.0
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/README.md +33 -0
- data/lib/sanatio.rb +3 -1
- data/lib/sanatio/block_validator.rb +8 -2
- data/lib/sanatio/built-in.rb +1 -0
- data/lib/sanatio/built-in/either.rb +47 -0
- data/lib/sanatio/built-in/equal_to.rb +1 -1
- data/lib/sanatio/built-in/greater_than.rb +1 -1
- data/lib/sanatio/built-in/greater_than_or_equal_to.rb +1 -1
- data/lib/sanatio/built-in/less_than.rb +1 -1
- data/lib/sanatio/built-in/less_than_or_equal_to.rb +1 -1
- data/lib/sanatio/built-in/matching.rb +1 -1
- data/lib/sanatio/built-in/not_equal_to.rb +1 -1
- data/lib/sanatio/built-in/one_of.rb +1 -1
- data/lib/sanatio/built-in/present.rb +1 -1
- data/lib/sanatio/built-in/valid.rb +1 -1
- data/lib/sanatio/class_validator.rb +2 -2
- data/lib/sanatio/field_validator.rb +2 -2
- data/lib/sanatio/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 585d48d86589b1bcc47c108afb413c9237f5d48b
|
4
|
+
data.tar.gz: bcd2e20c1bf6baf3efa471918f04bcd0b0e1ec16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 309b7b52daafb90e73c41cd8c284ef43651c8802de4c07f5aaaae257cf40c37c8afe2a0c43c2c28f0b1dfa13340fbee3ee56bce9cebe40bdb3d90cec6040d91d
|
7
|
+
data.tar.gz: 862fc04e1f17a84fe8fd700f44848e76d15451be5d8aa5235406f42542c4248760fcd7524e82b65552dc956f39cc664f0ed23da9ead8501df2ba028d78f29732
|
data/README.md
CHANGED
@@ -331,6 +331,39 @@ Person.new("DoesNotMatch").valid? #false
|
|
331
331
|
Person.new("DoesNotMatch").errors #[Error.new(:field => :name, :reason => :no_match)]
|
332
332
|
```
|
333
333
|
|
334
|
+
#### Either
|
335
|
+
|
336
|
+
This validation only passes when only one of the two fields matches the given
|
337
|
+
validator.
|
338
|
+
|
339
|
+
The validation is skipped if either field contains a value that would be
|
340
|
+
skipped.
|
341
|
+
|
342
|
+
```ruby
|
343
|
+
class Person
|
344
|
+
include Sanatio
|
345
|
+
|
346
|
+
def initialize(first_name, last_name)
|
347
|
+
@first_name = first_name
|
348
|
+
@last_name = last_name
|
349
|
+
end
|
350
|
+
|
351
|
+
attr_reader :first_name, :last_name
|
352
|
+
|
353
|
+
ensure_that(self).is Either, :first_name, :last_name, Present
|
354
|
+
end
|
355
|
+
|
356
|
+
Person.new('Test', 'Testington').valid? #false
|
357
|
+
Person.new('Test', nil).valid? #true
|
358
|
+
Person.new('', 'Testington).valid? #true
|
359
|
+
Person.new(nil, '').valid? #false
|
360
|
+
Person.new(nil, nil).errors #[Error.new(:reason => :neither, :params =>
|
361
|
+
# [:first_name, :last_name])]
|
362
|
+
Person.new('Test', 'Testington').errors #[Error.new(:reason => :both,
|
363
|
+
# :params =>
|
364
|
+
# [:first_name, :last_name])]
|
365
|
+
```
|
366
|
+
|
334
367
|
## Development
|
335
368
|
|
336
369
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/sanatio.rb
CHANGED
@@ -4,8 +4,6 @@ module Sanatio
|
|
4
4
|
class BlockValidator
|
5
5
|
include Skippable
|
6
6
|
|
7
|
-
attr_accessor :reason
|
8
|
-
|
9
7
|
def initialize(validation_block)
|
10
8
|
@validation_block = validation_block
|
11
9
|
end
|
@@ -13,6 +11,14 @@ module Sanatio
|
|
13
11
|
def valid?(object)
|
14
12
|
object.instance_eval(&@validation_block)
|
15
13
|
end
|
14
|
+
|
15
|
+
def reason=(reason)
|
16
|
+
@reason = reason
|
17
|
+
end
|
18
|
+
|
19
|
+
def reason(object)
|
20
|
+
@reason
|
21
|
+
end
|
16
22
|
end
|
17
23
|
end
|
18
24
|
|
data/lib/sanatio/built-in.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
1
|
+
class Either
|
2
|
+
def initialize(first_field, second_field, validator_class, *params)
|
3
|
+
@first_field = first_field
|
4
|
+
@second_field = second_field
|
5
|
+
@validator = validator_class.new(*params)
|
6
|
+
end
|
7
|
+
|
8
|
+
def skip?(value)
|
9
|
+
@validator.skip?(first_value(value)) || @validator.skip?(second_value(value))
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?(value)
|
13
|
+
!both?(value) && !neither?(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def reason(value)
|
17
|
+
if neither?(value)
|
18
|
+
:neither
|
19
|
+
else
|
20
|
+
if both?(value)
|
21
|
+
:both
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def params
|
27
|
+
[@first_field, @second_field]
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def both?(value)
|
33
|
+
@validator.valid?(first_value(value)) && @validator.valid?(second_value(value))
|
34
|
+
end
|
35
|
+
|
36
|
+
def neither?(value)
|
37
|
+
!@validator.valid?(first_value(value)) && !@validator.valid?(second_value(value))
|
38
|
+
end
|
39
|
+
|
40
|
+
def first_value(value)
|
41
|
+
value.send(@first_field)
|
42
|
+
end
|
43
|
+
|
44
|
+
def second_value(value)
|
45
|
+
value.send(@second_field)
|
46
|
+
end
|
47
|
+
end
|
data/lib/sanatio/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sanatio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thijs Wouters
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- lib/sanatio.rb
|
73
73
|
- lib/sanatio/block_validator.rb
|
74
74
|
- lib/sanatio/built-in.rb
|
75
|
+
- lib/sanatio/built-in/either.rb
|
75
76
|
- lib/sanatio/built-in/equal_to.rb
|
76
77
|
- lib/sanatio/built-in/greater_than.rb
|
77
78
|
- lib/sanatio/built-in/greater_than_or_equal_to.rb
|
@@ -110,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
111
|
version: '0'
|
111
112
|
requirements: []
|
112
113
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.4.
|
114
|
+
rubygems_version: 2.4.8
|
114
115
|
signing_key:
|
115
116
|
specification_version: 4
|
116
117
|
summary: A gem for validation, without all the dependencies.
|