veto 0.1.3 → 1.0.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.
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
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::NumericCheck do
4
+ context 'when value is numeric' do
5
+ it 'assigns no errors' do
6
+ entity = stub(:name => 123)
7
+ errors = new_errors
8
+ check = new_check(:name)
9
+
10
+ check.call(new_cto(entity, errors))
11
+ errors[:name].must_be_nil
12
+ end
13
+ end
14
+
15
+ context 'when value is not numeric' do
16
+ it 'assigns no errors' do
17
+ entity = stub(:name => 'abc')
18
+ errors = new_errors
19
+ check = new_check(:name)
20
+
21
+ check.call(new_cto(entity, errors))
22
+ errors[:name].must_equal(["is not a number"])
23
+ end
24
+ end
25
+
26
+ def new_cto(entity, errors)
27
+ stub(:entity => entity, :errors => errors)
28
+ end
29
+
30
+ def new_check(*args)
31
+ Veto::NumericCheck.new(*args)
32
+ end
33
+
34
+ def new_errors
35
+ Veto::Errors.new
36
+ end
37
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::PresenceCheck do
4
+ context 'when value is not null' do
5
+ it 'assigns no errors' do
6
+ entity = stub(:name => 'abcde')
7
+ errors = new_errors
8
+ check = new_check(:name)
9
+
10
+ check.call(new_cto(entity, errors))
11
+ errors[:name].must_be_nil
12
+ end
13
+ end
14
+
15
+ context 'when value is blank' do
16
+ it 'assigns no errors' do
17
+ entity = stub(:name => '')
18
+ errors = new_errors
19
+ check = new_check(:name)
20
+
21
+ check.call(new_cto(entity, errors))
22
+ errors[:name].must_equal(["is not present"])
23
+ end
24
+ end
25
+
26
+ context 'when value null' do
27
+ it 'assigns no errors' do
28
+ entity = stub(:name => nil)
29
+ errors = new_errors
30
+ check = new_check(:name)
31
+
32
+ check.call(new_cto(entity, errors))
33
+ errors[:name].must_equal(["is not present"])
34
+ end
35
+ end
36
+
37
+ def new_cto(entity, errors)
38
+ stub(:entity => entity, :errors => errors)
39
+ end
40
+
41
+ def new_check(*args)
42
+ Veto::PresenceCheck.new(*args)
43
+ end
44
+
45
+ def new_errors
46
+ Veto::Errors.new
47
+ end
48
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::ConditionFactory do
4
+ context 'when condition is a string' do
5
+ it 'returns entity eval condition instance' do
6
+ factory.new('a-string').must_be_instance_of(Veto::EntityEvalCondition)
7
+ end
8
+ end
9
+
10
+ context 'when condition is a symbol' do
11
+ it 'returns validator method condition instance' do
12
+ factory.new(:a_symbol).must_be_instance_of(Veto::ContextMethodCondition)
13
+ end
14
+ end
15
+
16
+ context 'when condition is a proc' do
17
+ it 'returns proc condition instance' do
18
+ factory.new(Proc.new{'blah'}).must_be_instance_of(Veto::ProcCondition)
19
+ end
20
+ end
21
+
22
+ context 'when condition is any other general statement or primative' do
23
+ it 'returns primative condition instance' do
24
+ factory.new(true).must_be_instance_of(Veto::PrimativeCondition)
25
+ end
26
+ end
27
+
28
+ def factory
29
+ Veto::ConditionFactory
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::Conditions do
4
+ it 'accepts single option' do
5
+ group = new_conditions(:good_method?)
6
+ group.size.must_equal(1)
7
+ end
8
+
9
+ it 'accepts array of options' do
10
+ group = new_conditions([:good_method?, Proc.new{ 'blah' }])
11
+ group.size.must_equal(2)
12
+ end
13
+
14
+ it 'iterates through conditions' do
15
+ group = new_conditions([:good_method?, Proc.new{ 'blah' }])
16
+ condition_list = []
17
+
18
+ group.each {|c| condition_list << c.class.name }
19
+ condition_list.must_equal(["Veto::ContextMethodCondition", "Veto::ProcCondition"])
20
+ end
21
+
22
+ def new_conditions(*args)
23
+ Veto::Conditions.new(*args)
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::ContextMethodCondition do
4
+ context 'when context method returns truthy value' do
5
+ it 'passes' do
6
+ context = stub(:is_good? => true)
7
+ cco = stub(:context => context, :entity => stub)
8
+ condition = new_condition(:is_good?)
9
+ condition.pass?(cco).must_equal(true)
10
+ end
11
+ end
12
+
13
+ context 'when context method returns falsy value' do
14
+ it 'fails' do
15
+ context = stub(:is_good? => false)
16
+ cco = stub(:context => context, :entity => stub)
17
+ condition = new_condition(:is_good?)
18
+ condition.pass?(cco).must_equal(false)
19
+ end
20
+ end
21
+
22
+ def new_condition(*args)
23
+ Veto::ContextMethodCondition.new(*args)
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::EntityEvalCondition do
4
+ it 'passes when entity evals to truthy value' do
5
+ entity = stub(:today_is_good => 'yup')
6
+ cco = stub(:entity => entity)
7
+ condition = new_condition('today_is_good')
8
+ condition.pass?(cco).must_equal(true)
9
+ end
10
+
11
+ it 'fails when entity evals to falsy value' do
12
+ entity = stub(:today_is_good => false)
13
+ cco = stub(:entity => entity)
14
+ condition = new_condition('today_is_good')
15
+ condition.pass?(cco).must_equal(false)
16
+ end
17
+
18
+ def new_condition(*args)
19
+ Veto::EntityEvalCondition.new(*args)
20
+ end
21
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::IfConditions do
4
+ context 'with passing condition' do
5
+ it 'correctly evalutes conditions' do
6
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
7
+ cco = stub(:entity => circle)
8
+ eval_condition = 'can_roll?' # pass
9
+
10
+ condition = new_conditions(eval_condition)
11
+ condition.pass?(cco).must_equal(true)
12
+ end
13
+ end
14
+
15
+ context 'with failing condition' do
16
+ it 'correctly evalutes conditions' do
17
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
18
+ cco = stub(:entity => circle)
19
+ eval_condition = 'is_stackable?' # fail
20
+
21
+ condition = new_conditions(eval_condition)
22
+ condition.pass?(cco).must_equal(false)
23
+ end
24
+ end
25
+
26
+ context 'with passing conditions' do
27
+ it 'correctly evalutes conditions' do
28
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
29
+ cco = stub(:entity => circle)
30
+ proc_condition = Proc.new{|entity| entity.is_round?} # pass
31
+ eval_condition = 'can_roll?' # pass
32
+
33
+ condition = new_conditions([proc_condition, eval_condition])
34
+ condition.pass?(cco).must_equal(true)
35
+ end
36
+ end
37
+
38
+ context 'with one passing and one failing condition' do
39
+ it 'correctly evalutes conditions' do
40
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
41
+ cco = stub(:entity => circle)
42
+ proc_condition = Proc.new{|entity| entity.is_round?} # pass
43
+ eval_condition = 'has_corners?' # fail
44
+
45
+ condition = new_conditions([proc_condition, eval_condition])
46
+ condition.pass?(cco).must_equal(false)
47
+ end
48
+ end
49
+
50
+ context 'with failing conditions' do
51
+ it 'correctly evalutes conditions' do
52
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
53
+ cco = stub(:entity => circle)
54
+ proc_condition = Proc.new{|entity| entity.has_corners?} # fail
55
+ eval_condition = 'is_stackable?' # fail
56
+
57
+ condition = new_conditions([proc_condition, eval_condition])
58
+ condition.pass?(cco).must_equal(false)
59
+ end
60
+ end
61
+
62
+ def new_conditions(*args)
63
+ Veto::IfConditions.new(*args)
64
+ end
65
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::IfUnlessConditions do
4
+ context 'when passing conditions' do
5
+ it 'correctly evaluates conditions' do
6
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
7
+ cco = stub(:entity => circle)
8
+
9
+ condition = new_if_unless_conditions(
10
+ :if => 'can_roll?',
11
+ :unless => 'has_corners?'
12
+ )
13
+ condition.pass?(cco).must_equal(true)
14
+ end
15
+ end
16
+
17
+ context 'when failing conditions' do
18
+ it 'correctly evaluates conditions' do
19
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
20
+ cco = stub(:entity => circle)
21
+
22
+ condition = new_if_unless_conditions(
23
+ :if => 'has_corners?',
24
+ :unless => 'can_roll?'
25
+ )
26
+ condition.pass?(cco).must_equal(false)
27
+ end
28
+ end
29
+
30
+ def new_if_unless_conditions(*args)
31
+ Veto::IfUnlessConditions.new(*args)
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::PrimativeCondition do
4
+ context 'when input coerses to true' do
5
+ it 'passes' do
6
+ new_condition(true).pass?.must_equal(true)
7
+ new_condition('string').pass?.must_equal(true)
8
+ new_condition(:symbol).pass?.must_equal(true)
9
+ end
10
+ end
11
+
12
+ context 'when input coerses to false' do
13
+ it 'fails' do
14
+ new_condition(false).pass?.must_equal(false)
15
+ new_condition('blah' == 'blahblah').pass?.must_equal(false)
16
+ end
17
+ end
18
+
19
+ def new_condition(*args)
20
+ Veto::PrimativeCondition.new(*args)
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::ProcCondition do
4
+ context 'when proc returns truthy value' do
5
+ it 'passes' do
6
+ entity = stub(:good? => true)
7
+ cco = stub(:entity => entity)
8
+ condition = new_condition(Proc.new{|e| e.good? })
9
+ condition.pass?(cco).must_equal(true)
10
+ end
11
+ end
12
+
13
+ context 'when proc returns falsy value' do
14
+ it 'fails' do
15
+ entity = stub(:good? => false)
16
+ cco = stub(:entity => entity)
17
+ condition = new_condition(Proc.new{|e| e.good? })
18
+ condition.pass?(cco).must_equal(false)
19
+ end
20
+ end
21
+
22
+ def new_condition(*args)
23
+ Veto::ProcCondition.new(*args)
24
+ end
25
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Veto::UnlessConditions do
4
+ context 'with passing condition' do
5
+ it 'correctly evalutes conditions' do
6
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
7
+ cco = stub(:entity => circle)
8
+ eval_condition = 'has_corners?' # fail
9
+
10
+ condition = new_conditions(eval_condition)
11
+ condition.pass?(cco).must_equal(true)
12
+ end
13
+ end
14
+
15
+ context 'with failing condition' do
16
+ it 'correctly evalutes conditions' do
17
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
18
+ cco = stub(:entity => circle)
19
+ eval_condition = 'is_round?' # pass
20
+
21
+ condition = new_conditions(eval_condition)
22
+ condition.pass?(cco).must_equal(false)
23
+ end
24
+ end
25
+
26
+ context 'with passing conditions' do
27
+ it 'correctly evalutes conditions' do
28
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
29
+ cco = stub(:entity => circle)
30
+ proc_condition = Proc.new{|entity| entity.is_stackable?} # fail
31
+ eval_condition = 'has_corners?' # fail
32
+
33
+ condition = new_conditions([proc_condition, eval_condition])
34
+ condition.pass?(cco).must_equal(true)
35
+ end
36
+ end
37
+
38
+ context 'with one passing and one failing condition' do
39
+ it 'correctly evalutes conditions' do
40
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
41
+ cco = stub(:entity => circle)
42
+ proc_condition = Proc.new{|entity| entity.is_round?} # pass
43
+ eval_condition = 'is_stackable?' # fail
44
+
45
+ condition = new_conditions([proc_condition, eval_condition])
46
+ condition.pass?(cco).must_equal(false)
47
+ end
48
+ end
49
+
50
+ context 'with failing conditions' do
51
+ it 'correctly evalutes conditions' do
52
+ circle = stub(:is_round? => true, :can_roll? => true, :has_corners? => false, :is_stackable? => false)
53
+ cco = stub(:entity => circle)
54
+ proc_condition = Proc.new{|entity| entity.is_round?} # pass
55
+ eval_condition = 'can_roll?' # pass
56
+
57
+ condition = new_conditions([proc_condition, eval_condition])
58
+ condition.pass?(cco).must_equal(false)
59
+ end
60
+ end
61
+
62
+ def new_conditions(*args)
63
+ Veto::UnlessConditions.new(*args)
64
+ end
65
+ end
@@ -1,30 +1,25 @@
1
1
  require 'spec_helper'
2
- require 'veto/configuration'
3
2
 
4
3
  describe Veto::Configuration::Message do
5
- let(:msg_proc){ Proc.new{'custom message'} }
6
- let(:msg){ Veto::Configuration::Message.new }
4
+ it 'gets message for check type' do
5
+ msg = new_message
6
+ msg.get(:exact_length, 23).must_equal('is not 23 characters')
7
+ end
7
8
 
8
- describe '#set' do
9
- it 'sets options on type' do
10
- msg.send(:custom_messages).must_equal({})
11
- msg.set(:exact_format, msg_proc)
12
- msg.send(:custom_messages).must_equal({:exact_format => msg_proc})
13
- end
14
- end
9
+ it 'overrides message for check type' do
10
+ msg = new_message
11
+ msg.set(:exact_length, Proc.new{'custom message'})
12
+ msg.get(:exact_length).must_equal('custom message')
13
+ end
15
14
 
16
- describe '#get' do
17
- context 'when custom_message is defined' do
18
- it 'returns custom message' do
19
- msg.set(:presence, msg_proc)
20
- msg.get(:presence).must_equal 'custom message'
21
- end
22
- end
15
+ context 'when type unknown' do
16
+ it 'outputs default message' do
17
+ msg = new_message
18
+ msg.get(:does_not_exist).must_equal('is not valid')
19
+ end
20
+ end
23
21
 
24
- context 'when custom_message is not defined' do
25
- it 'returns default message' do
26
- msg.get(:presence).must_equal 'is not present'
27
- end
28
- end
29
- end
22
+ def new_message
23
+ Veto::Configuration::Message.new
24
+ end
30
25
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'minitest/autorun'
2
2
  require 'mocha/setup'
3
+ require 'veto'
3
4
 
4
5
  def context *args, &block
5
- describe *args, &block
6
+ describe *args, &block
6
7
  end