simple_validation 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/README.md +3 -3
- data/lib/simple_validation/version.rb +1 -1
- data/lib/simple_validation.rb +12 -4
- data/simple_validation.gemspec +1 -0
- data/spec/simple_validation_spec.rb +26 -10
- metadata +4 -3
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -6,7 +6,7 @@ This is very simple gem to allow custom validations in a ruby object. You are su
|
|
6
6
|
Dependencies
|
7
7
|
------------
|
8
8
|
|
9
|
-
These are no dependencies for this gem. You need minitest to run the tests for this gem.
|
9
|
+
These are no runtime dependencies for this gem. You need minitest to run the tests for this gem.
|
10
10
|
|
11
11
|
Installation
|
12
12
|
------------
|
@@ -63,13 +63,13 @@ valid_number.value # 6
|
|
63
63
|
invalid_number = AlienNumber.new(-1, 12)
|
64
64
|
invalid_number.valid? # false
|
65
65
|
invalid_number.invalid? # true
|
66
|
-
invalid_number.errors # ["-1 is negative"]
|
66
|
+
invalid_number.errors # ["-1 is negative", "12 is greater than 9"]
|
67
67
|
invalid_number.value # 11
|
68
68
|
|
69
69
|
another_invalid_number = AlienNumber.new(-10, 12, 15)
|
70
70
|
another_invalid_number.valid? # false
|
71
71
|
another_invalid_number.invalid? # true
|
72
|
-
another_invalid_number.errors # ["-
|
72
|
+
another_invalid_number.errors # ["-10 is negative"]
|
73
73
|
another_invalid_number.value # 17
|
74
74
|
```
|
75
75
|
|
data/lib/simple_validation.rb
CHANGED
@@ -57,7 +57,8 @@ module SimpleValidation
|
|
57
57
|
|
58
58
|
module ClassMethods
|
59
59
|
# Add a validation method
|
60
|
-
# The object to validate be able to invoke the method supplied
|
60
|
+
# The object to validate should be able to invoke the method supplied
|
61
|
+
# You can pass a list of conditions that the object must satisfy for the validations to run
|
61
62
|
#
|
62
63
|
# Example:
|
63
64
|
# class AlienNumber
|
@@ -89,7 +90,7 @@ module SimpleValidation
|
|
89
90
|
# Runs all validations and returns _true_ if the object is valid
|
90
91
|
def valid?
|
91
92
|
validate
|
92
|
-
|
93
|
+
all_errors.empty?
|
93
94
|
end
|
94
95
|
|
95
96
|
# Runs all validations and returns _true_ if the object is invalid
|
@@ -99,17 +100,24 @@ module SimpleValidation
|
|
99
100
|
|
100
101
|
# Adds an error to the errors collection
|
101
102
|
def add_error(error)
|
102
|
-
|
103
|
+
all_errors << error
|
103
104
|
end
|
104
105
|
|
105
106
|
# Adds an array of errors to the errors collection
|
106
107
|
def add_errors(more_errors)
|
107
|
-
|
108
|
+
all_errors.concat(more_errors)
|
108
109
|
end
|
109
110
|
|
110
111
|
# Returns an array of the current errors
|
111
112
|
def errors
|
112
113
|
validate
|
114
|
+
all_errors
|
115
|
+
end
|
116
|
+
|
117
|
+
def all_errors # :nodoc:
|
113
118
|
@errors ||= []
|
119
|
+
@errors.uniq!
|
120
|
+
@errors
|
114
121
|
end
|
122
|
+
private :all_errors
|
115
123
|
end
|
data/simple_validation.gemspec
CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.description = <<-EOS
|
15
15
|
A simple way to validate custom ruby objects using validation defined in the class.
|
16
16
|
EOS
|
17
|
+
s.license = "MIT"
|
17
18
|
s.rubyforge_project = "simple_validation"
|
18
19
|
s.files = `git ls-files`.split("\n")
|
19
20
|
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
@@ -61,15 +61,15 @@ class TestEntityWithConditionalValidation
|
|
61
61
|
def statement_two
|
62
62
|
@statement_two_invoked_count += 1
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
def statement_three
|
66
66
|
@statement_three_invoked_count += 1
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
def condition_one?
|
70
70
|
true
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def condition_two?
|
74
74
|
false
|
75
75
|
end
|
@@ -178,7 +178,7 @@ describe "A validatable entity" do
|
|
178
178
|
end
|
179
179
|
end
|
180
180
|
end
|
181
|
-
|
181
|
+
|
182
182
|
describe "with conditional validation" do
|
183
183
|
before do
|
184
184
|
@entity = TestEntityWithConditionalValidation.new
|
@@ -206,14 +206,30 @@ describe "A validatable entity" do
|
|
206
206
|
describe "with already existing errors" do
|
207
207
|
it "doesn't lose its older errors on validation" do
|
208
208
|
entity = TestEntityWithValidation.new
|
209
|
-
entity.
|
210
|
-
entity.errors.sort.must_equal ["An error", "
|
209
|
+
entity.add_error("An error")
|
210
|
+
entity.errors.sort.must_equal ["An error", "Always invalid"].sort
|
211
|
+
end
|
212
|
+
|
213
|
+
it "doesn't duplicate errors" do
|
214
|
+
entity = TestEntityWithValidation.new
|
215
|
+
entity.add_error("An error")
|
216
|
+
entity.add_error("An error")
|
217
|
+
entity.errors.sort.must_equal ["Always invalid", "An error"].sort
|
211
218
|
end
|
212
219
|
end
|
213
220
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
221
|
+
describe "#add_error" do
|
222
|
+
it "can accept multiple errors" do
|
223
|
+
entity = TestEntity.new
|
224
|
+
entity.add_errors(["An error", "Another error"])
|
225
|
+
entity.errors.sort.must_equal ["An error", "Another error"].sort
|
226
|
+
end
|
227
|
+
|
228
|
+
it "doesn't duplicate errors" do
|
229
|
+
entity = TestEntity.new
|
230
|
+
entity.add_errors(["An error", "Another error"])
|
231
|
+
entity.add_errors(["An error", "Another error"])
|
232
|
+
entity.errors.sort.must_equal ["An error", "Another error"].sort
|
233
|
+
end
|
218
234
|
end
|
219
235
|
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.5
|
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-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
@@ -60,7 +60,8 @@ files:
|
|
60
60
|
- !binary |-
|
61
61
|
c3BlYy9zcGVjX2hlbHBlci5yYg==
|
62
62
|
homepage: https://github.com/chiku/simple_validation
|
63
|
-
licenses:
|
63
|
+
licenses:
|
64
|
+
- MIT
|
64
65
|
post_install_message:
|
65
66
|
rdoc_options: []
|
66
67
|
require_paths:
|