simple_validation 0.1.2 → 0.1.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.
- data/CHANGELOG.md +6 -0
- data/README.md +0 -3
- data/lib/simple_validation/version.rb +1 -1
- data/lib/simple_validation.rb +13 -8
- data/spec/simple_validation_spec.rb +65 -34
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
data/lib/simple_validation.rb
CHANGED
@@ -13,9 +13,6 @@
|
|
13
13
|
# class AlienNumber
|
14
14
|
# include SimpleValidation
|
15
15
|
#
|
16
|
-
# validate :digits_are_positive
|
17
|
-
# validate :digits_are_less_than_ten
|
18
|
-
#
|
19
16
|
# def initialize(*digits)
|
20
17
|
# @digits = digits
|
21
18
|
# end
|
@@ -79,12 +76,19 @@ module SimpleValidation
|
|
79
76
|
end
|
80
77
|
end
|
81
78
|
|
82
|
-
#
|
83
|
-
def
|
84
|
-
|
85
|
-
|
79
|
+
# Run all validations associated with the object
|
80
|
+
def validate # :nodoc:
|
81
|
+
unless @validated
|
82
|
+
@validated = true
|
83
|
+
self.class.validation_methods.each do |method_name|
|
84
|
+
send method_name
|
85
|
+
end
|
86
86
|
end
|
87
|
+
end
|
87
88
|
|
89
|
+
# Runs all validations and returns _true_ if the object is valid
|
90
|
+
def valid?
|
91
|
+
validate
|
88
92
|
errors.empty?
|
89
93
|
end
|
90
94
|
|
@@ -95,7 +99,7 @@ module SimpleValidation
|
|
95
99
|
|
96
100
|
# Adds an error to the errors collection
|
97
101
|
def add_error(error)
|
98
|
-
errors << error
|
102
|
+
errors << error
|
99
103
|
end
|
100
104
|
|
101
105
|
# Adds an array of errors to the errors collection
|
@@ -105,6 +109,7 @@ module SimpleValidation
|
|
105
109
|
|
106
110
|
# Returns an array of the current errors
|
107
111
|
def errors
|
112
|
+
validate
|
108
113
|
@errors ||= []
|
109
114
|
end
|
110
115
|
end
|
@@ -16,29 +16,29 @@ end
|
|
16
16
|
class TestEntityWithValidation
|
17
17
|
include SimpleValidation
|
18
18
|
|
19
|
-
attr_reader :
|
20
|
-
attr_reader :
|
19
|
+
attr_reader :always_valid_invoked_count
|
20
|
+
attr_reader :always_invalid_invoked_count
|
21
21
|
|
22
22
|
validate :always_valid
|
23
23
|
validate :always_invalid
|
24
24
|
|
25
25
|
def initialize
|
26
|
-
@
|
27
|
-
@
|
26
|
+
@always_valid_invoked_count = 0
|
27
|
+
@always_invalid_invoked_count = 0
|
28
28
|
end
|
29
29
|
|
30
30
|
def always_valid
|
31
|
-
@
|
31
|
+
@always_valid_invoked_count += 1
|
32
32
|
end
|
33
33
|
|
34
34
|
def always_invalid
|
35
|
-
@
|
35
|
+
@always_invalid_invoked_count += 1
|
36
36
|
add_error "Always invalid"
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
describe "
|
41
|
-
describe "
|
40
|
+
describe "A validatable entity" do
|
41
|
+
describe "with an error" do
|
42
42
|
before do
|
43
43
|
@entity = TestEntity.new
|
44
44
|
@entity.add_error("An error")
|
@@ -55,13 +55,9 @@ describe "An entity that can be validated" do
|
|
55
55
|
it "exposes its errors" do
|
56
56
|
@entity.errors.must_equal ["An error"]
|
57
57
|
end
|
58
|
-
|
59
|
-
it "allows custom validation" do
|
60
|
-
@entity.errors.must_equal ["An error"]
|
61
|
-
end
|
62
58
|
end
|
63
59
|
|
64
|
-
describe "
|
60
|
+
describe "whithout any errors" do
|
65
61
|
before do
|
66
62
|
@entity = TestEntity.new
|
67
63
|
end
|
@@ -79,48 +75,83 @@ describe "An entity that can be validated" do
|
|
79
75
|
end
|
80
76
|
end
|
81
77
|
|
82
|
-
describe "
|
78
|
+
describe "with custom validation" do
|
83
79
|
before do
|
84
80
|
@entity = TestEntityWithValidation.new
|
85
81
|
end
|
86
82
|
|
87
|
-
describe "
|
83
|
+
describe "#valid?" do
|
88
84
|
before do
|
89
85
|
@entity.valid?
|
90
86
|
end
|
91
87
|
|
92
|
-
it "invokes
|
93
|
-
@entity.
|
94
|
-
@entity.
|
88
|
+
it "invokes its validations" do
|
89
|
+
@entity.always_valid_invoked_count.must_equal 1
|
90
|
+
@entity.always_invalid_invoked_count.must_equal 1
|
95
91
|
end
|
96
92
|
|
97
93
|
it "has errors added during validation" do
|
98
94
|
@entity.errors.must_equal ["Always invalid"]
|
99
95
|
end
|
100
|
-
end
|
101
96
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
97
|
+
describe "on another #valid?" do
|
98
|
+
before do
|
99
|
+
@entity.valid?
|
100
|
+
end
|
101
|
+
|
102
|
+
it "doesn't duplicate its errors" do
|
103
|
+
@entity.errors.must_equal ["Always invalid"]
|
104
|
+
end
|
105
|
+
|
106
|
+
it "doesn't re-run validation" do
|
107
|
+
@entity.always_valid_invoked_count.must_equal 1
|
108
|
+
@entity.always_invalid_invoked_count.must_equal 1
|
109
|
+
end
|
108
110
|
end
|
109
111
|
end
|
110
112
|
|
111
|
-
describe "
|
112
|
-
|
113
|
-
@entity
|
114
|
-
|
115
|
-
|
113
|
+
describe "#errors" do
|
114
|
+
before do
|
115
|
+
@entity.errors
|
116
|
+
end
|
117
|
+
|
118
|
+
it "invokes its validations" do
|
119
|
+
@entity.always_valid_invoked_count.must_equal 1
|
120
|
+
@entity.always_invalid_invoked_count.must_equal 1
|
121
|
+
end
|
122
|
+
|
123
|
+
it "has errors added during validation" do
|
116
124
|
@entity.errors.must_equal ["Always invalid"]
|
117
125
|
end
|
126
|
+
|
127
|
+
describe "on another #error" do
|
128
|
+
before do
|
129
|
+
@entity.errors
|
130
|
+
end
|
131
|
+
|
132
|
+
it "doesn't duplicate its errors" do
|
133
|
+
@entity.errors.must_equal ["Always invalid"]
|
134
|
+
end
|
135
|
+
|
136
|
+
it "doesn't re-run validation" do
|
137
|
+
@entity.always_valid_invoked_count.must_equal 1
|
138
|
+
@entity.always_invalid_invoked_count.must_equal 1
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "with already existing errors" do
|
145
|
+
it "doesn't lose its older errors on validation" do
|
146
|
+
entity = TestEntityWithValidation.new
|
147
|
+
entity.add_errors(["An error", "Another error"])
|
148
|
+
entity.errors.sort.must_equal ["An error", "Another error", "Always invalid"].sort
|
118
149
|
end
|
119
150
|
end
|
120
151
|
|
121
|
-
it "can
|
122
|
-
|
123
|
-
|
124
|
-
|
152
|
+
it "can accept multiple errors" do
|
153
|
+
entity = TestEntity.new
|
154
|
+
entity.add_errors(["An error", "Another error"])
|
155
|
+
entity.errors.must_equal ["An error", "Another error"]
|
125
156
|
end
|
126
157
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|