vanguard 0.0.3

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 (78) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +17 -0
  5. data/Gemfile +7 -0
  6. data/Gemfile.devtools +56 -0
  7. data/Guardfile +18 -0
  8. data/LICENSE +21 -0
  9. data/README.md +80 -0
  10. data/Rakefile +2 -0
  11. data/TODO +3 -0
  12. data/config/flay.yml +3 -0
  13. data/config/flog.yml +2 -0
  14. data/config/mutant.yml +3 -0
  15. data/config/reek.yml +99 -0
  16. data/config/roodi.yml +26 -0
  17. data/config/yardstick.yml +2 -0
  18. data/lib/vanguard.rb +85 -0
  19. data/lib/vanguard/builder.rb +14 -0
  20. data/lib/vanguard/builder/nullary.rb +182 -0
  21. data/lib/vanguard/dsl.rb +43 -0
  22. data/lib/vanguard/dsl/evaluator.rb +197 -0
  23. data/lib/vanguard/evaluator.rb +72 -0
  24. data/lib/vanguard/instance_methods.rb +76 -0
  25. data/lib/vanguard/matcher.rb +19 -0
  26. data/lib/vanguard/matcher/binary.rb +59 -0
  27. data/lib/vanguard/matcher/binary/and.rb +20 -0
  28. data/lib/vanguard/matcher/binary/or.rb +20 -0
  29. data/lib/vanguard/matcher/binary/xor.rb +22 -0
  30. data/lib/vanguard/matcher/nullary.rb +27 -0
  31. data/lib/vanguard/matcher/nullary/equality.rb +46 -0
  32. data/lib/vanguard/matcher/nullary/format.rb +126 -0
  33. data/lib/vanguard/matcher/nullary/greater_than.rb +45 -0
  34. data/lib/vanguard/matcher/nullary/identity.rb +50 -0
  35. data/lib/vanguard/matcher/nullary/inclusion.rb +67 -0
  36. data/lib/vanguard/matcher/nullary/less_than.rb +48 -0
  37. data/lib/vanguard/matcher/nullary/primitive.rb +47 -0
  38. data/lib/vanguard/matcher/nullary/proc.rb +44 -0
  39. data/lib/vanguard/matcher/nullary/value.rb +32 -0
  40. data/lib/vanguard/matcher/unary.rb +45 -0
  41. data/lib/vanguard/matcher/unary/attribute.rb +49 -0
  42. data/lib/vanguard/matcher/unary/not.rb +25 -0
  43. data/lib/vanguard/result.rb +93 -0
  44. data/lib/vanguard/rule.rb +43 -0
  45. data/lib/vanguard/rule/guard.rb +103 -0
  46. data/lib/vanguard/rule/nullary.rb +81 -0
  47. data/lib/vanguard/rule/nullary/attribute.rb +69 -0
  48. data/lib/vanguard/rule/nullary/attribute/absence.rb +19 -0
  49. data/lib/vanguard/rule/nullary/attribute/format.rb +46 -0
  50. data/lib/vanguard/rule/nullary/attribute/inclusion.rb +62 -0
  51. data/lib/vanguard/rule/nullary/attribute/length.rb +184 -0
  52. data/lib/vanguard/rule/nullary/attribute/predicate.rb +10 -0
  53. data/lib/vanguard/rule/nullary/attribute/presence.rb +32 -0
  54. data/lib/vanguard/rule/nullary/attribute/presence/not_blank.rb +0 -0
  55. data/lib/vanguard/rule/nullary/attribute/presence/not_nil.rb +0 -0
  56. data/lib/vanguard/rule/nullary/attribute/primitive.rb +35 -0
  57. data/lib/vanguard/rule/nullary/confirmation.rb +210 -0
  58. data/lib/vanguard/support/blank.rb +29 -0
  59. data/lib/vanguard/validator.rb +116 -0
  60. data/lib/vanguard/validator/builder.rb +52 -0
  61. data/lib/vanguard/violation.rb +84 -0
  62. data/spec/integration/vanguard/dsl/guard_spec.rb +58 -0
  63. data/spec/integration/vanguard/dsl/validates_absence_of_spec.rb +19 -0
  64. data/spec/integration/vanguard/dsl/validates_acceptance_of_spec.rb +19 -0
  65. data/spec/integration/vanguard/dsl/validates_confirmation_of_spec.rb +27 -0
  66. data/spec/integration/vanguard/dsl/validates_format_of_spec.rb +79 -0
  67. data/spec/integration/vanguard/dsl/validates_inclusion_of_spec.rb +23 -0
  68. data/spec/integration/vanguard/dsl/validates_length_of_spec.rb +77 -0
  69. data/spec/integration/vanguard/dsl/validates_presence_of_spec.rb +19 -0
  70. data/spec/integration/vanguard/dsl/validates_value_of_spec.rb +30 -0
  71. data/spec/integration/vanguard/validator_spec.rb +57 -0
  72. data/spec/rcov.opts +6 -0
  73. data/spec/shared/dsl_spec.rb +73 -0
  74. data/spec/spec_helper.rb +3 -0
  75. data/spec/suite.rb +10 -0
  76. data/spec/unit/vanguard/support/blank_spec.rb +72 -0
  77. data/vanguard.gemspec +23 -0
  78. metadata +190 -0
@@ -0,0 +1,84 @@
1
+ #encoding: utf-8
2
+
3
+ module Vanguard
4
+ # Rule violation on resouce
5
+ class Violation
6
+ include Adamantium::Flat, Equalizer.new(:resource, :rule)
7
+
8
+ # Return object validated in this violation
9
+ #
10
+ # @return [Object]
11
+ #
12
+ # @api private
13
+ #
14
+ attr_reader :resource
15
+
16
+ # Rule which generated this Violation
17
+ #
18
+ # @return [Vanguard::Rule]
19
+ #
20
+ # @api private
21
+ #
22
+ attr_reader :rule
23
+
24
+ # Name of the attribute which this Violation pertains to
25
+ #
26
+ # @return [Symbol]
27
+ # the name of the validated attribute associated with this violation
28
+ #
29
+ # @api private
30
+ #
31
+ def attribute_name
32
+ rule.attribute_name
33
+ end
34
+
35
+ # Return symbolic type of rule
36
+ #
37
+ # @return [Symbol]
38
+ #
39
+ # @api private
40
+ #
41
+ def type
42
+ rule.type
43
+ end
44
+
45
+ # Return violation info
46
+ #
47
+ # @return [Hash]
48
+ #
49
+ # @api private
50
+ #
51
+ def info
52
+ rule.violation_info.merge(:value => @value)
53
+ end
54
+ memoize :info
55
+
56
+ # Return Violation values
57
+ #
58
+ # @return [Object]
59
+ #
60
+ # @api private
61
+ #
62
+ def values
63
+ rule.violation_values
64
+ end
65
+
66
+ private
67
+
68
+ # Initialize object
69
+ #
70
+ # @param [Resource] resource
71
+ # the validated object
72
+ #
73
+ # @param [Rule] rule
74
+ # the rule that was violated
75
+ #
76
+ # @return [undefined]
77
+ #
78
+ # @api private
79
+ #
80
+ def initialize(rule, resource)
81
+ @rule, @resource = rule, resource
82
+ end
83
+ end # class Violation
84
+ end # module Vanguard
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, 'guard specs' do
4
+ include Spec::Shared::DSL
5
+
6
+ before do
7
+ class_under_test.class_eval do
8
+ def foo?
9
+ !!@foo
10
+ end
11
+
12
+ attr_accessor :foo
13
+ end
14
+ builder.validates_presence_of(attribute_name,options)
15
+ end
16
+
17
+ context 'with :if option' do
18
+ let(:options) { { :if => :foo? } }
19
+
20
+ context 'when predicate is true' do
21
+ before do
22
+ resource.foo = true
23
+ end
24
+
25
+ describe 'when validated attribute is present' do
26
+ let(:attribute_value) { :foo }
27
+
28
+ it_should_be_a_valid_instance
29
+ end
30
+
31
+ describe 'when validated attribute is absent' do
32
+ let(:attribute_value) { nil }
33
+
34
+ it_should_be_an_invalid_instance
35
+ end
36
+ end
37
+
38
+ context 'when predicate is false' do
39
+ before do
40
+ resource.foo = false
41
+ end
42
+
43
+ describe 'when validated attribute is present' do
44
+ let(:attribute_value) { :foo }
45
+
46
+ it_should_be_a_valid_instance
47
+ end
48
+
49
+ describe 'when validated attribute is absent' do
50
+ let(:attribute_value) { nil }
51
+
52
+ context 'and options include :if predicate' do
53
+ it_should_be_a_valid_instance
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, '#validates_absence_of' do
4
+ include Spec::Shared::DSL
5
+
6
+ before { builder.validates_absence_of attribute_name }
7
+
8
+ describe 'when validated attribute is present' do
9
+ let(:attribute_value) { :foo }
10
+
11
+ it_should_be_an_invalid_instance
12
+ end
13
+
14
+ describe 'when validated attribute is absent' do
15
+ let(:attribute_value) { nil }
16
+
17
+ it_should_be_a_valid_instance
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, '#validates_acceptance_of' do
4
+ include Spec::Shared::DSL
5
+
6
+ before { builder.validates_acceptance_of attribute_name }
7
+
8
+ describe 'when attribute value is accepted' do
9
+ let(:attribute_value) { true }
10
+
11
+ it_should_be_a_valid_instance
12
+ end
13
+
14
+ describe 'when attribute value is not accepted' do
15
+ let(:attribute_value) { false }
16
+
17
+ it_should_be_an_invalid_instance
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, '#validates_confirmation_of' do
4
+ include Spec::Shared::DSL
5
+
6
+ let(:confirmation_attribute_name) { "#{attribute_name}_explicit_confirmation" }
7
+
8
+ before do
9
+ class_under_test.send(:attr_accessor, confirmation_attribute_name)
10
+ builder.validates_confirmation_of(attribute_name, :confirm => confirmation_attribute_name)
11
+ resource.send("#{confirmation_attribute_name}=", confirmation_value)
12
+ end
13
+
14
+ describe 'when confirmation value matches' do
15
+ let(:attribute_value) { :foo }
16
+ let(:confirmation_value) { :foo }
17
+
18
+ it_should_be_a_valid_instance
19
+ end
20
+
21
+ describe 'when confirmation value does not match' do
22
+ let(:attribute_value) { :foo }
23
+ let(:confirmation_value) { :bar }
24
+
25
+ it_should_be_an_invalid_instance
26
+ end
27
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, '#validates_format_of' do
4
+ include Spec::Shared::DSL
5
+
6
+ describe 'with a Proc' do
7
+ let(:attribute_value) { mock }
8
+
9
+ before do
10
+ builder.validates_format_of attribute_name, :format => lambda { |value| proc_return }
11
+ end
12
+
13
+ describe 'when format proc returns true' do
14
+ let(:proc_return) { true }
15
+
16
+ it_should_be_a_valid_instance
17
+ end
18
+
19
+ describe 'when validated attribute is absent' do
20
+ let(:proc_return) { false }
21
+
22
+ it_should_be_an_invalid_instance
23
+ end
24
+ end
25
+
26
+ describe 'with a Regexp' do
27
+ before do
28
+ builder.validates_format_of attribute_name, :format => /foo/
29
+ end
30
+
31
+ describe 'when validated attribute is present' do
32
+ let(:attribute_value) { 'foo' }
33
+
34
+ it_should_be_a_valid_instance
35
+ end
36
+
37
+ describe 'when validated attribute is absent' do
38
+ let(:attribute_value) { 'bar' }
39
+
40
+ it_should_be_an_invalid_instance
41
+ end
42
+ end
43
+
44
+ describe 'with :url' do
45
+ before do
46
+ builder.validates_format_of attribute_name, :format => :url
47
+ end
48
+
49
+ describe 'when validated attribute is a URL' do
50
+ let(:attribute_value) { 'http://www.example.com' }
51
+
52
+ it_should_be_a_valid_instance
53
+ end
54
+
55
+ describe 'when validated attribute is not a URL' do
56
+ let(:attribute_value) { 'not a url' }
57
+
58
+ it_should_be_an_invalid_instance
59
+ end
60
+ end
61
+
62
+ describe 'with :email_address' do
63
+ before do
64
+ builder.validates_format_of attribute_name, :format => :email_address
65
+ end
66
+
67
+ describe 'when validated attribute is an email address' do
68
+ let(:attribute_value) { 'address@example.com' }
69
+
70
+ it_should_be_a_valid_instance
71
+ end
72
+
73
+ describe 'when validated attribute is not an email address' do
74
+ let(:attribute_value) { 'not an email' }
75
+
76
+ it_should_be_an_invalid_instance
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, '#validates_inclusion_of' do
4
+ include Spec::Shared::DSL
5
+
6
+ before do
7
+ builder.validates_inclusion_of attribute_name, :within => Set[*set]
8
+ end
9
+
10
+ let(:set) { [:a, :b, :c] }
11
+
12
+ describe 'when validated attribute value is included in the set' do
13
+ let(:attribute_value) { set.first }
14
+
15
+ it_should_be_a_valid_instance
16
+ end
17
+
18
+ describe 'when validated attribute value is not included in the set' do
19
+ let(:attribute_value) { :foo }
20
+
21
+ it_should_be_an_invalid_instance
22
+ end
23
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, '#validates_length_of' do
4
+ include Spec::Shared::DSL
5
+
6
+ describe 'with fixnum :length option' do
7
+ before do
8
+ builder.validates_length_of attribute_name, :length => 3
9
+ end
10
+
11
+ describe 'when validated attribute value is expected length' do
12
+ let(:attribute_value) { 'foo' }
13
+
14
+ it_should_be_a_valid_instance
15
+ end
16
+
17
+ describe 'when validated attribute value is not expected length' do
18
+ let(:attribute_value) { 'barz' }
19
+
20
+ it_should_be_an_invalid_instance
21
+ end
22
+ end
23
+
24
+ describe 'with :maximum option' do
25
+ before do
26
+ builder.validates_length_of attribute_name, :maximum => 3
27
+ end
28
+
29
+ describe 'when validated attribute value is at most expected length' do
30
+ let(:attribute_value) { 'foo' }
31
+
32
+ it_should_be_a_valid_instance
33
+ end
34
+
35
+ describe 'when validated attribute value is more than expected length' do
36
+ let(:attribute_value) { 'barz' }
37
+
38
+ it_should_be_an_invalid_instance
39
+ end
40
+ end
41
+
42
+ describe 'with :minimum option' do
43
+ before do
44
+ builder.validates_length_of attribute_name, :minimum => 3
45
+ end
46
+
47
+ describe 'when validated attribute value is at least expected length' do
48
+ let(:attribute_value) { 'foo' }
49
+
50
+ it_should_be_a_valid_instance
51
+ end
52
+
53
+ describe 'when validated attribute value is less than expected length' do
54
+ let(:attribute_value) { 'bz' }
55
+
56
+ it_should_be_an_invalid_instance
57
+ end
58
+ end
59
+
60
+ describe 'with range :length options' do
61
+ before do
62
+ builder.validates_length_of attribute_name, :length => 2..3
63
+ end
64
+
65
+ describe 'when validated attribute value length is within expected range' do
66
+ let(:attribute_value) { 'foo' }
67
+
68
+ it_should_be_a_valid_instance
69
+ end
70
+
71
+ describe 'when validated attribute value length is not within expected range' do
72
+ let(:attribute_value) { 'barz' }
73
+
74
+ it_should_be_an_invalid_instance
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, '#validates_presence_of' do
4
+ include Spec::Shared::DSL
5
+
6
+ before { builder.validates_presence_of attribute_name }
7
+
8
+ describe 'when validated attribute is present' do
9
+ let(:attribute_value) { :foo }
10
+
11
+ it_should_be_a_valid_instance
12
+ end
13
+
14
+ describe 'when validated attribute is absent' do
15
+ let(:attribute_value) { nil }
16
+
17
+ it_should_be_an_invalid_instance
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vanguard::DSL, '#validates_value_of' do
4
+ include Spec::Shared::DSL
5
+
6
+ before do
7
+ pending
8
+ end
9
+
10
+
11
+ describe 'with a lambda that returns lower and upper bounds' do
12
+ before do
13
+ builder.validates_value_of attribute_name, :in => bound
14
+ end
15
+
16
+ let(:bound) { lambda { (Date.today - 5)..Date.today } }
17
+
18
+ describe 'when validated attribute value is within the range' do
19
+ let(:attribute_value) { bound.call.begin + 1 }
20
+
21
+ it_should_be_a_valid_instance
22
+ end
23
+
24
+ describe 'when validated attribute value is not within the range' do
25
+ let(:attribute_value) { bound.call.end + 1 }
26
+
27
+ it_should_be_an_invalid_instance
28
+ end
29
+ end
30
+ end