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 CHANGED
@@ -1,3 +1,9 @@
1
+ v0.1.3
2
+ -------
3
+
4
+ * Don't run validations if the object is already validated
5
+ * #errors validates the object
6
+
1
7
  v0.1.2
2
8
  ------
3
9
 
data/README.md CHANGED
@@ -24,9 +24,6 @@ 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
29
-
30
27
  def initialize(*digits)
31
28
  @digits = digits
32
29
  end
@@ -6,5 +6,5 @@
6
6
  # See LICENSE for license
7
7
 
8
8
  module SimpleValidation
9
- VERSION = "0.1.2"
9
+ VERSION = "0.1.3"
10
10
  end
@@ -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
- # Runs all validations and returns _true_ if the object is valid
83
- def valid?
84
- self.class.validation_methods.each do |method_name|
85
- send method_name
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 unless errors.include?(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 :always_valid_invoked
20
- attr_reader :always_invalid_invoked
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
- @always_valid_invoked = false
27
- @always_invalid_invoked = false
26
+ @always_valid_invoked_count = 0
27
+ @always_invalid_invoked_count = 0
28
28
  end
29
29
 
30
30
  def always_valid
31
- @always_valid_invoked = true
31
+ @always_valid_invoked_count += 1
32
32
  end
33
33
 
34
34
  def always_invalid
35
- @always_invalid_invoked = true
35
+ @always_invalid_invoked_count += 1
36
36
  add_error "Always invalid"
37
37
  end
38
38
  end
39
39
 
40
- describe "An entity that can be validated" do
41
- describe "when it has errors" do
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 "when it doesn't have any error" do
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 "when it has a custom validation" do
78
+ describe "with custom validation" do
83
79
  before do
84
80
  @entity = TestEntityWithValidation.new
85
81
  end
86
82
 
87
- describe "on validation" do
83
+ describe "#valid?" do
88
84
  before do
89
85
  @entity.valid?
90
86
  end
91
87
 
92
- it "invokes the validations" do
93
- @entity.always_valid_invoked.must_equal true
94
- @entity.always_invalid_invoked.must_equal true
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
- describe "with already existing errors" do
103
- it "doesn't lose its older errors on validation" do
104
- @entity = TestEntityWithValidation.new
105
- @entity.add_errors(["An error", "Another error"])
106
- @entity.valid?
107
- @entity.errors.must_equal ["An error", "Another error", "Always invalid"]
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 "when double validated" do
112
- it "doesn't duplicate its errors" do
113
- @entity = TestEntityWithValidation.new
114
- @entity.valid?
115
- @entity.valid?
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 be added an array of errors" do
122
- @entity = TestEntity.new
123
- @entity.add_errors(["An error", "Another error"])
124
- @entity.errors.must_equal ["An error", "Another error"]
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.2
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-20 00:00:00.000000000 Z
12
+ date: 2012-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest