veto 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -94
  3. data/lib/veto/blocks/block.rb +25 -0
  4. data/lib/veto/blocks/checker.rb +28 -0
  5. data/lib/veto/blocks/conditional_block.rb +26 -0
  6. data/lib/veto/blocks/validate_block.rb +18 -0
  7. data/lib/veto/blocks/validates_block.rb +51 -0
  8. data/lib/veto/blocks/with_options_block.rb +7 -0
  9. data/lib/veto/check_context_object.rb +10 -0
  10. data/lib/veto/checks/attribute_check.rb +19 -0
  11. data/lib/veto/checks/check.rb +7 -0
  12. data/lib/veto/checks/check_factory.rb +18 -0
  13. data/lib/veto/{validators/exact_length_validator.rb → checks/exact_length_check.rb} +5 -4
  14. data/lib/veto/{validators/format_validator.rb → checks/format_check.rb} +5 -4
  15. data/lib/veto/{validators/greater_than_validator.rb → checks/greater_than_check.rb} +2 -4
  16. data/lib/veto/{validators/greater_than_or_equal_to_validator.rb → checks/greater_than_or_equal_to_check.rb} +2 -4
  17. data/lib/veto/{validators/inclusion_validator.rb → checks/inclusion_check.rb} +2 -4
  18. data/lib/veto/{validators/integer_validator.rb → checks/integer_check.rb} +2 -4
  19. data/lib/veto/{validators/length_range_validator.rb → checks/length_range_check.rb} +2 -4
  20. data/lib/veto/{validators/less_than_validator.rb → checks/less_than_check.rb} +2 -4
  21. data/lib/veto/{validators/less_than_or_equal_to_validator.rb → checks/less_than_or_equal_to_check.rb} +2 -4
  22. data/lib/veto/{validators/max_length_validator.rb → checks/max_length_check.rb} +2 -4
  23. data/lib/veto/checks/method_check.rb +17 -0
  24. data/lib/veto/{validators/min_length_validator.rb → checks/min_length_check.rb} +2 -4
  25. data/lib/veto/{validators/not_null_validator.rb → checks/not_null_check.rb} +2 -4
  26. data/lib/veto/{validators/numeric_validator.rb → checks/numeric_check.rb} +2 -4
  27. data/lib/veto/{validators/presence_validator.rb → checks/presence_check.rb} +2 -4
  28. data/lib/veto/conditions/condition.rb +7 -0
  29. data/lib/veto/conditions/condition_factory.rb +18 -0
  30. data/lib/veto/conditions/conditions.rb +21 -0
  31. data/lib/veto/conditions/context_method_condition.rb +11 -0
  32. data/lib/veto/conditions/entity_eval_condition.rb +11 -0
  33. data/lib/veto/conditions/if_conditions.rb +8 -0
  34. data/lib/veto/conditions/if_unless_conditions.rb +21 -0
  35. data/lib/veto/conditions/passing_condition.rb +7 -0
  36. data/lib/veto/conditions/primative_condition.rb +11 -0
  37. data/lib/veto/conditions/proc_condition.rb +11 -0
  38. data/lib/veto/conditions/unless_conditions.rb +8 -0
  39. data/lib/veto/configuration.rb +38 -37
  40. data/lib/veto/errors.rb +27 -27
  41. data/lib/veto/exceptions.rb +13 -3
  42. data/lib/veto/model.rb +33 -35
  43. data/lib/veto/validator.rb +68 -169
  44. data/lib/veto/version.rb +1 -1
  45. data/lib/veto.rb +90 -49
  46. data/spec/blocks/block_spec.rb +31 -0
  47. data/spec/blocks/check_block_spec.rb +46 -0
  48. data/spec/blocks/conditional_block_spec.rb +36 -0
  49. data/spec/blocks/validate_block_spec.rb +18 -0
  50. data/spec/blocks/validates_block_spec.rb +22 -0
  51. data/spec/checks/exact_length_check_spec.rb +37 -0
  52. data/spec/checks/format_validator_spec.rb +37 -0
  53. data/spec/checks/greater_than_check_spec.rb +48 -0
  54. data/spec/checks/greater_than_or_equal_to_check_spec.rb +48 -0
  55. data/spec/checks/inclusion_check_spec.rb +37 -0
  56. data/spec/checks/integer_check_spec.rb +37 -0
  57. data/spec/checks/length_range_check_spec.rb +37 -0
  58. data/spec/checks/less_than_check_spec.rb +48 -0
  59. data/spec/checks/less_than_or_equal_to_check_spec.rb +48 -0
  60. data/spec/checks/max_length_check_spec.rb +48 -0
  61. data/spec/checks/method_check_spec.rb +13 -0
  62. data/spec/checks/min_length_check_spec.rb +48 -0
  63. data/spec/checks/not_null_check_spec.rb +37 -0
  64. data/spec/checks/numeric_check_spec.rb +37 -0
  65. data/spec/checks/presence_check_spec.rb +48 -0
  66. data/spec/conditions/condition_factory_spec.rb +31 -0
  67. data/spec/conditions/conditions_spec.rb +25 -0
  68. data/spec/conditions/context_method_condition_spec.rb +25 -0
  69. data/spec/conditions/entity_eval_condition_spec.rb +21 -0
  70. data/spec/conditions/if_conditions_spec.rb +65 -0
  71. data/spec/conditions/if_unless_conditions_spec.rb +33 -0
  72. data/spec/conditions/primative_condition_spec.rb +22 -0
  73. data/spec/conditions/proc_condition_spec.rb +25 -0
  74. data/spec/conditions/unless_conditions_spec.rb +65 -0
  75. data/spec/configuration/message_spec.rb +18 -23
  76. data/spec/spec_helper.rb +2 -1
  77. data/spec/validator_spec.rb +224 -115
  78. metadata +96 -61
  79. data/lib/veto/attribute_validator_factory.rb +0 -36
  80. data/lib/veto/builder.rb +0 -65
  81. data/lib/veto/conditions.rb +0 -23
  82. data/lib/veto/conditions_evaluator.rb +0 -30
  83. data/lib/veto/validators/abstract_validator.rb +0 -15
  84. data/lib/veto/validators/attribute_validator.rb +0 -22
  85. data/lib/veto/validators/custom_method_validator.rb +0 -19
  86. data/spec/attribute_validator_factory_spec.rb +0 -72
  87. data/spec/builder_spec.rb +0 -38
  88. data/spec/conditions_evaluator_spec.rb +0 -90
  89. data/spec/conditions_spec.rb +0 -16
  90. data/spec/configuration_spec.rb +0 -6
  91. data/spec/errors_spec.rb +0 -22
  92. data/spec/model_spec.rb +0 -67
  93. data/spec/system/validator_spec.rb +0 -530
  94. data/spec/validators/exact_length_validator_spec.rb +0 -37
  95. data/spec/validators/format_validator_spec.rb +0 -32
  96. data/spec/validators/inclusion_validator_spec.rb +0 -45
  97. data/spec/validators/integer_validator_spec.rb +0 -42
  98. data/spec/validators/length_range_validator_spec.rb +0 -55
  99. data/spec/validators/max_length_validator_spec.rb +0 -32
  100. data/spec/validators/min_length_validator_spec.rb +0 -32
  101. data/spec/validators/not_null_validator_spec.rb +0 -27
  102. data/spec/validators/numeric_validator_spec.rb +0 -42
  103. data/spec/validators/presence_validator_spec.rb +0 -47
  104. data/spec/veto_spec.rb +0 -24
@@ -1,119 +1,228 @@
1
1
  require 'spec_helper'
2
- require 'veto/validator'
3
2
 
4
3
  describe Veto::Validator do
5
- let(:entity){ stub }
6
- let(:validator_class) { Class.new{ include Veto::Validator } }
7
- let(:validator) { validator_class.new(entity) }
8
-
9
- describe '::builder' do
10
- it 'returns builder' do
11
- validator_class.send(:builder).must_be_instance_of Veto::Builder
12
- end
13
- end
14
-
15
- describe '::with_options' do
16
- it 'delegates to builder' do
17
- builder = stub
18
- validator_class.expects(:builder).returns(builder)
19
- builder.expects(:with_options).with('args')
20
- validator_class.with_options('args')
21
- end
22
- end
23
-
24
- describe '::validates' do
25
- it 'delegates to builder' do
26
- builder = stub
27
- validator_class.expects(:builder).returns(builder)
28
- builder.expects(:validates).with('args')
29
- validator_class.validates('args')
30
- end
31
- end
32
-
33
- describe '::validate' do
34
- it 'delegates to builder' do
35
- builder = stub
36
- validator_class.expects(:builder).returns(builder)
37
- builder.expects(:validate).with('args')
38
- validator_class.validate('args')
39
- end
40
- end
41
-
42
- describe '::validate_with' do
43
- it 'adds validator to validators array' do
44
- validator = stub
45
- validator_class.validate_with validator
46
- validator_class.validators.must_equal [validator]
47
- end
48
-
49
- it 'adds validators to validators array' do
50
- validator = stub
51
- validator_class.validate_with [validator, validator]
52
- validator_class.validators.must_equal [validator, validator]
53
- end
54
- end
55
-
56
- describe '::validators' do
57
- it 'returns validators list' do
58
- validator_class.validators.must_equal []
59
- end
60
- end
61
-
62
- describe '::valid?' do
63
- it 'initializes new instance and delegates to instance method' do
64
- validator_class.expects(:new).with(entity).returns(entity)
65
- entity.expects(:valid?)
66
- validator_class.valid?(entity)
67
- end
68
- end
69
-
70
- describe '::validate!' do
71
- it 'initializes new instance and delegates to instance method' do
72
- validator_class.expects(:new).with(entity).returns(entity)
73
- entity.expects(:validate!)
74
- validator_class.validate!(entity)
75
- end
76
- end
77
-
78
- describe '#valid?' do
79
- context 'when entity is valid' do
80
- it 'returns true' do
81
- errors = stub(:empty? => true)
82
- validator.expects(:errors => errors)
83
- validator.valid?.must_equal true
84
- end
85
- end
86
-
87
- context 'when entity is invalid' do
88
- it 'returns false' do
89
- errors = stub(:empty? => false)
90
- validator.expects(:errors => errors)
91
- validator.valid?.must_equal false
92
- end
93
- end
94
- end
95
-
96
- describe '#validate!' do
97
- context 'when entity is valid' do
98
- it 'returns true' do
99
- validator.expects(:valid?).returns(true)
100
- validator.validate!.must_be_nil
101
- end
102
- end
103
-
104
- context 'when entity is invalid' do
105
- it 'returns false' do
106
- errors = stub(:full_messages => '')
107
- validator.expects(:valid?).returns(false)
108
- validator.expects(:errors => errors)
109
- proc { validator.validate! }.must_raise ::Veto::InvalidEntity
110
- end
111
- end
112
- end
113
-
114
- describe '#errors' do
115
- it 'returns errors object' do
116
- validator.errors.must_be_instance_of Veto::Errors
117
- end
118
- end
4
+ describe 'general usage' do
5
+ context 'when validation fails' do
6
+ it 'it can generate errors' do
7
+ entity = stub(:name => nil)
8
+ validator = new_validator(:validates, :name, :presence => true)
9
+ validator.valid?(entity)
10
+ validator.errors[:name].must_equal(["is not present"])
11
+ end
12
+
13
+ it 'it can validate entity strictly' do
14
+ entity = stub(:name => nil)
15
+ validator = new_validator(:validates, :name, :presence => true)
16
+ proc { validator.validate!(entity) }.must_raise(Veto::InvalidEntity)
17
+ end
18
+ end
19
+
20
+ def new_validator(*args)
21
+ klass = Class.new{ include Veto.validator }
22
+ klass.send(*args)
23
+ klass.new
24
+ end
25
+ end
26
+
27
+ describe 'validates dsl' do
28
+ context 'when group conditions pass' do
29
+ context 'when check conditions pass' do
30
+ it 'assign errors' do
31
+ entity = stub(:name => nil)
32
+ validator = new_validator(:validates, :name, :presence => {:if => true}, :if => true)
33
+ validator.valid?(entity)
34
+ validator.errors[:name].must_equal(["is not present"])
35
+ end
36
+ end
37
+
38
+ context 'when check conditions fail' do
39
+ it 'does not assign errors' do
40
+ entity = stub(:name => nil)
41
+ validator = new_validator(:validates, :name, :presence => {:if => false}, :if => true)
42
+ validator.valid?(entity)
43
+ validator.errors.must_be_empty
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'when group conditions fail' do
49
+ it 'assign errors' do
50
+ entity = stub(:name => nil)
51
+ validator = new_validator(:validates, :name, :presence => {:if => true}, :if => false)
52
+ validator.valid?(entity)
53
+ validator.errors.must_be_empty
54
+ end
55
+ end
56
+
57
+ describe 'attribute validation' do
58
+ it 'assign errors' do
59
+ entity = stub(:isbn => '23432-53444-3234234')
60
+ validator = new_validator(:validates, :isbn, :exact_length => 13)
61
+ validator.valid?(entity)
62
+ validator.errors[:isbn].must_equal(["is not 13 characters"])
63
+ end
64
+ end
65
+
66
+ describe 'format check' do
67
+ context 'when value matches format' do
68
+ it 'assigns no errors' do
69
+ entity = stub(:first_name => 'john')
70
+ validator = new_validator(:validates, :first_name, :format => /[A-Za-z]+/)
71
+ validator.valid?(entity)
72
+ validator.errors.must_be_empty
73
+ end
74
+ end
75
+
76
+ context 'when value does not match format' do
77
+ it 'assigns errors' do
78
+ entity = stub(:first_name => 'john')
79
+ validator = new_validator(:validates, :first_name, :format => /[0-9]+/)
80
+ validator.valid?(entity)
81
+ validator.errors[:first_name].must_equal(["is not valid"])
82
+ end
83
+ end
84
+ end
85
+
86
+ def new_validator(*args)
87
+ klass = Class.new{ include Veto.validator }
88
+ klass.send(*args)
89
+ klass.new
90
+ end
91
+ end
92
+
93
+ describe 'validate dsl' do
94
+ context 'when conditions pass' do
95
+ context 'when check passes' do
96
+ it 'does not assign errors' do
97
+ entity = stub(:validate? => true, :first_name => 'John')
98
+ validator = new_validator
99
+ validator.valid?(entity)
100
+ validator.errors.must_be_empty
101
+ end
102
+ end
103
+
104
+ context 'when check fails' do
105
+ it 'assigns errors' do
106
+ entity = stub(:validate? => true, :first_name => 'Bob')
107
+ validator = new_validator
108
+ validator.valid?(entity)
109
+ validator.errors.must_equal(:first_name=>["is has wrong length"])
110
+ end
111
+ end
112
+ end
113
+
114
+ context 'when conditions fail' do
115
+ it 'does not assign errors' do
116
+ entity = stub(:validate? => false, :first_name => 'John')
117
+ validator = new_validator
118
+ validator.valid?(entity)
119
+ validator.errors.must_be_empty
120
+ end
121
+ end
122
+
123
+ def new_validator
124
+ Class.new {
125
+ include Veto.validator
126
+
127
+ validate :validate_first_name_length, :if => 'validate?'
128
+
129
+ def validate_first_name_length(entity)
130
+ errors.add(:first_name, 'is has wrong length') unless entity.first_name.size == 4
131
+ end
132
+ }.new
133
+ end
134
+ end
135
+
136
+ describe 'with_options dsl' do
137
+ context 'when conditions pass' do
138
+ it 'runs checks inside block' do
139
+ entity = stub(:validate? => true, :first_name => nil, :last_name => nil)
140
+ validator = new_validator
141
+ validator.valid?(entity)
142
+ validator.errors.must_equal(:first_name=>["is not present"], :last_name=>["is not present"])
143
+ end
144
+ end
145
+
146
+ context 'when conditions fail' do
147
+ it 'does not run checks inside block' do
148
+ entity = stub(:validate? => false, :first_name => nil, :last_name => nil)
149
+ validator = new_validator
150
+ validator.valid?(entity)
151
+ validator.errors.must_be_empty
152
+ end
153
+ end
154
+
155
+ def new_validator
156
+ Class.new {
157
+ include Veto.validator
158
+
159
+ with_options :if => 'validate?' do
160
+ validates :first_name, :presence => true
161
+ validates :last_name, :presence => true
162
+ end
163
+ }.new
164
+ end
165
+ end
166
+
167
+ describe 'custom validator' do
168
+ it 'uses custom validator' do
169
+ validator = Class.new {
170
+ include Veto.validator
171
+
172
+ class EmailCheck < ::Veto::AttributeCheck
173
+ def check(attribute_name, value, errors, options)
174
+ unless value.to_s =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
175
+ errors.add(attribute_name, "is not a valid email address")
176
+ end
177
+ end
178
+ end
179
+
180
+ validates :email_address, :email => true
181
+ }.new
182
+
183
+ entity = stub(:email_address => 123)
184
+ validator.valid?(entity)
185
+ validator.errors.must_equal(:email_address=>["is not a valid email address"])
186
+ end
187
+ end
188
+
189
+ describe 'validator descendents/inheritence' do
190
+ it 'inherits superclass validation rules' do
191
+ super_class = Class.new{
192
+ include Veto.validator
193
+
194
+ validate :create_superclass_error
195
+
196
+ def create_superclass_error(entity)
197
+ errors.add(:base, "superclass error")
198
+ end
199
+ }
200
+
201
+ sub_class1 = Class.new(super_class){
202
+
203
+ validate :create_subclass_error
204
+
205
+ def create_subclass_error(entity)
206
+ errors.add(:base, "subclass1 error")
207
+ end
208
+ }
209
+
210
+ sub_class2 = Class.new(super_class){
211
+
212
+ validate :create_subclass_error
213
+
214
+ def create_subclass_error(entity)
215
+ errors.add(:base, "subclass2 error")
216
+ end
217
+ }
218
+
219
+ validator = sub_class1.new
220
+ validator.valid?(stub)
221
+ validator.errors.must_equal(:base=>["superclass error", "subclass1 error"])
222
+
223
+ validator = sub_class2.new
224
+ validator.valid?(stub)
225
+ validator.errors.must_equal(:base=>["superclass error", "subclass2 error"])
226
+ end
227
+ end
119
228
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: veto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Lott
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-18 00:00:00.000000000 Z
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,55 +80,80 @@ files:
80
80
  - README.md
81
81
  - Rakefile
82
82
  - lib/veto.rb
83
- - lib/veto/attribute_validator_factory.rb
84
- - lib/veto/builder.rb
85
- - lib/veto/conditions.rb
86
- - lib/veto/conditions_evaluator.rb
83
+ - lib/veto/blocks/block.rb
84
+ - lib/veto/blocks/checker.rb
85
+ - lib/veto/blocks/conditional_block.rb
86
+ - lib/veto/blocks/validate_block.rb
87
+ - lib/veto/blocks/validates_block.rb
88
+ - lib/veto/blocks/with_options_block.rb
89
+ - lib/veto/check_context_object.rb
90
+ - lib/veto/checks/attribute_check.rb
91
+ - lib/veto/checks/check.rb
92
+ - lib/veto/checks/check_factory.rb
93
+ - lib/veto/checks/exact_length_check.rb
94
+ - lib/veto/checks/format_check.rb
95
+ - lib/veto/checks/greater_than_check.rb
96
+ - lib/veto/checks/greater_than_or_equal_to_check.rb
97
+ - lib/veto/checks/inclusion_check.rb
98
+ - lib/veto/checks/integer_check.rb
99
+ - lib/veto/checks/length_range_check.rb
100
+ - lib/veto/checks/less_than_check.rb
101
+ - lib/veto/checks/less_than_or_equal_to_check.rb
102
+ - lib/veto/checks/max_length_check.rb
103
+ - lib/veto/checks/method_check.rb
104
+ - lib/veto/checks/min_length_check.rb
105
+ - lib/veto/checks/not_null_check.rb
106
+ - lib/veto/checks/numeric_check.rb
107
+ - lib/veto/checks/presence_check.rb
108
+ - lib/veto/conditions/condition.rb
109
+ - lib/veto/conditions/condition_factory.rb
110
+ - lib/veto/conditions/conditions.rb
111
+ - lib/veto/conditions/context_method_condition.rb
112
+ - lib/veto/conditions/entity_eval_condition.rb
113
+ - lib/veto/conditions/if_conditions.rb
114
+ - lib/veto/conditions/if_unless_conditions.rb
115
+ - lib/veto/conditions/passing_condition.rb
116
+ - lib/veto/conditions/primative_condition.rb
117
+ - lib/veto/conditions/proc_condition.rb
118
+ - lib/veto/conditions/unless_conditions.rb
87
119
  - lib/veto/configuration.rb
88
120
  - lib/veto/errors.rb
89
121
  - lib/veto/exceptions.rb
90
122
  - lib/veto/model.rb
91
123
  - lib/veto/validator.rb
92
- - lib/veto/validators/abstract_validator.rb
93
- - lib/veto/validators/attribute_validator.rb
94
- - lib/veto/validators/custom_method_validator.rb
95
- - lib/veto/validators/exact_length_validator.rb
96
- - lib/veto/validators/format_validator.rb
97
- - lib/veto/validators/greater_than_or_equal_to_validator.rb
98
- - lib/veto/validators/greater_than_validator.rb
99
- - lib/veto/validators/inclusion_validator.rb
100
- - lib/veto/validators/integer_validator.rb
101
- - lib/veto/validators/length_range_validator.rb
102
- - lib/veto/validators/less_than_or_equal_to_validator.rb
103
- - lib/veto/validators/less_than_validator.rb
104
- - lib/veto/validators/max_length_validator.rb
105
- - lib/veto/validators/min_length_validator.rb
106
- - lib/veto/validators/not_null_validator.rb
107
- - lib/veto/validators/numeric_validator.rb
108
- - lib/veto/validators/presence_validator.rb
109
124
  - lib/veto/version.rb
110
- - spec/attribute_validator_factory_spec.rb
111
- - spec/builder_spec.rb
112
- - spec/conditions_evaluator_spec.rb
113
- - spec/conditions_spec.rb
125
+ - spec/blocks/block_spec.rb
126
+ - spec/blocks/check_block_spec.rb
127
+ - spec/blocks/conditional_block_spec.rb
128
+ - spec/blocks/validate_block_spec.rb
129
+ - spec/blocks/validates_block_spec.rb
130
+ - spec/checks/exact_length_check_spec.rb
131
+ - spec/checks/format_validator_spec.rb
132
+ - spec/checks/greater_than_check_spec.rb
133
+ - spec/checks/greater_than_or_equal_to_check_spec.rb
134
+ - spec/checks/inclusion_check_spec.rb
135
+ - spec/checks/integer_check_spec.rb
136
+ - spec/checks/length_range_check_spec.rb
137
+ - spec/checks/less_than_check_spec.rb
138
+ - spec/checks/less_than_or_equal_to_check_spec.rb
139
+ - spec/checks/max_length_check_spec.rb
140
+ - spec/checks/method_check_spec.rb
141
+ - spec/checks/min_length_check_spec.rb
142
+ - spec/checks/not_null_check_spec.rb
143
+ - spec/checks/numeric_check_spec.rb
144
+ - spec/checks/presence_check_spec.rb
145
+ - spec/conditions/condition_factory_spec.rb
146
+ - spec/conditions/conditions_spec.rb
147
+ - spec/conditions/context_method_condition_spec.rb
148
+ - spec/conditions/entity_eval_condition_spec.rb
149
+ - spec/conditions/if_conditions_spec.rb
150
+ - spec/conditions/if_unless_conditions_spec.rb
151
+ - spec/conditions/primative_condition_spec.rb
152
+ - spec/conditions/proc_condition_spec.rb
153
+ - spec/conditions/unless_conditions_spec.rb
114
154
  - spec/configuration/message_spec.rb
115
- - spec/configuration_spec.rb
116
- - spec/errors_spec.rb
117
- - spec/model_spec.rb
118
155
  - spec/spec_helper.rb
119
- - spec/system/validator_spec.rb
120
156
  - spec/validator_spec.rb
121
- - spec/validators/exact_length_validator_spec.rb
122
- - spec/validators/format_validator_spec.rb
123
- - spec/validators/inclusion_validator_spec.rb
124
- - spec/validators/integer_validator_spec.rb
125
- - spec/validators/length_range_validator_spec.rb
126
- - spec/validators/max_length_validator_spec.rb
127
- - spec/validators/min_length_validator_spec.rb
128
- - spec/validators/not_null_validator_spec.rb
129
- - spec/validators/numeric_validator_spec.rb
130
- - spec/validators/presence_validator_spec.rb
131
- - spec/veto_spec.rb
132
157
  - veto.gemspec
133
158
  homepage: http://github.com/kodio/veto
134
159
  licenses:
@@ -155,25 +180,35 @@ signing_key:
155
180
  specification_version: 4
156
181
  summary: Validations for plain ruby objects
157
182
  test_files:
158
- - spec/attribute_validator_factory_spec.rb
159
- - spec/builder_spec.rb
160
- - spec/conditions_evaluator_spec.rb
161
- - spec/conditions_spec.rb
183
+ - spec/blocks/block_spec.rb
184
+ - spec/blocks/check_block_spec.rb
185
+ - spec/blocks/conditional_block_spec.rb
186
+ - spec/blocks/validate_block_spec.rb
187
+ - spec/blocks/validates_block_spec.rb
188
+ - spec/checks/exact_length_check_spec.rb
189
+ - spec/checks/format_validator_spec.rb
190
+ - spec/checks/greater_than_check_spec.rb
191
+ - spec/checks/greater_than_or_equal_to_check_spec.rb
192
+ - spec/checks/inclusion_check_spec.rb
193
+ - spec/checks/integer_check_spec.rb
194
+ - spec/checks/length_range_check_spec.rb
195
+ - spec/checks/less_than_check_spec.rb
196
+ - spec/checks/less_than_or_equal_to_check_spec.rb
197
+ - spec/checks/max_length_check_spec.rb
198
+ - spec/checks/method_check_spec.rb
199
+ - spec/checks/min_length_check_spec.rb
200
+ - spec/checks/not_null_check_spec.rb
201
+ - spec/checks/numeric_check_spec.rb
202
+ - spec/checks/presence_check_spec.rb
203
+ - spec/conditions/condition_factory_spec.rb
204
+ - spec/conditions/conditions_spec.rb
205
+ - spec/conditions/context_method_condition_spec.rb
206
+ - spec/conditions/entity_eval_condition_spec.rb
207
+ - spec/conditions/if_conditions_spec.rb
208
+ - spec/conditions/if_unless_conditions_spec.rb
209
+ - spec/conditions/primative_condition_spec.rb
210
+ - spec/conditions/proc_condition_spec.rb
211
+ - spec/conditions/unless_conditions_spec.rb
162
212
  - spec/configuration/message_spec.rb
163
- - spec/configuration_spec.rb
164
- - spec/errors_spec.rb
165
- - spec/model_spec.rb
166
213
  - spec/spec_helper.rb
167
- - spec/system/validator_spec.rb
168
214
  - spec/validator_spec.rb
169
- - spec/validators/exact_length_validator_spec.rb
170
- - spec/validators/format_validator_spec.rb
171
- - spec/validators/inclusion_validator_spec.rb
172
- - spec/validators/integer_validator_spec.rb
173
- - spec/validators/length_range_validator_spec.rb
174
- - spec/validators/max_length_validator_spec.rb
175
- - spec/validators/min_length_validator_spec.rb
176
- - spec/validators/not_null_validator_spec.rb
177
- - spec/validators/numeric_validator_spec.rb
178
- - spec/validators/presence_validator_spec.rb
179
- - spec/veto_spec.rb
@@ -1,36 +0,0 @@
1
- require 'veto/validators/exact_length_validator'
2
- require 'veto/validators/format_validator'
3
- require 'veto/validators/greater_than_validator'
4
- require 'veto/validators/greater_than_or_equal_to_validator'
5
- require 'veto/validators/inclusion_validator'
6
- require 'veto/validators/integer_validator'
7
- require 'veto/validators/length_range_validator'
8
- require 'veto/validators/less_than_validator'
9
- require 'veto/validators/less_than_or_equal_to_validator'
10
- require 'veto/validators/max_length_validator'
11
- require 'veto/validators/min_length_validator'
12
- require 'veto/validators/not_null_validator'
13
- require 'veto/validators/numeric_validator'
14
- require 'veto/validators/presence_validator'
15
-
16
- module Veto
17
- class AttributeValidatorFactory
18
- def self.new_validator type, attribute, options={}
19
- klass_name = "#{camel_case(type.to_s)}Validator"
20
-
21
- begin
22
- klass = Veto.const_get(klass_name)
23
- rescue NameError => e
24
- raise(ArgumentError, "Validator not found: ::Veto::#{klass_name}")
25
- end
26
-
27
- klass.new(attribute, options)
28
- end
29
-
30
- private
31
-
32
- def self.camel_case(str)
33
- str.split('_').map{|w| w.capitalize }.join('')
34
- end
35
- end
36
- end
data/lib/veto/builder.rb DELETED
@@ -1,65 +0,0 @@
1
- require 'veto/conditions'
2
- require 'veto/attribute_validator_factory'
3
- require 'veto/validators/custom_method_validator'
4
-
5
- module Veto
6
- class Builder
7
- attr_reader :context, :conditions_context
8
-
9
- def initialize context, conditions_context={}, &block
10
- @context = context
11
- @conditions_context = conditions_context
12
- instance_eval(&block) if block_given?
13
- end
14
-
15
- def with_options conditions={}, &block
16
- mc = ::Veto::Conditions.merge(conditions_context, conditions)
17
- ::Veto::Builder.new(context, mc, &block)
18
- end
19
-
20
- def validates attribute, options={}
21
- a = attribute
22
- c = ::Veto::Conditions.select(options)
23
- mc = ::Veto::Conditions.merge(conditions_context, c)
24
- o = ::Veto::Conditions.reject(options)
25
- vs = o.map do |t, vopts|
26
- vo = case vopts
27
- when TrueClass
28
- {}
29
- when Hash
30
- vopts
31
- when Range, Array
32
- { :in => vopts }
33
- else
34
- { :with => vopts }
35
- end
36
-
37
- vc = ::Veto::Conditions.select(vo)
38
- mvc = ::Veto::Conditions.merge(mc, vc)
39
- mvo = vo.merge(mvc)
40
- ::Veto::AttributeValidatorFactory.new_validator(t, a, mvo)
41
- end
42
- validate_with vs
43
- end
44
-
45
- def validate *method_names
46
- if method_names.last.is_a?(Hash)
47
- c = method_names.last
48
- mns = method_names[0..-2]
49
- else
50
- c = {}
51
- mns = method_names
52
- end
53
-
54
- mc = ::Veto::Conditions.merge(conditions_context, c)
55
- vs = mns.map do |mn|
56
- ::Veto::CustomMethodValidator.new(mn, mc)
57
- end
58
- validate_with vs
59
- end
60
-
61
- def validate_with *args
62
- context.validate_with *args
63
- end
64
- end
65
- end
@@ -1,23 +0,0 @@
1
- module Veto
2
- class Conditions
3
- CONDITION_KEYS = [:if, :unless].freeze
4
-
5
- def self.reject hash={}
6
- hash.reject{|k,v| CONDITION_KEYS.include?(k) }
7
- end
8
-
9
- def self.select hash={}
10
- hash.select{|k,v| CONDITION_KEYS.include?(k) }
11
- end
12
-
13
- def self.merge dest_hash, source_hash
14
- CONDITION_KEYS.inject({}) do |m, k|
15
- c = []
16
- c << dest_hash[k]
17
- c << source_hash[k]
18
- m[k] = c.flatten.compact
19
- m
20
- end
21
- end
22
- end
23
- end