sanatio 0.2.0 → 0.3.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: 0519a019706d7b884adb4aadb461e572bb966395
4
- data.tar.gz: c92a11cf8416f2209bdabbbcb0c1d9b13a85ec5b
3
+ metadata.gz: 699ced36fd16610bf36b5aff22c232db0964db66
4
+ data.tar.gz: 168e872cca8ec73fd08adacb070fd49b205f7997
5
5
  SHA512:
6
- metadata.gz: 873be4fe9aa533a5c879b5b2fa94d4b95b501bf27e94fcc64fad2bd4b4ff400493c4d940eb4a40e457407f44d5565928f4165ae7bd4031d6cd069bd50535545a
7
- data.tar.gz: 85fb148a1ea81e9f23e1b8d7d67a8f3c177701aebca7ad20c7d98c27e1730f96c6d8accc02274dca8c49ed282f7ece2d09ef4fe4136706ed1694bfb4269368c6
6
+ metadata.gz: 9c4cf10bcaadea803a7119c61abb46531537d985287ecaf095a07a227617d0a9a49ed19ec991682a922248f794784f3aba149864d8b4e8b911f801fcf56a5562
7
+ data.tar.gz: d539982a01572c22dd55cab8cfbe868fee89af3fc0f44eaf485c47cd9dfde574fcad4f9963361b191ec847828cf7b52ffc1711e8b66301828f85ac42a4567530
data/README.md CHANGED
@@ -93,6 +93,118 @@ Person.new(nil).valid? # true
93
93
  Person.new("").valid? # false
94
94
  ```
95
95
 
96
+ ### Built-in validations
97
+
98
+ #### Present
99
+
100
+ This validation fails when the value is not present. Or empty when appropriate.
101
+
102
+ ```ruby
103
+ class Person
104
+ include Sanatio
105
+
106
+ def initialize(name)
107
+ @name = name
108
+ end
109
+
110
+ attr_reader :name
111
+
112
+ ensure_that(:name).is Present
113
+ end
114
+
115
+ Person.new(nil).valid? #false
116
+ Person.new("").valid? #false
117
+ Person.new("Test Testington").valid? #true
118
+ ```
119
+
120
+ #### GreaterThanOrEqualTo
121
+
122
+ This validation fails when the value is smaller than a given value.
123
+
124
+ ```ruby
125
+ class Person
126
+ include Sanatio
127
+
128
+ def initialize(age)
129
+ @age = age
130
+ end
131
+
132
+ attr_reader :age
133
+
134
+ ensure_that(:age).is GreaterThanOrEqualTo, 18
135
+ endO
136
+
137
+ Person.new(nil).valid? #true, validation is skipped
138
+ Person.new(15).valid? #false
139
+ Person.new(18).valid? #true
140
+ ```
141
+
142
+ #### GreaterThan
143
+
144
+ This validation fails when the value is smaller than or equal a given value.
145
+
146
+ ```ruby
147
+ class Person
148
+ include Sanatio
149
+
150
+ def initialize(age)
151
+ @age = age
152
+ end
153
+
154
+ attr_reader :age
155
+
156
+ ensure_that(:age).is GreaterThan, 18
157
+ endO
158
+
159
+ Person.new(nil).valid? #true, validation is skipped
160
+ Person.new(15).valid? #false
161
+ Person.new(19).valid? #true
162
+ ```
163
+
164
+ #### LessThanOrEqualTo
165
+
166
+ This validation fails when the value is greater than a given value.
167
+
168
+ ```ruby
169
+ class Person
170
+ include Sanatio
171
+
172
+ def initialize(age)
173
+ @age = age
174
+ end
175
+
176
+ attr_reader :age
177
+
178
+ ensure_that(:age).is LessThanOrEqualTo, 18
179
+ endO
180
+
181
+ Person.new(nil).valid? #true, validation is skipped
182
+ Person.new(15).valid? #true
183
+ Person.new(19).valid? #false
184
+ ```
185
+
186
+ #### LessThan
187
+
188
+ This validation fails when the value is greater than or equal to a given value.
189
+
190
+ ```ruby
191
+ class Person
192
+ include Sanatio
193
+
194
+ def initialize(age)
195
+ @age = age
196
+ end
197
+
198
+ attr_reader :age
199
+
200
+ ensure_that(:age).is LessThan, 18
201
+ endO
202
+
203
+ Person.new(nil).valid? #true, validation is skipped
204
+ Person.new(15).valid? #true
205
+ Person.new(19).valid? #false
206
+ ```
207
+
96
208
  ## Development
97
209
 
98
210
  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
@@ -1,11 +1,12 @@
1
1
  require "sanatio/version"
2
- require "sanatio/error"
3
- require "sanatio/validator_factory"
2
+ require "sanatio/class_validator"
3
+ require "sanatio/field_validator"
4
+ require "sanatio/built-in"
4
5
 
5
6
  module Sanatio
6
7
  module ClassMethods
7
8
  def ensure_that(target)
8
- ValidatorFactory.create(target).tap do |validator|
9
+ create(target).tap do |validator|
9
10
  validators << validator
10
11
  end
11
12
  end
@@ -13,6 +14,15 @@ module Sanatio
13
14
  def validators
14
15
  @validation ||= []
15
16
  end
17
+
18
+ private
19
+ def create(target)
20
+ if target.instance_of? Class
21
+ ClassValidator.new
22
+ else
23
+ FieldValidator.new(target)
24
+ end
25
+ end
16
26
  end
17
27
 
18
28
  module InstanceMethods
@@ -25,12 +35,14 @@ module Sanatio
25
35
  validator.skip?(self)
26
36
  end.reject do |validator|
27
37
  validator.valid?(self)
28
- end.map(&Error.method(:new))
38
+ end.map(&:error)
29
39
  end
30
40
  end
31
41
 
32
42
  def self.included(other)
33
43
  other.send(:include, InstanceMethods)
34
44
  other.send(:extend, ClassMethods)
45
+
46
+ other.send(:include, Sanatio::BuiltIn)
35
47
  end
36
48
  end
@@ -1,13 +1,18 @@
1
+ require 'sanatio/skippable'
2
+
1
3
  module Sanatio
2
- module BlockValidator
3
- def is(&validation_block)
4
- raise UsageError.new("You need to give a block to #is.") unless block_given?
4
+ class BlockValidator
5
+ include Skippable
6
+
7
+ attr_accessor :reason
8
+
9
+ def initialize(validation_block)
5
10
  @validation_block = validation_block
6
- self
7
11
  end
8
12
 
9
13
  def valid?(object)
10
- evaluate(object, @validation_block)
14
+ object.instance_eval(&@validation_block)
11
15
  end
12
16
  end
13
17
  end
18
+
@@ -0,0 +1,5 @@
1
+ require 'sanatio/built-in/present'
2
+ require 'sanatio/built-in/greater_than_or_equal_to'
3
+ require 'sanatio/built-in/greater_than'
4
+ require 'sanatio/built-in/less_than_or_equal_to'
5
+ require 'sanatio/built-in/less_than'
@@ -0,0 +1,25 @@
1
+ module Sanatio
2
+ module BuiltIn
3
+ class GreaterThan
4
+ def initialize(value)
5
+ @value = value
6
+ end
7
+
8
+ def skip?(object)
9
+ !object.respond_to?(:>)
10
+ end
11
+
12
+ def valid?(object)
13
+ object > @value
14
+ end
15
+
16
+ def reason
17
+ :less_than_or_equal_to
18
+ end
19
+
20
+ def params
21
+ [@value]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Sanatio
2
+ module BuiltIn
3
+ class GreaterThanOrEqualTo
4
+ def initialize(value)
5
+ @value = value
6
+ end
7
+
8
+ def skip?(object)
9
+ !object.respond_to?(:>=)
10
+ end
11
+
12
+ def valid?(object)
13
+ object >= @value
14
+ end
15
+
16
+ def reason
17
+ :less_than
18
+ end
19
+
20
+ def params
21
+ [@value]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Sanatio
2
+ module BuiltIn
3
+ class LessThan
4
+ def initialize(value)
5
+ @value = value
6
+ end
7
+
8
+ def skip?(object)
9
+ !object.respond_to?(:<)
10
+ end
11
+
12
+ def valid?(object)
13
+ object < @value
14
+ end
15
+
16
+ def reason
17
+ :greater_than_or_equal_to
18
+ end
19
+
20
+ def params
21
+ [@value]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Sanatio
2
+ module BuiltIn
3
+ class LessThanOrEqualTo
4
+ def initialize(value)
5
+ @value = value
6
+ end
7
+
8
+ def skip?(object)
9
+ !object.respond_to?(:<=)
10
+ end
11
+
12
+ def valid?(object)
13
+ object <= @value
14
+ end
15
+
16
+ def reason
17
+ :greater_than
18
+ end
19
+
20
+ def params
21
+ [@value]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ module Sanatio
2
+ module BuiltIn
3
+ class Present
4
+ def skip?(_)
5
+ end
6
+
7
+ def valid?(object)
8
+ !object.nil? && (!object.respond_to?(:empty?) || !object.empty?)
9
+ end
10
+
11
+ def reason
12
+ :not_present
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,17 +1,30 @@
1
- require 'sanatio/usage_error'
2
- require 'sanatio/skippable'
3
- require 'sanatio/block_validator'
1
+ require 'sanatio/validator_factory'
4
2
 
5
3
  module Sanatio
6
4
  class ClassValidator
7
- include Skippable
8
- include BlockValidator
5
+ include ValidatorFactory
9
6
 
10
- attr_accessor :reason
7
+ Error = Struct.new(:reason, :params)
8
+
9
+ def valid?(object)
10
+ @validator.valid?(object)
11
+ end
12
+
13
+ def skip?(object)
14
+ @validator.skip?(object)
15
+ end
16
+
17
+ def error
18
+ Error.new(@validator.reason, params)
19
+ end
11
20
 
12
21
  private
13
- def evaluate(object, block)
14
- object.instance_eval(&block)
22
+ def params
23
+ if @validator.respond_to?(:params)
24
+ @validator.params
25
+ else
26
+ []
27
+ end
15
28
  end
16
29
  end
17
30
  end
@@ -1,22 +1,34 @@
1
- require 'sanatio/usage_error'
2
- require 'sanatio/skippable'
3
- require 'sanatio/block_validator'
1
+ require 'sanatio/validator_factory'
4
2
 
5
3
  module Sanatio
6
4
  class FieldValidator
7
- include Skippable
8
- include BlockValidator
5
+ include ValidatorFactory
9
6
 
10
- attr_reader :field
11
- attr_accessor :reason
7
+ Error = Struct.new(:field, :reason, :params)
12
8
 
13
9
  def initialize(field)
14
10
  @field = field
15
11
  end
16
12
 
13
+ def valid?(object)
14
+ @validator.valid?(object.send(@field))
15
+ end
16
+
17
+ def skip?(object)
18
+ @validator.skip?(object.send(@field))
19
+ end
20
+
21
+ def error
22
+ Error.new(@field, @validator.reason, params)
23
+ end
24
+
17
25
  private
18
- def evaluate(object, test)
19
- object.send(@field).instance_eval &test
26
+ def params
27
+ if @validator.respond_to?(:params)
28
+ @validator.params
29
+ else
30
+ []
31
+ end
20
32
  end
21
33
  end
22
34
  end
@@ -13,7 +13,7 @@ module Sanatio
13
13
  end
14
14
 
15
15
  def skip?(object)
16
- evaluate(object, skip_test)
16
+ object.instance_eval(&skip_test)
17
17
  end
18
18
  end
19
19
  end
@@ -1,13 +1,14 @@
1
- require "sanatio/field_validator"
2
- require "sanatio/class_validator"
1
+ require 'sanatio/block_validator'
2
+ require 'sanatio/usage_error'
3
3
 
4
4
  module Sanatio
5
5
  module ValidatorFactory
6
- def self.create(target)
7
- if target.instance_of? Class
8
- ClassValidator.new
6
+ def is(validator = nil, *params, &validation_block)
7
+ if validator
8
+ @validator = validator.new(*params)
9
9
  else
10
- FieldValidator.new(target)
10
+ raise UsageError.new("You need to give a block to #is.") unless block_given?
11
+ @validator = BlockValidator.new(validation_block)
11
12
  end
12
13
  end
13
14
  end
@@ -1,3 +1,3 @@
1
1
  module Sanatio
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.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-04-12 00:00:00.000000000 Z
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,8 +71,13 @@ files:
71
71
  - bin/setup
72
72
  - lib/sanatio.rb
73
73
  - lib/sanatio/block_validator.rb
74
+ - lib/sanatio/built-in.rb
75
+ - lib/sanatio/built-in/greater_than.rb
76
+ - lib/sanatio/built-in/greater_than_or_equal_to.rb
77
+ - lib/sanatio/built-in/less_than.rb
78
+ - lib/sanatio/built-in/less_than_or_equal_to.rb
79
+ - lib/sanatio/built-in/present.rb
74
80
  - lib/sanatio/class_validator.rb
75
- - lib/sanatio/error.rb
76
81
  - lib/sanatio/field_validator.rb
77
82
  - lib/sanatio/skippable.rb
78
83
  - lib/sanatio/usage_error.rb
data/lib/sanatio/error.rb DELETED
@@ -1,13 +0,0 @@
1
- require 'forwardable'
2
-
3
- module Sanatio
4
- class Error
5
- extend Forwardable
6
-
7
- def_delegators :@validation, :field, :reason
8
-
9
- def initialize(validation)
10
- @validation = validation
11
- end
12
- end
13
- end