sanatio 0.3.0 → 0.4.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: 699ced36fd16610bf36b5aff22c232db0964db66
4
- data.tar.gz: 168e872cca8ec73fd08adacb070fd49b205f7997
3
+ metadata.gz: bd128dbe87fa8e5eedada9ea84cd2c2ecaa1fbf0
4
+ data.tar.gz: 12d18969cdb230afa2c0a7f69d2232261ba7c253
5
5
  SHA512:
6
- metadata.gz: 9c4cf10bcaadea803a7119c61abb46531537d985287ecaf095a07a227617d0a9a49ed19ec991682a922248f794784f3aba149864d8b4e8b911f801fcf56a5562
7
- data.tar.gz: d539982a01572c22dd55cab8cfbe868fee89af3fc0f44eaf485c47cd9dfde574fcad4f9963361b191ec847828cf7b52ffc1711e8b66301828f85ac42a4567530
6
+ metadata.gz: 934d064743bdd8381f0d950757867d1bf015062a113dfe7b855901a36ffdffc275c445960101f351aa79bd93209dd323521d46a6474aecc7896cc564ace2ab0a
7
+ data.tar.gz: 1593525b215691eafce260a7be7c966ad8ec346661b72ffac86d714b6bc62151b54bedc0371f3397b8f9e04a209a01ef0c6f9fe7e7148768bc8f9486b432c771
data/.travis.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - ruby-head
3
4
  - 2.2
4
5
  - 2.1
5
6
  - 2.0
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Sanatio
2
2
 
3
3
  ![Build status](https://travis-ci.org/ThijsWouters/sanatio.svg)
4
+ [![Gem Version](https://badge.fury.io/rb/sanatio.svg)](http://badge.fury.io/rb/sanatio)
5
+ [![Code Climate](https://codeclimate.com/github/ThijsWouters/sanatio/badges/gpa.svg)](https://codeclimate.com/github/ThijsWouters/sanatio)
4
6
 
5
7
  Sanatio is a gem that does validation. It has no external dependencies.
6
8
 
@@ -205,6 +207,86 @@ Person.new(15).valid? #true
205
207
  Person.new(19).valid? #false
206
208
  ```
207
209
 
210
+ #### OneOf
211
+
212
+ This validation fails when the value is not in the list given.
213
+
214
+ ```ruby
215
+ class Person
216
+ include Sanatio
217
+
218
+ def initialize(name)
219
+ @name = name
220
+ end
221
+
222
+ attr_reader :name
223
+
224
+ ensure_that(:name).is OneOf, 'Santa', 'Easter Bunny'
225
+ end
226
+
227
+ Person.new(nil).valid? #false
228
+ Person.new("Test Testington").valid? #false
229
+ Person.new("Easter Bunny").valid? #true
230
+ Person.new("Santa").valid? #true
231
+ ```
232
+
233
+ #### Valid
234
+
235
+ This validation fails when the value is not valid.
236
+
237
+ ```ruby
238
+ class Address
239
+ include Sanatio
240
+
241
+ def initialize(street)
242
+ @street = street
243
+ end
244
+
245
+ attr_reader :street
246
+
247
+ ensure_that(:street).is Present
248
+ end
249
+
250
+ class Person
251
+ include Sanatio
252
+
253
+ def initialize(address)
254
+ @address = address
255
+ end
256
+
257
+ attr_reader :address
258
+
259
+ ensure_that(:address).is Valid
260
+ end
261
+
262
+ Person.new(Address.new('Alleystreet')).valid? #true
263
+ Person.new(Address.new(nil)).valid? #false
264
+ Person.new(Address.new(nil)).errors #[Error.new(:field => :address, :reason => :invalid)]
265
+ ```
266
+
267
+ #### Matching
268
+
269
+ This validation fails when the value does not match the provided regex.
270
+
271
+ ```ruby
272
+ class Person
273
+ include Sanatio
274
+
275
+ def initialize(name)
276
+ @name = name
277
+ end
278
+
279
+ attr_reader :name
280
+
281
+ ensure_that(:name).is Matching, /Test/
282
+ end
283
+
284
+ Person.new(nil).valid? #false
285
+ Person.new("Test Testington").valid? #true
286
+ Person.new("DoesNotMatch").valid? #false
287
+ Person.new("DoesNotMatch").errors #[Error.new(:field => :name, :reason => :no_match)]
288
+ ```
289
+
208
290
  ## Development
209
291
 
210
292
  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.
@@ -1,25 +1,15 @@
1
+ require 'sanatio/built-in/support/compare'
2
+
1
3
  module Sanatio
2
4
  module BuiltIn
3
- class GreaterThan
5
+ class GreaterThan < Compare
4
6
  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
7
+ super(value, :>)
14
8
  end
15
9
 
16
10
  def reason
17
11
  :less_than_or_equal_to
18
12
  end
19
-
20
- def params
21
- [@value]
22
- end
23
13
  end
24
14
  end
25
15
  end
@@ -1,25 +1,15 @@
1
+ require 'sanatio/built-in/support/compare'
2
+
1
3
  module Sanatio
2
4
  module BuiltIn
3
- class GreaterThanOrEqualTo
5
+ class GreaterThanOrEqualTo < Compare
4
6
  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
7
+ super(value, :>=)
14
8
  end
15
9
 
16
10
  def reason
17
11
  :less_than
18
12
  end
19
-
20
- def params
21
- [@value]
22
- end
23
13
  end
24
14
  end
25
15
  end
@@ -1,25 +1,15 @@
1
+ require 'sanatio/built-in/support/compare'
2
+
1
3
  module Sanatio
2
4
  module BuiltIn
3
- class LessThan
5
+ class LessThan < Compare
4
6
  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
7
+ super(value, :<)
14
8
  end
15
9
 
16
10
  def reason
17
11
  :greater_than_or_equal_to
18
12
  end
19
-
20
- def params
21
- [@value]
22
- end
23
13
  end
24
14
  end
25
15
  end
@@ -1,25 +1,15 @@
1
+ require 'sanatio/built-in/support/compare'
2
+
1
3
  module Sanatio
2
4
  module BuiltIn
3
- class LessThanOrEqualTo
5
+ class LessThanOrEqualTo < Compare
4
6
  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
7
+ super(value, :<=)
14
8
  end
15
9
 
16
10
  def reason
17
11
  :greater_than
18
12
  end
19
-
20
- def params
21
- [@value]
22
- end
23
13
  end
24
14
  end
25
15
  end
@@ -0,0 +1,26 @@
1
+ module Sanatio
2
+ module BuiltIn
3
+ class Matching
4
+ def initialize(regex)
5
+ @regex = regex
6
+ end
7
+
8
+ def skip?(value)
9
+ !value.respond_to?(:to_str)
10
+ end
11
+
12
+ def valid?(value)
13
+ @regex.match(value)
14
+ end
15
+
16
+ def reason
17
+ :no_match
18
+ end
19
+
20
+ def params
21
+ [@regex]
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,25 @@
1
+ require 'sanatio/skippable'
2
+
3
+ module Sanatio
4
+ module BuiltIn
5
+ class OneOf
6
+ include Skippable
7
+
8
+ def initialize(*values)
9
+ @values = values
10
+ end
11
+
12
+ def valid?(object)
13
+ @values.include?(object)
14
+ end
15
+
16
+ def reason
17
+ :not_one_of
18
+ end
19
+
20
+ def params
21
+ @values
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ module Sanatio
2
+ module BuiltIn
3
+ class Compare
4
+ def initialize(value, method)
5
+ @value = value
6
+ @method = method
7
+ end
8
+
9
+ def params
10
+ [@value]
11
+ end
12
+
13
+ def skip?(object)
14
+ !object.respond_to?(@method)
15
+ end
16
+
17
+ def valid?(object)
18
+ object.send(@method, @value)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ module Sanatio
2
+ module BuiltIn
3
+ class Valid
4
+ def skip?(object)
5
+ !object.respond_to?(:valid?)
6
+ end
7
+
8
+ def valid?(object)
9
+ object.valid?
10
+ end
11
+
12
+ def reason
13
+ :invalid
14
+ end
15
+ end
16
+ end
17
+ end
@@ -3,3 +3,6 @@ require 'sanatio/built-in/greater_than_or_equal_to'
3
3
  require 'sanatio/built-in/greater_than'
4
4
  require 'sanatio/built-in/less_than_or_equal_to'
5
5
  require 'sanatio/built-in/less_than'
6
+ require 'sanatio/built-in/one_of'
7
+ require 'sanatio/built-in/valid'
8
+ require 'sanatio/built-in/matching'
@@ -1,3 +1,3 @@
1
1
  module Sanatio
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.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.3.0
4
+ version: 0.4.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-20 00:00:00.000000000 Z
11
+ date: 2015-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,7 +76,11 @@ files:
76
76
  - lib/sanatio/built-in/greater_than_or_equal_to.rb
77
77
  - lib/sanatio/built-in/less_than.rb
78
78
  - lib/sanatio/built-in/less_than_or_equal_to.rb
79
+ - lib/sanatio/built-in/matching.rb
80
+ - lib/sanatio/built-in/one_of.rb
79
81
  - lib/sanatio/built-in/present.rb
82
+ - lib/sanatio/built-in/support/compare.rb
83
+ - lib/sanatio/built-in/valid.rb
80
84
  - lib/sanatio/class_validator.rb
81
85
  - lib/sanatio/field_validator.rb
82
86
  - lib/sanatio/skippable.rb