whyvalidationssuckin96 1.5.2 → 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/whyvalidationssuckin96/macros/validates_exclusion.rb +22 -10
- data/lib/whyvalidationssuckin96/macros/validates_inclusion.rb +22 -10
- data/test/macros/validates_exclusion_test.rb +28 -8
- data/test/macros/validates_inclusion_test.rb +28 -8
- data/test/validation_test.rb +20 -20
- data/whyvalidationssuckin96.gemspec +2 -2
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.5.
|
1
|
+
1.5.3
|
@@ -2,30 +2,42 @@ require 'whyvalidationssuckin96/skippable_validation'
|
|
2
2
|
require 'whyvalidationssuckin96/attribute_based_validation'
|
3
3
|
|
4
4
|
module WhyValidationsSuckIn96
|
5
|
-
|
5
|
+
|
6
6
|
# Checks the validity of an attribute against a collection of excluded values.
|
7
7
|
#
|
8
8
|
# @example Default usage
|
9
9
|
# setup_validations do
|
10
10
|
# validates_exclusion_of :subdomain, :in => %w[www ftp]
|
11
11
|
# end
|
12
|
-
|
12
|
+
#
|
13
|
+
# @example Usage when checking a set of values against another set
|
14
|
+
# setup_validations do
|
15
|
+
# validates_exclusion_of :colours, :in => %w[red green], :set => true
|
16
|
+
# # colours can now be an array containing anything except 'red' or 'green'
|
17
|
+
# end
|
18
|
+
class ValidatesExclusion < Validation
|
13
19
|
include WhyValidationsSuckIn96::SkippableValidation
|
14
20
|
include WhyValidationsSuckIn96::AttributeBasedValidation
|
15
|
-
|
16
|
-
DefaultOptions = {:message => "is in the excluded collection"
|
17
|
-
|
18
|
-
|
19
|
-
# @param [
|
20
|
-
# @
|
21
|
+
|
22
|
+
DefaultOptions = {:message => "is in the excluded collection",
|
23
|
+
:set => false}
|
24
|
+
|
25
|
+
# @param [Object] validatable An object to be validated.
|
26
|
+
# @param [Hash] options The options to set up the validation with.
|
27
|
+
# @option options [#include?] :in A collection to check against for exclusion.
|
28
|
+
# @option options [true, false] :set Whether or not to do a set based comparison
|
21
29
|
def initialize(validatable, options = {})
|
22
30
|
super
|
23
31
|
raise(ArgumentError, "Collection to check for exclusion against should be specified with :in") unless options[:in]
|
24
32
|
end
|
25
|
-
|
33
|
+
|
26
34
|
def validate
|
27
35
|
super
|
28
|
-
if options[:
|
36
|
+
if options[:set]
|
37
|
+
attribute_value.any? do |val|
|
38
|
+
options[:in].include?(val)
|
39
|
+
end ? fail : pass
|
40
|
+
elsif !options[:set] && options[:in].include?(attribute_value)
|
29
41
|
fail
|
30
42
|
else
|
31
43
|
pass
|
@@ -2,30 +2,42 @@ require 'whyvalidationssuckin96/skippable_validation'
|
|
2
2
|
require 'whyvalidationssuckin96/attribute_based_validation'
|
3
3
|
|
4
4
|
module WhyValidationsSuckIn96
|
5
|
-
|
5
|
+
|
6
6
|
# Checks the validity of an attribute against a list of values for it to be included in.
|
7
7
|
#
|
8
8
|
# @example Default usage
|
9
9
|
# setup_validations do
|
10
10
|
# validates_inclusion_of :unit_system, :in => %w[imperial metric]
|
11
11
|
# end
|
12
|
-
|
12
|
+
#
|
13
|
+
# @example Usage when checking a set of values against another set
|
14
|
+
# setup_validations do
|
15
|
+
# validates_inclusion_of :colours, :in => %w[red green], :set => true
|
16
|
+
# # colours can now be an array containing either 'red' or 'green' or both
|
17
|
+
# end
|
18
|
+
class ValidatesInclusion < Validation
|
13
19
|
include WhyValidationsSuckIn96::SkippableValidation
|
14
20
|
include WhyValidationsSuckIn96::AttributeBasedValidation
|
15
|
-
|
16
|
-
DefaultOptions = {:message => "is not in the acceptable collection"
|
17
|
-
|
18
|
-
|
19
|
-
# @param [
|
20
|
-
# @
|
21
|
+
|
22
|
+
DefaultOptions = {:message => "is not in the acceptable collection",
|
23
|
+
:set => false}
|
24
|
+
|
25
|
+
# @param [Object] validatable An object to be validated.
|
26
|
+
# @param [Hash] options The options to set up the validation with.
|
27
|
+
# @option options [#include?] :in A collection to check against for inclusion.
|
28
|
+
# @option options [true, false] :set Whether or not to do a set based comparison
|
21
29
|
def initialize(validatable, options = {})
|
22
30
|
super
|
23
31
|
raise(ArgumentError, "Collection to check for inclusion against should be specified with :in") unless options[:in]
|
24
32
|
end
|
25
|
-
|
33
|
+
|
26
34
|
def validate
|
27
35
|
super
|
28
|
-
if options[:
|
36
|
+
if options[:set]
|
37
|
+
attribute_value.any? && attribute_value.all? do |val|
|
38
|
+
options[:in].include?(val)
|
39
|
+
end ? pass : fail
|
40
|
+
elsif !options[:set] && options[:in].include?(attribute_value)
|
29
41
|
pass
|
30
42
|
else
|
31
43
|
fail
|
@@ -1,37 +1,57 @@
|
|
1
1
|
require 'teststrap'
|
2
2
|
|
3
3
|
context "validates exclusion" do
|
4
|
-
|
4
|
+
|
5
5
|
should "add a validation macro" do
|
6
6
|
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
7
|
end.includes('validates_exclusion_of')
|
8
|
-
|
8
|
+
|
9
9
|
should "raise if an :in option is not given" do
|
10
|
-
WhyValidationsSuckIn96::ValidatesExclusion.new(Object.new, :attribute => :colour)
|
10
|
+
WhyValidationsSuckIn96::ValidatesExclusion.new(Object.new, :attribute => :colour)
|
11
11
|
end.raises(ArgumentError, "Collection to check for exclusion against should be specified with :in")
|
12
|
-
|
12
|
+
|
13
13
|
context "with some default options" do
|
14
14
|
setup do
|
15
15
|
WhyValidationsSuckIn96::ValidatesExclusion.new(Object.new, :attribute => :colour, :in => %w[1 2 3])
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
should "have a message accessor with a default message" do
|
19
19
|
topic.message
|
20
20
|
end.equals("is in the excluded collection")
|
21
21
|
end # with some default options
|
22
|
-
|
22
|
+
|
23
23
|
context "validating an object" do
|
24
|
-
|
24
|
+
|
25
25
|
should "fail if the attribute is in the excluded set" do
|
26
26
|
validation = WhyValidationsSuckIn96::ValidatesExclusion.new(OpenStruct.new(:colour => "red"), :attribute => :colour,
|
27
27
|
:in => %w[red blue])
|
28
28
|
validation.validates?
|
29
29
|
end.equals(false)
|
30
|
-
|
30
|
+
|
31
31
|
should "pass if the attribute is out of the excluded set" do
|
32
32
|
validation = WhyValidationsSuckIn96::ValidatesExclusion.new(OpenStruct.new(:colour => "red"), :attribute => :colour,
|
33
33
|
:in => %w[green blue])
|
34
34
|
validation.validates?
|
35
35
|
end
|
36
36
|
end # validating an object
|
37
|
+
|
38
|
+
context "validating an object with the :set option given" do
|
39
|
+
|
40
|
+
should "fail if any of the attributes are in the given set" do
|
41
|
+
validation = WhyValidationsSuckIn96::ValidatesExclusion.new(OpenStruct.new(:colours => %w[red blue yellow]),
|
42
|
+
:attribute => :colours,
|
43
|
+
:set => true,
|
44
|
+
:in => %w[blue green])
|
45
|
+
validation.validates?
|
46
|
+
end.equals(false)
|
47
|
+
|
48
|
+
should "pass if all the attributes are not in the given set" do
|
49
|
+
validation = WhyValidationsSuckIn96::ValidatesExclusion.new(OpenStruct.new(:colours => %w[yellow purple]),
|
50
|
+
:attribute => :colours,
|
51
|
+
:set => true,
|
52
|
+
:in => %w[red green blue])
|
53
|
+
validation.validates?
|
54
|
+
end
|
55
|
+
end # validating an object
|
56
|
+
|
37
57
|
end # validates exclusion
|
@@ -1,37 +1,57 @@
|
|
1
1
|
require 'teststrap'
|
2
2
|
|
3
3
|
context "validates inclusion" do
|
4
|
-
|
4
|
+
|
5
5
|
should "add a validation macro" do
|
6
6
|
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
7
|
end.includes('validates_inclusion_of')
|
8
|
-
|
8
|
+
|
9
9
|
should "raise if an :in option is not given" do
|
10
|
-
WhyValidationsSuckIn96::ValidatesInclusion.new(Object.new, :attribute => :colour)
|
10
|
+
WhyValidationsSuckIn96::ValidatesInclusion.new(Object.new, :attribute => :colour)
|
11
11
|
end.raises(ArgumentError, "Collection to check for inclusion against should be specified with :in")
|
12
|
-
|
12
|
+
|
13
13
|
context "with some default options" do
|
14
14
|
setup do
|
15
15
|
WhyValidationsSuckIn96::ValidatesInclusion.new(Object.new, :attribute => :colour, :in => %w[1 2 3])
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
should "have a message accessor with a default message" do
|
19
19
|
topic.message
|
20
20
|
end.equals("is not in the acceptable collection")
|
21
21
|
end # with some default options
|
22
|
-
|
22
|
+
|
23
23
|
context "validating an object" do
|
24
|
-
|
24
|
+
|
25
25
|
should "fail if the attribute is not in the given set" do
|
26
26
|
validation = WhyValidationsSuckIn96::ValidatesInclusion.new(OpenStruct.new(:colour => "red"), :attribute => :colour,
|
27
27
|
:in => %w[blue green])
|
28
28
|
validation.validates?
|
29
29
|
end.equals(false)
|
30
|
-
|
30
|
+
|
31
31
|
should "pass if the attribute is in the given set" do
|
32
32
|
validation = WhyValidationsSuckIn96::ValidatesInclusion.new(OpenStruct.new(:colour => "red"), :attribute => :colour,
|
33
33
|
:in => %w[red green blue])
|
34
34
|
validation.validates?
|
35
35
|
end
|
36
36
|
end # validating an object
|
37
|
+
|
38
|
+
context "validating an object with the :set option given" do
|
39
|
+
|
40
|
+
should "fail if any of the attributes are not in the given set" do
|
41
|
+
validation = WhyValidationsSuckIn96::ValidatesInclusion.new(OpenStruct.new(:colours => %w[red blue yellow]),
|
42
|
+
:attribute => :colours,
|
43
|
+
:set => true,
|
44
|
+
:in => %w[blue green])
|
45
|
+
validation.validates?
|
46
|
+
end.equals(false)
|
47
|
+
|
48
|
+
should "pass if all the attributes are in the given set" do
|
49
|
+
validation = WhyValidationsSuckIn96::ValidatesInclusion.new(OpenStruct.new(:colours => %w[red green]),
|
50
|
+
:attribute => :colours,
|
51
|
+
:set => true,
|
52
|
+
:in => %w[red green blue])
|
53
|
+
validation.validates?
|
54
|
+
end
|
55
|
+
end # validating an object
|
56
|
+
|
37
57
|
end # validates inclusion
|
data/test/validation_test.rb
CHANGED
@@ -5,39 +5,39 @@ context "validation" do
|
|
5
5
|
setup do
|
6
6
|
WhyValidationsSuckIn96::Validation.new_subclass(:validates_rockingness, lambda {})
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
should "have a readable string returned by inspect" do
|
10
10
|
topic.new(Object.new).inspect
|
11
|
-
end.
|
12
|
-
|
11
|
+
end.matches(/^#<WhyValidationsSuckIn96::Validation subclass for validating 'validates_rockingness'>/)
|
12
|
+
|
13
13
|
should "have a name on the new subclass" do
|
14
14
|
topic.name
|
15
15
|
end.equals(:validates_rockingness)
|
16
|
-
|
16
|
+
|
17
17
|
should "define validate as private on the new subclass" do
|
18
18
|
topic.private_instance_methods
|
19
|
-
end.includes("validate")
|
19
|
+
end.includes("validate")
|
20
20
|
end # creating a new subclass
|
21
|
-
|
21
|
+
|
22
22
|
context "an instance of a subclass" do
|
23
23
|
setup do
|
24
24
|
WhyValidationsSuckIn96::Validation.new_subclass(:validates_rockingness, lambda {})
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
should "allow accessing the options via an instance method" do
|
28
28
|
topic.new(Object.new, :example => "stuff").options
|
29
29
|
end.equals(:example => "stuff")
|
30
|
-
|
30
|
+
|
31
31
|
should "return a true by default when pass/fail aren't called in the validation definition" do
|
32
32
|
topic.new(Object.new).validates?
|
33
33
|
end.equals(true)
|
34
|
-
|
34
|
+
|
35
35
|
should "be passed by default when pass/fail aren't called in the validation definition" do
|
36
36
|
validation = topic.new(Object.new)
|
37
37
|
validation.validates?
|
38
38
|
validation.passed?
|
39
39
|
end.equals(true)
|
40
|
-
|
40
|
+
|
41
41
|
should "not be passed if the validation hasn't run yet" do
|
42
42
|
topic.new(Object.new).passed?
|
43
43
|
end.equals(false)
|
@@ -45,13 +45,13 @@ context "validation" do
|
|
45
45
|
should "not be failed if the validation hasn't run yet" do
|
46
46
|
topic.new(Object.new).failed?
|
47
47
|
end.equals(false)
|
48
|
-
|
48
|
+
|
49
49
|
should "not return true for has_run? if the validation hasn't run yet" do
|
50
50
|
topic.new(Object.new).has_run?
|
51
51
|
end.equals(false)
|
52
|
-
|
52
|
+
|
53
53
|
end # an instance of a subclass
|
54
|
-
|
54
|
+
|
55
55
|
context "an instance of a subclass with an actual validation definition" do
|
56
56
|
setup do
|
57
57
|
validation = lambda { validatable ? pass : fail }
|
@@ -62,40 +62,40 @@ context "validation" do
|
|
62
62
|
validation = topic.new(true)
|
63
63
|
validation.validates?
|
64
64
|
end.equals(true)
|
65
|
-
|
65
|
+
|
66
66
|
should "be passed when run and pass is called" do
|
67
67
|
validation = topic.new(true)
|
68
68
|
validation.validates?
|
69
69
|
validation.passed?
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
should "have has_run? return true when the validation runs" do
|
73
73
|
validation = topic.new(true)
|
74
74
|
validation.validates?
|
75
75
|
validation.has_run?
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
should "have validates? return false if the validation fails" do
|
79
79
|
validation = topic.new(false)
|
80
80
|
validation.validates?
|
81
81
|
end.equals(false)
|
82
|
-
|
82
|
+
|
83
83
|
should "be failed when run and fail is called" do
|
84
84
|
validation = topic.new(false)
|
85
85
|
validation.validates?
|
86
86
|
validation.failed?
|
87
87
|
end
|
88
88
|
end # an instance of a subclass with an actual validation definition
|
89
|
-
|
89
|
+
|
90
90
|
context "an instance of a subclass with a validation definition that raises" do
|
91
91
|
setup do
|
92
92
|
validation = lambda { raise "hell" }
|
93
93
|
WhyValidationsSuckIn96::Validation.new_subclass(:validates_rockingness, validation)
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
should "raise an exception when validating" do
|
97
97
|
topic.new(Object.new).validates?
|
98
98
|
end.raises(RuntimeError, "hell")
|
99
|
-
|
99
|
+
|
100
100
|
end # an instance of a subclass with a validation definition that raises
|
101
101
|
end # validation
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{whyvalidationssuckin96}
|
8
|
-
s.version = "1.5.
|
8
|
+
s.version = "1.5.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["gabrielg", "douglasmeyer"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-03-05}
|
13
13
|
s.description = %q{A library for setting up model validations, such as in ActiveRecord.}
|
14
14
|
s.email = %q{gabriel.gironda@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: whyvalidationssuckin96
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gabrielg
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2010-
|
13
|
+
date: 2010-03-05 00:00:00 -06:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|