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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86489fb3a7e95291f3cb895dd5ad7b46e6469501
4
- data.tar.gz: 02a12e955cb9f32e3375610da45d4d2ab275a03d
3
+ metadata.gz: 585d48d86589b1bcc47c108afb413c9237f5d48b
4
+ data.tar.gz: bcd2e20c1bf6baf3efa471918f04bcd0b0e1ec16
5
5
  SHA512:
6
- metadata.gz: e9a414ffd1a40f35cc81e9443258defd54156a22ec5e002e049da71d23f15bf03d54694940664440e4484d8dd455423f955afd69a660c783db8362e9c24b4783
7
- data.tar.gz: 83bade7aa04d63bbfb66ca0d5c7bc0f369ba0b91bfe8cbf5d55778cbb0762a6710dd1a7c0c8c441a8883797c09c0c01a4e556e0a107bba612a84b5cb99c95189
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.
@@ -35,7 +35,9 @@ module Sanatio
35
35
  validator.skip?(self)
36
36
  end.reject do |validator|
37
37
  validator.valid?(self)
38
- end.map(&:error)
38
+ end.map do |validator|
39
+ validator.error(self)
40
+ end
39
41
  end
40
42
  end
41
43
 
@@ -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
 
@@ -8,3 +8,4 @@ require 'sanatio/built-in/not_equal_to'
8
8
  require 'sanatio/built-in/one_of'
9
9
  require 'sanatio/built-in/valid'
10
10
  require 'sanatio/built-in/matching'
11
+ require 'sanatio/built-in/either'
@@ -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
@@ -10,7 +10,7 @@ class EqualTo
10
10
  object == @value
11
11
  end
12
12
 
13
- def reason
13
+ def reason(_)
14
14
  :not_equal_to
15
15
  end
16
16
 
@@ -7,7 +7,7 @@ module Sanatio
7
7
  super(value, :>)
8
8
  end
9
9
 
10
- def reason
10
+ def reason(_)
11
11
  :less_than_or_equal_to
12
12
  end
13
13
  end
@@ -7,7 +7,7 @@ module Sanatio
7
7
  super(value, :>=)
8
8
  end
9
9
 
10
- def reason
10
+ def reason(_)
11
11
  :less_than
12
12
  end
13
13
  end
@@ -7,7 +7,7 @@ module Sanatio
7
7
  super(value, :<)
8
8
  end
9
9
 
10
- def reason
10
+ def reason(_)
11
11
  :greater_than_or_equal_to
12
12
  end
13
13
  end
@@ -7,7 +7,7 @@ module Sanatio
7
7
  super(value, :<=)
8
8
  end
9
9
 
10
- def reason
10
+ def reason(_)
11
11
  :greater_than
12
12
  end
13
13
  end
@@ -13,7 +13,7 @@ module Sanatio
13
13
  @regex.match(value)
14
14
  end
15
15
 
16
- def reason
16
+ def reason(_)
17
17
  :no_match
18
18
  end
19
19
 
@@ -10,7 +10,7 @@ class NotEqualTo
10
10
  object != @value
11
11
  end
12
12
 
13
- def reason
13
+ def reason(_)
14
14
  :equal_to
15
15
  end
16
16
 
@@ -13,7 +13,7 @@ module Sanatio
13
13
  @values.include?(object)
14
14
  end
15
15
 
16
- def reason
16
+ def reason(_)
17
17
  :not_one_of
18
18
  end
19
19
 
@@ -8,7 +8,7 @@ module Sanatio
8
8
  !object.nil? && (!object.respond_to?(:empty?) || !object.empty?)
9
9
  end
10
10
 
11
- def reason
11
+ def reason(_)
12
12
  :not_present
13
13
  end
14
14
  end
@@ -9,7 +9,7 @@ module Sanatio
9
9
  object.valid?
10
10
  end
11
11
 
12
- def reason
12
+ def reason(_)
13
13
  :invalid
14
14
  end
15
15
  end
@@ -14,8 +14,8 @@ module Sanatio
14
14
  @validator.skip?(object)
15
15
  end
16
16
 
17
- def error
18
- Error.new(@validator.reason, params)
17
+ def error(object)
18
+ Error.new(@validator.reason(object), params)
19
19
  end
20
20
 
21
21
  private
@@ -18,8 +18,8 @@ module Sanatio
18
18
  @validator.skip?(object.send(@field))
19
19
  end
20
20
 
21
- def error
22
- Error.new(@field, @validator.reason, params)
21
+ def error(object)
22
+ Error.new(@field, @validator.reason(object.send(@field)), params)
23
23
  end
24
24
 
25
25
  private
@@ -1,3 +1,3 @@
1
1
  module Sanatio
2
- VERSION = "0.5.0"
2
+ VERSION = "0.8.0"
3
3
  end
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.5.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-06-30 00:00:00.000000000 Z
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.6
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.