veto 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -94
- data/lib/veto/blocks/block.rb +25 -0
- data/lib/veto/blocks/checker.rb +28 -0
- data/lib/veto/blocks/conditional_block.rb +26 -0
- data/lib/veto/blocks/validate_block.rb +18 -0
- data/lib/veto/blocks/validates_block.rb +51 -0
- data/lib/veto/blocks/with_options_block.rb +7 -0
- data/lib/veto/check_context_object.rb +10 -0
- data/lib/veto/checks/attribute_check.rb +19 -0
- data/lib/veto/checks/check.rb +7 -0
- data/lib/veto/checks/check_factory.rb +18 -0
- data/lib/veto/{validators/exact_length_validator.rb → checks/exact_length_check.rb} +5 -4
- data/lib/veto/{validators/format_validator.rb → checks/format_check.rb} +5 -4
- data/lib/veto/{validators/greater_than_validator.rb → checks/greater_than_check.rb} +2 -4
- data/lib/veto/{validators/greater_than_or_equal_to_validator.rb → checks/greater_than_or_equal_to_check.rb} +2 -4
- data/lib/veto/{validators/inclusion_validator.rb → checks/inclusion_check.rb} +2 -4
- data/lib/veto/{validators/integer_validator.rb → checks/integer_check.rb} +2 -4
- data/lib/veto/{validators/length_range_validator.rb → checks/length_range_check.rb} +2 -4
- data/lib/veto/{validators/less_than_validator.rb → checks/less_than_check.rb} +2 -4
- data/lib/veto/{validators/less_than_or_equal_to_validator.rb → checks/less_than_or_equal_to_check.rb} +2 -4
- data/lib/veto/{validators/max_length_validator.rb → checks/max_length_check.rb} +2 -4
- data/lib/veto/checks/method_check.rb +17 -0
- data/lib/veto/{validators/min_length_validator.rb → checks/min_length_check.rb} +2 -4
- data/lib/veto/{validators/not_null_validator.rb → checks/not_null_check.rb} +2 -4
- data/lib/veto/{validators/numeric_validator.rb → checks/numeric_check.rb} +2 -4
- data/lib/veto/{validators/presence_validator.rb → checks/presence_check.rb} +2 -4
- data/lib/veto/conditions/condition.rb +7 -0
- data/lib/veto/conditions/condition_factory.rb +18 -0
- data/lib/veto/conditions/conditions.rb +21 -0
- data/lib/veto/conditions/context_method_condition.rb +11 -0
- data/lib/veto/conditions/entity_eval_condition.rb +11 -0
- data/lib/veto/conditions/if_conditions.rb +8 -0
- data/lib/veto/conditions/if_unless_conditions.rb +21 -0
- data/lib/veto/conditions/passing_condition.rb +7 -0
- data/lib/veto/conditions/primative_condition.rb +11 -0
- data/lib/veto/conditions/proc_condition.rb +11 -0
- data/lib/veto/conditions/unless_conditions.rb +8 -0
- data/lib/veto/configuration.rb +38 -37
- data/lib/veto/errors.rb +27 -27
- data/lib/veto/exceptions.rb +13 -3
- data/lib/veto/model.rb +33 -35
- data/lib/veto/validator.rb +68 -169
- data/lib/veto/version.rb +1 -1
- data/lib/veto.rb +90 -49
- data/spec/blocks/block_spec.rb +31 -0
- data/spec/blocks/check_block_spec.rb +46 -0
- data/spec/blocks/conditional_block_spec.rb +36 -0
- data/spec/blocks/validate_block_spec.rb +18 -0
- data/spec/blocks/validates_block_spec.rb +22 -0
- data/spec/checks/exact_length_check_spec.rb +37 -0
- data/spec/checks/format_validator_spec.rb +37 -0
- data/spec/checks/greater_than_check_spec.rb +48 -0
- data/spec/checks/greater_than_or_equal_to_check_spec.rb +48 -0
- data/spec/checks/inclusion_check_spec.rb +37 -0
- data/spec/checks/integer_check_spec.rb +37 -0
- data/spec/checks/length_range_check_spec.rb +37 -0
- data/spec/checks/less_than_check_spec.rb +48 -0
- data/spec/checks/less_than_or_equal_to_check_spec.rb +48 -0
- data/spec/checks/max_length_check_spec.rb +48 -0
- data/spec/checks/method_check_spec.rb +13 -0
- data/spec/checks/min_length_check_spec.rb +48 -0
- data/spec/checks/not_null_check_spec.rb +37 -0
- data/spec/checks/numeric_check_spec.rb +37 -0
- data/spec/checks/presence_check_spec.rb +48 -0
- data/spec/conditions/condition_factory_spec.rb +31 -0
- data/spec/conditions/conditions_spec.rb +25 -0
- data/spec/conditions/context_method_condition_spec.rb +25 -0
- data/spec/conditions/entity_eval_condition_spec.rb +21 -0
- data/spec/conditions/if_conditions_spec.rb +65 -0
- data/spec/conditions/if_unless_conditions_spec.rb +33 -0
- data/spec/conditions/primative_condition_spec.rb +22 -0
- data/spec/conditions/proc_condition_spec.rb +25 -0
- data/spec/conditions/unless_conditions_spec.rb +65 -0
- data/spec/configuration/message_spec.rb +18 -23
- data/spec/spec_helper.rb +2 -1
- data/spec/validator_spec.rb +224 -115
- metadata +96 -61
- data/lib/veto/attribute_validator_factory.rb +0 -36
- data/lib/veto/builder.rb +0 -65
- data/lib/veto/conditions.rb +0 -23
- data/lib/veto/conditions_evaluator.rb +0 -30
- data/lib/veto/validators/abstract_validator.rb +0 -15
- data/lib/veto/validators/attribute_validator.rb +0 -22
- data/lib/veto/validators/custom_method_validator.rb +0 -19
- data/spec/attribute_validator_factory_spec.rb +0 -72
- data/spec/builder_spec.rb +0 -38
- data/spec/conditions_evaluator_spec.rb +0 -90
- data/spec/conditions_spec.rb +0 -16
- data/spec/configuration_spec.rb +0 -6
- data/spec/errors_spec.rb +0 -22
- data/spec/model_spec.rb +0 -67
- data/spec/system/validator_spec.rb +0 -530
- data/spec/validators/exact_length_validator_spec.rb +0 -37
- data/spec/validators/format_validator_spec.rb +0 -32
- data/spec/validators/inclusion_validator_spec.rb +0 -45
- data/spec/validators/integer_validator_spec.rb +0 -42
- data/spec/validators/length_range_validator_spec.rb +0 -55
- data/spec/validators/max_length_validator_spec.rb +0 -32
- data/spec/validators/min_length_validator_spec.rb +0 -32
- data/spec/validators/not_null_validator_spec.rb +0 -27
- data/spec/validators/numeric_validator_spec.rb +0 -42
- data/spec/validators/presence_validator_spec.rb +0 -47
- data/spec/veto_spec.rb +0 -24
@@ -1,30 +0,0 @@
|
|
1
|
-
module Veto
|
2
|
-
class ConditionsEvaluator
|
3
|
-
def self.truthy? condition, context, entity
|
4
|
-
case condition
|
5
|
-
when String
|
6
|
-
!!entity.instance_eval(condition)
|
7
|
-
when Symbol
|
8
|
-
!!context.send(condition)
|
9
|
-
when Proc
|
10
|
-
!!condition.call(entity)
|
11
|
-
else
|
12
|
-
!!condition
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.truthy_conditions? conditions, context, entity
|
17
|
-
return true if !conditions.key?(:if) && !conditions.key?(:unless)
|
18
|
-
|
19
|
-
[*conditions[:if]].each do |condition|
|
20
|
-
return false unless truthy?(condition, context, entity)
|
21
|
-
end
|
22
|
-
|
23
|
-
[*conditions[:unless]].each do |condition|
|
24
|
-
return false if truthy?(condition, context, entity)
|
25
|
-
end
|
26
|
-
|
27
|
-
true
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'veto/conditions_evaluator'
|
2
|
-
|
3
|
-
module Veto
|
4
|
-
class AbstractValidator
|
5
|
-
def execute(context, entity, errors)
|
6
|
-
raise NotImplementedError
|
7
|
-
end
|
8
|
-
|
9
|
-
private
|
10
|
-
|
11
|
-
def truthy_conditions?(conditions, context, entity)
|
12
|
-
ConditionsEvaluator.truthy_conditions?(conditions, context, entity)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'veto/validators/abstract_validator'
|
2
|
-
|
3
|
-
module Veto
|
4
|
-
class AttributeValidator < AbstractValidator
|
5
|
-
def initialize attribute, options={}
|
6
|
-
@attribute = attribute
|
7
|
-
@options = options
|
8
|
-
end
|
9
|
-
|
10
|
-
def execute context, entity, errors
|
11
|
-
if truthy_conditions?(@options, context, entity)
|
12
|
-
value = entity.public_send(@attribute)
|
13
|
-
validate(entity, @attribute, value, errors, @options)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def validate entity, attribute, value, errors, options={}
|
18
|
-
raise NotImplementedError
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'veto/validators/abstract_validator'
|
2
|
-
|
3
|
-
module Veto
|
4
|
-
class CustomMethodValidator < AbstractValidator
|
5
|
-
|
6
|
-
attr_reader :method_name, :conditions
|
7
|
-
|
8
|
-
def initialize method_name, conditions
|
9
|
-
@method_name = method_name
|
10
|
-
@conditions = conditions
|
11
|
-
end
|
12
|
-
|
13
|
-
def execute context, entity, errors
|
14
|
-
if truthy_conditions?(@conditions, context, entity)
|
15
|
-
context.send(method_name)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/attribute_validator_factory'
|
3
|
-
require 'veto'
|
4
|
-
|
5
|
-
describe Veto::AttributeValidatorFactory do
|
6
|
-
let(:factory) { Veto::AttributeValidatorFactory }
|
7
|
-
let(:type) { :format }
|
8
|
-
|
9
|
-
describe '::new_validator' do
|
10
|
-
let(:attribute){ :first_name }
|
11
|
-
let(:options) { {} }
|
12
|
-
let(:result) { factory.new_validator(type, attribute, options) }
|
13
|
-
|
14
|
-
context 'when type is exact_length' do
|
15
|
-
let(:type) { :exact_length }
|
16
|
-
it { result.must_be_instance_of Veto::ExactLengthValidator }
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'when type is format' do
|
20
|
-
let(:type) { :format }
|
21
|
-
it { result.must_be_instance_of Veto::FormatValidator }
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'when type is inclusion' do
|
25
|
-
let(:type) { :inclusion }
|
26
|
-
it { result.must_be_instance_of Veto::InclusionValidator }
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'when type is integer' do
|
30
|
-
let(:type) { :integer }
|
31
|
-
it { result.must_be_instance_of Veto::IntegerValidator }
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'when type is length_range' do
|
35
|
-
let(:type) { :length_range }
|
36
|
-
it { result.must_be_instance_of Veto::LengthRangeValidator }
|
37
|
-
end
|
38
|
-
|
39
|
-
context 'when type is max_length' do
|
40
|
-
let(:type) { :max_length }
|
41
|
-
it { result.must_be_instance_of Veto::MaxLengthValidator }
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'when type is min_length' do
|
45
|
-
let(:type) { :min_length }
|
46
|
-
it { result.must_be_instance_of Veto::MinLengthValidator }
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'when type is not_null' do
|
50
|
-
let(:type) { :not_null }
|
51
|
-
it { result.must_be_instance_of Veto::NotNullValidator }
|
52
|
-
end
|
53
|
-
|
54
|
-
context 'when type is numeric' do
|
55
|
-
let(:type) { :numeric }
|
56
|
-
it { result.must_be_instance_of Veto::NumericValidator }
|
57
|
-
end
|
58
|
-
|
59
|
-
context 'when type is presence' do
|
60
|
-
let(:type) { :presence }
|
61
|
-
it { result.must_be_instance_of Veto::PresenceValidator }
|
62
|
-
end
|
63
|
-
|
64
|
-
context 'when type not recognized' do
|
65
|
-
let(:type) { :blah }
|
66
|
-
it 'raises error' do
|
67
|
-
e = proc{ result }.must_raise(ArgumentError)
|
68
|
-
e.message.must_equal 'Validator not found: ::Veto::BlahValidator'
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
data/spec/builder_spec.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
# require 'spec_helper'
|
2
|
-
# require 'veto/builder'
|
3
|
-
|
4
|
-
# describe Veto::Builder do
|
5
|
-
# let(:context) {stub}
|
6
|
-
# let(:context_conditions){ {:if => :is_good?} }
|
7
|
-
# let(:builder) { Veto::Builder.new(context, context_conditions) }
|
8
|
-
|
9
|
-
# describe '#with_options' do
|
10
|
-
# it 'returns new builder instance' do
|
11
|
-
# result = builder.with_options(:if => :is_tasty?, :unless => :rotten?)
|
12
|
-
# result.must_be_instance_of Veto::Builder
|
13
|
-
# end
|
14
|
-
|
15
|
-
# it 'passes conditions to instance as new conditions context' do
|
16
|
-
# result = builder.with_options(:if => :is_tasty?, :unless => :rotten?)
|
17
|
-
# result.conditions_context.must_equal({:if=>[:is_good?, :is_tasty?], :unless=>[:rotten?]})
|
18
|
-
# end
|
19
|
-
# end
|
20
|
-
|
21
|
-
# describe '#validates' do
|
22
|
-
# it 'passes list of validators to validates_with' do
|
23
|
-
# validators = stub
|
24
|
-
# Veto::Builder::ValidatesBuilder.expects(:validators).returns(validators)
|
25
|
-
# builder.expects(:validate_with).with(validators)
|
26
|
-
# builder.validates(:first_name, :presence => true, :if => 'high5')
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
|
30
|
-
# describe '#validate' do
|
31
|
-
# it 'passes list of validators to validates_with' do
|
32
|
-
# validators = stub
|
33
|
-
# builder.expects(:validate_with)
|
34
|
-
# Veto::Builder::ValidateBuilder.expects(:validators).returns(validators)
|
35
|
-
# builder.validate(:meth1, :meth2, :if => :cond1)
|
36
|
-
# end
|
37
|
-
# end
|
38
|
-
# end
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/conditions_evaluator'
|
3
|
-
|
4
|
-
describe Veto::ConditionsEvaluator do
|
5
|
-
let(:conditions){}
|
6
|
-
let(:entity){Object.new}
|
7
|
-
let(:context){Object.new}
|
8
|
-
let(:evaluator) { Veto::ConditionsEvaluator.new(conditions) }
|
9
|
-
|
10
|
-
describe '::truthy?' do
|
11
|
-
let(:condition){}
|
12
|
-
let(:result) { Veto::ConditionsEvaluator.truthy?(condition, context, entity) }
|
13
|
-
|
14
|
-
context 'when condition is true' do
|
15
|
-
let(:condition) { true }
|
16
|
-
it { result.must_equal true }
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'when condition is false' do
|
20
|
-
let(:condition) { false }
|
21
|
-
it { result.must_equal false }
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'when condition is nil' do
|
25
|
-
let(:condition) { nil }
|
26
|
-
it { result.must_equal false }
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'when condition is 0' do
|
30
|
-
let(:condition) { 0 }
|
31
|
-
it { result.must_equal true }
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'when condition is string' do
|
35
|
-
let(:entity) { stub(:is_alive => true) }
|
36
|
-
let(:condition) { 'is_alive' }
|
37
|
-
it { result.must_equal true }
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'when condition is empty string' do
|
41
|
-
let(:condition) { '' }
|
42
|
-
it { result.must_equal false }
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'when condition is empty hash' do
|
46
|
-
let(:condition) { {} }
|
47
|
-
it { result.must_equal true }
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'when condition is method_name symbol' do
|
51
|
-
let(:context) { stub(:active? => true) }
|
52
|
-
let(:condition) { :active? }
|
53
|
-
it { result.must_equal true }
|
54
|
-
end
|
55
|
-
|
56
|
-
context 'when condition is proc' do
|
57
|
-
let(:entity) { stub(:is_alive => true) }
|
58
|
-
let(:condition) { Proc.new{|e| e.is_alive } }
|
59
|
-
it { result.must_equal true }
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe '::truthy_conditions?' do
|
64
|
-
let(:conditions){}
|
65
|
-
let(:result) { Veto::ConditionsEvaluator.truthy_conditions?(conditions, context, entity) }
|
66
|
-
|
67
|
-
context 'when no conditions are assigned' do
|
68
|
-
let(:conditions){{}}
|
69
|
-
it { result.must_equal true }
|
70
|
-
end
|
71
|
-
|
72
|
-
context 'when if conditions return true' do
|
73
|
-
context 'when unless conditions return false' do
|
74
|
-
let(:conditions){{:if => [true, true], :unless => [false, false]}}
|
75
|
-
it { result.must_equal true }
|
76
|
-
end
|
77
|
-
|
78
|
-
context 'when one unless conditions returns true' do
|
79
|
-
let(:conditions){{:if => [true, true], :unless => [true, false]}}
|
80
|
-
it { result.must_equal false }
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
context 'when one if condition returns false' do
|
85
|
-
let(:conditions){{:if => [false, true], :unless => [false, false]}}
|
86
|
-
it { result.must_equal false }
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
data/spec/conditions_spec.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/conditions'
|
3
|
-
|
4
|
-
describe Veto::Conditions do
|
5
|
-
let(:params){{:if => :condition1, :unless => :condition2}}
|
6
|
-
let(:conditions){ Veto::Conditions.new(params) }
|
7
|
-
|
8
|
-
describe '::merge' do
|
9
|
-
it 'extracts and merges condition values' do
|
10
|
-
cond1 = {:if => [:is_good?, :is_tasty?]}
|
11
|
-
cond2 = {:if => "shaking", :unless => :is_bad?, :anotherkey => 'value'}
|
12
|
-
result = Veto::Conditions.merge(cond1, cond2)
|
13
|
-
result.must_equal({:if => [:is_good?, :is_tasty?, "shaking"], :unless => [:is_bad?]})
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
data/spec/configuration_spec.rb
DELETED
data/spec/errors_spec.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/errors'
|
3
|
-
require 'veto'
|
4
|
-
|
5
|
-
describe Veto::Errors do
|
6
|
-
let(:errors){ Veto::Errors.new }
|
7
|
-
describe '#add' do
|
8
|
-
context 'when message is string' do
|
9
|
-
it 'adds message to errors' do
|
10
|
-
errors.add(:name, 'is too long')
|
11
|
-
errors[:name].must_equal ['is too long']
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context 'when message contains message dictionary key' do
|
16
|
-
it 'adds dictionary message to errors' do
|
17
|
-
errors.add(:name, :presence)
|
18
|
-
errors[:name].must_equal ["is not present"]
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
data/spec/model_spec.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/model'
|
3
|
-
|
4
|
-
describe Veto::Model do
|
5
|
-
let(:validator){stub}
|
6
|
-
let(:validator_class){stub(:new => validator)}
|
7
|
-
let(:entity_class) {
|
8
|
-
klass = Class.new{
|
9
|
-
include Veto::Model
|
10
|
-
attr_accessor :first_name, :last_name
|
11
|
-
}
|
12
|
-
klass.validates_with validator_class
|
13
|
-
klass
|
14
|
-
}
|
15
|
-
let(:entity){ entity_class.new }
|
16
|
-
|
17
|
-
describe '::validates_with' do
|
18
|
-
it 'assigns validator' do
|
19
|
-
obj = Object.new
|
20
|
-
entity_class.validates_with obj
|
21
|
-
entity_class.validator.must_equal obj
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '::validator' do
|
26
|
-
context 'when validator is assigned' do
|
27
|
-
it 'returns validator' do
|
28
|
-
entity_class.validator.must_equal validator_class
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'when validator is not assigned' do
|
33
|
-
before { entity_class.validates_with nil }
|
34
|
-
|
35
|
-
it 'raises error' do
|
36
|
-
proc{ entity_class.validator }.must_raise ::Veto::ValidatorNotAssigned
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe '#validator' do
|
42
|
-
it 'initializes, memoizes, and returns validator instance' do
|
43
|
-
entity.send(:validator).must_equal validator
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe '#errors' do
|
48
|
-
it 'delegates method to validator' do
|
49
|
-
validator.expects(:errors)
|
50
|
-
entity.errors
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
describe '#valid?' do
|
55
|
-
it 'delegates method to validator' do
|
56
|
-
validator.expects(:valid?)
|
57
|
-
entity.valid?
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
describe '#validate!' do
|
62
|
-
it 'delegates method to validator' do
|
63
|
-
validator.expects(:validate!)
|
64
|
-
entity.validate!
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|