veto 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/veto/validator.rb +23 -0
- data/lib/veto/version.rb +1 -1
- data/spec/system/validator_spec.rb +34 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e305094f3935b538af7cd8c9d0c7e7e161184c5
|
4
|
+
data.tar.gz: f7036823f2d394a118e988c74921c04c592a3014
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d53f44223ba0e643bb49cb743497df739dbb636ea83c6a92c15a1d572ecaf709637a69e077cbe6458906f9d753b7b9f461d02c4e84891bf9d1708f6db375381
|
7
|
+
data.tar.gz: 6fd789da9b2c6308c6c792582bc1d49a7bba6e130ad810b9d485234aeb5688076f768a707c240bcd66c36978c0e7417ae6ddee7d4959b0f43b0865249ef0a9e1
|
data/lib/veto/validator.rb
CHANGED
@@ -8,6 +8,8 @@ module Veto
|
|
8
8
|
base.extend ClassMethods
|
9
9
|
end
|
10
10
|
|
11
|
+
|
12
|
+
|
11
13
|
module ClassMethods
|
12
14
|
def with_options *args, &block
|
13
15
|
builder.with_options(*args, &block)
|
@@ -75,6 +77,27 @@ module Veto
|
|
75
77
|
def builder
|
76
78
|
@builder ||= ::Veto::Builder.new(self)
|
77
79
|
end
|
80
|
+
|
81
|
+
# Ensures that when a Validator class is subclassed, the
|
82
|
+
# validation rules will be carried into the subclass as well,
|
83
|
+
# where they may be added upon.
|
84
|
+
#
|
85
|
+
# @example
|
86
|
+
# class PersonValidator
|
87
|
+
# include Veto.validator
|
88
|
+
# validates :name, :presence => true
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# class EmployeeValidator < PersonValidator
|
92
|
+
# validates :employee_id, :presence => true
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# employee = Employee.new
|
96
|
+
# EmployeeValidator.validate!(employee) # => ["name is not present", "employee_id is not present"]
|
97
|
+
|
98
|
+
def inherited(descendant)
|
99
|
+
descendant.validate_with validators
|
100
|
+
end
|
78
101
|
end
|
79
102
|
|
80
103
|
# Initializes validator
|
data/lib/veto/version.rb
CHANGED
@@ -493,4 +493,38 @@ describe Veto do
|
|
493
493
|
validator.errors.full_messages.must_equal ["email_address is not a valid email address"]
|
494
494
|
end
|
495
495
|
end
|
496
|
+
|
497
|
+
describe 'validator descendents/inheritence' do
|
498
|
+
let(:validator_class) do
|
499
|
+
Class.new{
|
500
|
+
include Veto.validator
|
501
|
+
|
502
|
+
validate :create_superclass_error
|
503
|
+
|
504
|
+
def create_superclass_error
|
505
|
+
errors.add(:base, "superclass error")
|
506
|
+
end
|
507
|
+
}
|
508
|
+
end
|
509
|
+
|
510
|
+
context 'when validator is subclassed' do
|
511
|
+
let(:validator_subclass) do
|
512
|
+
Class.new(validator_class){
|
513
|
+
|
514
|
+
validate :create_subclass_error
|
515
|
+
|
516
|
+
def create_subclass_error
|
517
|
+
errors.add(:base, "subclass error")
|
518
|
+
end
|
519
|
+
}
|
520
|
+
end
|
521
|
+
|
522
|
+
it 'inherits superclass validation rules' do
|
523
|
+
validator = validator_subclass.new(entity)
|
524
|
+
|
525
|
+
validator.valid?
|
526
|
+
validator.errors.must_equal({:base=>["superclass error", "subclass error"]})
|
527
|
+
end
|
528
|
+
end
|
529
|
+
end
|
496
530
|
end
|