simple_validation 0.1.3 → 0.1.4
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 +5 -0
- data/README.md +14 -1
- data/lib/simple_validation.rb +6 -6
- data/lib/simple_validation/version.rb +1 -1
- data/spec/simple_validation_spec.rb +62 -0
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -24,6 +24,9 @@ require "simple_validation"
|
|
24
24
|
class AlienNumber
|
25
25
|
include SimpleValidation
|
26
26
|
|
27
|
+
validate :digits_are_positive
|
28
|
+
validate :digits_are_less_than_ten, :if => [:digits_count_even?]
|
29
|
+
|
27
30
|
def initialize(*digits)
|
28
31
|
@digits = digits
|
29
32
|
end
|
@@ -45,6 +48,10 @@ class AlienNumber
|
|
45
48
|
add_error("#{digit} is greater than 9") unless digit < 10
|
46
49
|
end
|
47
50
|
end
|
51
|
+
|
52
|
+
def digits_count_even?
|
53
|
+
@digits.size.even?
|
54
|
+
end
|
48
55
|
end
|
49
56
|
|
50
57
|
valid_number = AlienNumber.new(1, 2, 3)
|
@@ -56,8 +63,14 @@ valid_number.value # 6
|
|
56
63
|
invalid_number = AlienNumber.new(-1, 12)
|
57
64
|
invalid_number.valid? # false
|
58
65
|
invalid_number.invalid? # true
|
59
|
-
invalid_number.errors # ["-1 is negative"
|
66
|
+
invalid_number.errors # ["-1 is negative"]
|
60
67
|
invalid_number.value # 11
|
68
|
+
|
69
|
+
another_invalid_number = AlienNumber.new(-10, 12, 15)
|
70
|
+
another_invalid_number.valid? # false
|
71
|
+
another_invalid_number.invalid? # true
|
72
|
+
another_invalid_number.errors # ["-1 is negative", "12 is greater than 9"]
|
73
|
+
another_invalid_number.value # 17
|
61
74
|
```
|
62
75
|
|
63
76
|
Running tests
|
data/lib/simple_validation.rb
CHANGED
@@ -64,15 +64,15 @@ module SimpleValidation
|
|
64
64
|
# include SimpleValidation
|
65
65
|
#
|
66
66
|
# validate :digits_are_positive
|
67
|
-
# validate :digits_are_less_than_ten
|
67
|
+
# validate :digits_are_less_than_ten, :if => [:digits_count_even?]
|
68
68
|
# end
|
69
|
-
def validate(method_name)
|
70
|
-
validation_methods
|
69
|
+
def validate(method_name, conditions = {})
|
70
|
+
validation_methods[method_name] = conditions[:if] || []
|
71
71
|
end
|
72
72
|
|
73
73
|
# The list of all method names that validate the object
|
74
74
|
def validation_methods # :nodoc:
|
75
|
-
@validation_methods ||=
|
75
|
+
@validation_methods ||= {}
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
@@ -80,8 +80,8 @@ module SimpleValidation
|
|
80
80
|
def validate # :nodoc:
|
81
81
|
unless @validated
|
82
82
|
@validated = true
|
83
|
-
self.class.validation_methods.each do |method_name|
|
84
|
-
send method_name
|
83
|
+
self.class.validation_methods.each do |method_name, conditions|
|
84
|
+
send method_name if conditions.all? { |condition| send condition }
|
85
85
|
end
|
86
86
|
end
|
87
87
|
end
|
@@ -37,6 +37,44 @@ class TestEntityWithValidation
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
class TestEntityWithConditionalValidation
|
41
|
+
include SimpleValidation
|
42
|
+
|
43
|
+
attr_reader :statement_one_invoked_count
|
44
|
+
attr_reader :statement_two_invoked_count
|
45
|
+
attr_reader :statement_three_invoked_count
|
46
|
+
|
47
|
+
validate :statement_one, :if => [:condition_one?]
|
48
|
+
validate :statement_two, :if => [:condition_two?]
|
49
|
+
validate :statement_three, :if => [:condition_one?, :condition_two?]
|
50
|
+
|
51
|
+
def initialize
|
52
|
+
@statement_one_invoked_count = 0
|
53
|
+
@statement_two_invoked_count = 0
|
54
|
+
@statement_three_invoked_count = 0
|
55
|
+
end
|
56
|
+
|
57
|
+
def statement_one
|
58
|
+
@statement_one_invoked_count += 1
|
59
|
+
end
|
60
|
+
|
61
|
+
def statement_two
|
62
|
+
@statement_two_invoked_count += 1
|
63
|
+
end
|
64
|
+
|
65
|
+
def statement_three
|
66
|
+
@statement_three_invoked_count += 1
|
67
|
+
end
|
68
|
+
|
69
|
+
def condition_one?
|
70
|
+
true
|
71
|
+
end
|
72
|
+
|
73
|
+
def condition_two?
|
74
|
+
false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
40
78
|
describe "A validatable entity" do
|
41
79
|
describe "with an error" do
|
42
80
|
before do
|
@@ -140,6 +178,30 @@ describe "A validatable entity" do
|
|
140
178
|
end
|
141
179
|
end
|
142
180
|
end
|
181
|
+
|
182
|
+
describe "with conditional validation" do
|
183
|
+
before do
|
184
|
+
@entity = TestEntityWithConditionalValidation.new
|
185
|
+
end
|
186
|
+
|
187
|
+
describe "#valid?" do
|
188
|
+
before do
|
189
|
+
@entity.valid?
|
190
|
+
end
|
191
|
+
|
192
|
+
it "invokes its validations that passes all conditions" do
|
193
|
+
@entity.statement_one_invoked_count.must_equal 1
|
194
|
+
end
|
195
|
+
|
196
|
+
it "doesn't invoke its validations that fail all conditions" do
|
197
|
+
@entity.statement_two_invoked_count.must_equal 0
|
198
|
+
end
|
199
|
+
|
200
|
+
it "doesn't invoke its validations that fail conditions partially" do
|
201
|
+
@entity.statement_three_invoked_count.must_equal 0
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
143
205
|
|
144
206
|
describe "with already existing errors" do
|
145
207
|
it "doesn't lose its older errors on validation" do
|
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.4
|
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-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|