sanatio 0.1.0 → 0.2.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: 479bf5da1408bc5c3ff984ad7baa6899a49a5152
4
- data.tar.gz: 1dff2a76f7fe9f3393ee37b4312df7a30cf9e6a0
3
+ metadata.gz: 0519a019706d7b884adb4aadb461e572bb966395
4
+ data.tar.gz: c92a11cf8416f2209bdabbbcb0c1d9b13a85ec5b
5
5
  SHA512:
6
- metadata.gz: 3703329eb46d2bd728dab0e55c0e38a59f3569fbe005767f36f2d23952c588ffde5e8e27a48ea93496ac9e7e2e77823bc0ca14f5072d109c4758c0399e00ccad
7
- data.tar.gz: 74bf2a27049e5d97281505679c3a71fde78d10176844fc6f987e94546148efa7761bd3a1a34b29c2b1fe96fb4fe561c071826fda580b281da425ddfc300f65d9
6
+ metadata.gz: 873be4fe9aa533a5c879b5b2fa94d4b95b501bf27e94fcc64fad2bd4b4ff400493c4d940eb4a40e457407f44d5565928f4165ae7bd4031d6cd069bd50535545a
7
+ data.tar.gz: 85fb148a1ea81e9f23e1b8d7d67a8f3c177701aebca7ad20c7d98c27e1730f96c6d8accc02274dca8c49ed282f7ece2d09ef4fe4136706ed1694bfb4269368c6
data/README.md CHANGED
@@ -71,6 +71,28 @@ error.reason # :name_not_nil
71
71
  person.valid? # false
72
72
  ```
73
73
 
74
+ ### Skipping validations
75
+
76
+ You can skip validations if they are not needed or if they cannot run.
77
+
78
+ ```ruby
79
+ class Person
80
+ include Sanatio
81
+
82
+ def initialize(name)
83
+ @name = name
84
+ end
85
+
86
+ attr_reader :name
87
+
88
+ # NilClass#empty? does not exist, so the validation will fail when the name is nil.
89
+ ensure_that(:name).is { !empty? }.skip_if { nil? }.reason = :not_empty
90
+ end
91
+
92
+ Person.new(nil).valid? # true
93
+ Person.new("").valid? # false
94
+ ```
95
+
74
96
  ## Development
75
97
 
76
98
  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.
@@ -0,0 +1,13 @@
1
+ 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?
5
+ @validation_block = validation_block
6
+ self
7
+ end
8
+
9
+ def valid?(object)
10
+ evaluate(object, @validation_block)
11
+ end
12
+ end
13
+ end
@@ -1,23 +1,17 @@
1
1
  require 'sanatio/usage_error'
2
+ require 'sanatio/skippable'
3
+ require 'sanatio/block_validator'
2
4
 
3
5
  module Sanatio
4
6
  class ClassValidator
5
- attr_accessor :reason
6
-
7
- def initialize
8
- end
7
+ include Skippable
8
+ include BlockValidator
9
9
 
10
- def is(&validation_block)
11
- unless block_given?
12
- raise UsageError.new("self")
13
- end
14
-
15
- @validation_block = validation_block
16
- self
17
- end
10
+ attr_accessor :reason
18
11
 
19
- def valid?(object)
20
- object.instance_eval(&@validation_block)
12
+ private
13
+ def evaluate(object, block)
14
+ object.instance_eval(&block)
21
15
  end
22
16
  end
23
17
  end
@@ -1,7 +1,12 @@
1
1
  require 'sanatio/usage_error'
2
+ require 'sanatio/skippable'
3
+ require 'sanatio/block_validator'
2
4
 
3
5
  module Sanatio
4
6
  class FieldValidator
7
+ include Skippable
8
+ include BlockValidator
9
+
5
10
  attr_reader :field
6
11
  attr_accessor :reason
7
12
 
@@ -9,17 +14,9 @@ module Sanatio
9
14
  @field = field
10
15
  end
11
16
 
12
- def is(&validation_block)
13
- unless block_given?
14
- raise UsageError.new(":#{field}")
15
- end
16
-
17
- @validation_block = validation_block
18
- self
19
- end
20
-
21
- def valid?(object)
22
- object.send(@field).instance_eval &@validation_block
17
+ private
18
+ def evaluate(object, test)
19
+ object.send(@field).instance_eval &test
23
20
  end
24
21
  end
25
22
  end
@@ -0,0 +1,19 @@
1
+ require 'sanatio/usage_error'
2
+
3
+ module Sanatio
4
+ module Skippable
5
+ def skip_if(&skip_test)
6
+ raise UsageError.new("You need to give a block to #skip_if.") unless block_given?
7
+ @skip_test = skip_test
8
+ self
9
+ end
10
+
11
+ def skip_test
12
+ @skip_test ||= Proc.new { false }
13
+ end
14
+
15
+ def skip?(object)
16
+ evaluate(object, skip_test)
17
+ end
18
+ end
19
+ end
@@ -1,7 +1,7 @@
1
1
  module Sanatio
2
2
  class UsageError < StandardError
3
- def initialize(target)
4
- super("You need to give a block to #is. The correct usage is:\nensure_that(#{target}).is { validation_test }")
3
+ def initialize(message)
4
+ super
5
5
  end
6
6
  end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module Sanatio
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/sanatio.rb CHANGED
@@ -22,6 +22,8 @@ module Sanatio
22
22
 
23
23
  def errors
24
24
  self.class.validators.reject do |validator|
25
+ validator.skip?(self)
26
+ end.reject do |validator|
25
27
  validator.valid?(self)
26
28
  end.map(&Error.method(:new))
27
29
  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.1.0
4
+ version: 0.2.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-10 00:00:00.000000000 Z
11
+ date: 2015-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,9 +70,11 @@ files:
70
70
  - bin/console
71
71
  - bin/setup
72
72
  - lib/sanatio.rb
73
+ - lib/sanatio/block_validator.rb
73
74
  - lib/sanatio/class_validator.rb
74
75
  - lib/sanatio/error.rb
75
76
  - lib/sanatio/field_validator.rb
77
+ - lib/sanatio/skippable.rb
76
78
  - lib/sanatio/usage_error.rb
77
79
  - lib/sanatio/validator_factory.rb
78
80
  - lib/sanatio/version.rb