daily_affirmation 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44d20be607b16937aac801de19c1973221a42cf4
4
- data.tar.gz: 941f6d4296dde5aaa5fb754dcc418df3786dea44
3
+ metadata.gz: 067ab891f1fade3f4a26150786cfcc87ee472280
4
+ data.tar.gz: f05e7558ace028cfecd5b4d9d43220943bf1eaae
5
5
  SHA512:
6
- metadata.gz: ae1c976af688895ad58f82c7aa718a1fa4a7f719d43d0cca1fc1036049940d2a8efd515c4016bc73104e9142e019ca529d4e2fc7735e5b9b49dae31b0b5e9418
7
- data.tar.gz: 6531765cd121565299d0ad104584d60ed84a65c160e6be6f527a89b9efc59ce30283c77585e7cec23f7cf39a80415b602a03663102089dd5815046ab32fd94dc
6
+ metadata.gz: cb9978830c16cbce9d3d7e27c028ade54c5ffd3a567600e32a36c1598b1636b105392a44217f75654f09f80c392b063907bb8c7e196b5a0852496ede8dfb794c
7
+ data.tar.gz: e3288aef6c9dd1ff25b5742199dd2e184973e8fb10d6f4818c5f0f7facf3f8f5a084acb51e337731d3f6b3d900f21566f4dc1ca15a94bbb1422516458d78994f
data/Gemfile CHANGED
@@ -1,5 +1,8 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "coveralls", :require => false
3
+ group :test do
4
+ gem "coveralls", :require => false
5
+ gem "pry", :require => false
6
+ end
4
7
 
5
8
  gemspec
@@ -33,113 +33,21 @@ module DailyAffirmation
33
33
  end
34
34
 
35
35
  def affirm(affirmation)
36
- method = "affirm_#{affirmation[:type]}_of"
36
+ type = affirmation[:type]
37
37
  attribute = affirmation[:attribute]
38
38
  args = affirmation.reject { |k, _| [:type, :attribute].include?(k) }
39
- send(method, attribute, args)
40
- end
41
-
42
- def affirm_presence_of(attribute, _ = {})
43
- if present?(attribute)
44
- [true, nil]
45
- else
46
- [false, "#{attribute} can't be blank"]
47
- end
48
- end
49
-
50
- def affirm_absence_of(attribute, _ = {})
51
- if present?(attribute)
52
- [false, "#{attribute} must be blank"]
53
- else
54
- [true, nil]
55
- end
56
- end
57
-
58
- def affirm_inclusion_of(attribute, list: [])
59
- if list.include?(object.send(attribute))
60
- [true, nil]
61
- else
62
- [false, "#{attribute} is not included in #{list}"]
63
- end
64
- end
65
-
66
- def affirm_exclusion_of(attribute, list: [])
67
- if list.include?(object.send(attribute))
68
- [false, "#{attribute} is reserved"]
69
- else
70
- [true, nil]
71
- end
72
- end
73
39
 
74
- def affirm_acceptance_of(attribute, _ = {})
75
- if object.send(attribute)
76
- [true, nil]
77
- else
78
- [false, "#{attribute} must be accepted"]
79
- end
80
- end
81
-
82
- def affirm_confirmation_of(attribute, _ = {})
83
- if object.send(attribute) == object.send("#{attribute}_confirmation")
84
- [true, nil]
85
- else
86
- [false, "#{attribute} doesn't match confirmation"]
87
- end
88
- end
89
-
90
- def affirm_format_of(attribute, regex: //)
91
- if regex.match(object.send(attribute))
92
- [true, nil]
93
- else
94
- [false, "#{attribute} is invalid"]
95
- end
96
- end
97
-
98
- def affirm_length_of(attribute, range: 0..0)
99
- if range.include?(object.send(attribute).size)
100
- [true, nil]
101
- else
102
- [false, "#{attribute} is the wrong length (allowed: #{range})"]
103
- end
104
- end
105
-
106
- def affirm_numericality_of(attribute, _ = {})
107
- if object.send(attribute).is_a?(Numeric)
108
- [true, nil]
109
- else
110
- [false, "#{attribute} is not a number"]
111
- end
112
- end
113
-
114
- def blank?(attribute)
115
- value = object.send(attribute)
116
- case value
117
- when String
118
- value !~ /[^[:space:]]/
119
- else
120
- value.respond_to?(:empty?) ? value.empty? : !value
121
- end
122
- end
123
-
124
- def present?(attribute)
125
- !blank?(attribute)
40
+ validator = Object.const_get(
41
+ "DailyAffirmation::Validators::#{type.to_s.capitalize}Validator"
42
+ )
43
+ validator.new(object, attribute, args).affirm
126
44
  end
127
45
 
128
46
  module ClassMethods
129
- def affirms_presence_of(attribute)
130
- affirmations << {:attribute => attribute, :type => :presence}
131
- end
132
-
133
47
  def affirms_absence_of(attribute)
134
48
  affirmations << {:attribute => attribute, :type => :absence}
135
49
  end
136
50
 
137
- def affirms_inclusion_of(attribute, list: [])
138
- affirmations << {
139
- :attribute => attribute, :type => :inclusion, :list => list
140
- }
141
- end
142
-
143
51
  def affirms_acceptance_of(attribute)
144
52
  affirmations << {:attribute => attribute, :type => :acceptance}
145
53
  end
@@ -160,6 +68,12 @@ module DailyAffirmation
160
68
  }
161
69
  end
162
70
 
71
+ def affirms_inclusion_of(attribute, list: [])
72
+ affirmations << {
73
+ :attribute => attribute, :type => :inclusion, :list => list
74
+ }
75
+ end
76
+
163
77
  def affirms_length_of(attribute, range: 0..0)
164
78
  affirmations << {
165
79
  :attribute => attribute, :type => :length, :range => range
@@ -170,6 +84,10 @@ module DailyAffirmation
170
84
  affirmations << {:attribute => attribute, :type => :numericality}
171
85
  end
172
86
 
87
+ def affirms_presence_of(attribute)
88
+ affirmations << {:attribute => attribute, :type => :presence}
89
+ end
90
+
173
91
  def affirmations
174
92
  @affirmations ||= []
175
93
  end
@@ -0,0 +1,26 @@
1
+ module DailyAffirmation
2
+ class Validator
3
+ def initialize(object, attribute, opts = {})
4
+ self.object = object
5
+ self.attribute = attribute
6
+ self.value = object.send(attribute)
7
+ self.opts = opts
8
+ end
9
+
10
+ def affirm
11
+ @affirm ||= [valid?, valid? ? nil : error_message]
12
+ end
13
+
14
+ def valid?
15
+ raise StandardError, "must implement #valid?"
16
+ end
17
+
18
+ def error_message
19
+ raise StandardError, "must implement #error_message"
20
+ end
21
+
22
+ private
23
+
24
+ attr_accessor :object, :attribute, :value, :opts
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ require_relative "../validator"
2
+ require_relative "presence_validator"
3
+
4
+ module DailyAffirmation
5
+ module Validators
6
+ class AbsenceValidator < Validator
7
+ def valid?
8
+ @valid ||= !PresenceValidator.new(object, attribute).valid?
9
+ end
10
+
11
+ def error_message
12
+ @error_message ||= "#{attribute} must be blank"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "../validator"
2
+
3
+ module DailyAffirmation
4
+ module Validators
5
+ class AcceptanceValidator < Validator
6
+ def valid?
7
+ @valid ||= !!value
8
+ end
9
+
10
+ def error_message
11
+ @error_message ||= "#{attribute} must be accepted"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "../validator"
2
+
3
+ module DailyAffirmation
4
+ module Validators
5
+ class ConfirmationValidator < Validator
6
+ def valid?
7
+ @valid ||= value == object.send("#{attribute}_confirmation")
8
+ end
9
+
10
+ def error_message
11
+ @error_message ||= "#{attribute} doesn't match confirmation"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require_relative "../validator"
2
+ require_relative "inclusion_validator"
3
+
4
+ module DailyAffirmation
5
+ module Validators
6
+ class ExclusionValidator < Validator
7
+ def valid?
8
+ @valid ||= !InclusionValidator.new(object, attribute, opts).valid?
9
+ end
10
+
11
+ def error_message
12
+ @error_message ||= "#{attribute} is reserved"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "../validator"
2
+
3
+ module DailyAffirmation
4
+ module Validators
5
+ class FormatValidator < Validator
6
+ def valid?
7
+ @valid ||= !!opts[:regex].match(value)
8
+ end
9
+
10
+ def error_message
11
+ @error_message ||= "#{attribute} is invalid"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "../validator"
2
+
3
+ module DailyAffirmation
4
+ module Validators
5
+ class InclusionValidator < Validator
6
+ def valid?
7
+ @valid ||= opts[:list].include?(value)
8
+ end
9
+
10
+ def error_message
11
+ @error_message ||= "#{attribute} is not included in #{opts[:list]}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "../validator"
2
+
3
+ module DailyAffirmation
4
+ module Validators
5
+ class LengthValidator < Validator
6
+ def valid?
7
+ @valid ||= opts[:range].include?(value.size)
8
+ end
9
+
10
+ def error_message
11
+ @error_message ||= "#{attribute} is the wrong length (allowed #{opts[:range]})"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "../validator"
2
+
3
+ module DailyAffirmation
4
+ module Validators
5
+ class NumericalityValidator < Validator
6
+ def valid?
7
+ @valid ||= value.is_a?(Numeric)
8
+ end
9
+
10
+ def error_message
11
+ @error_message ||= "#{attribute} is not a number"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "../validator"
2
+
3
+ module DailyAffirmation
4
+ module Validators
5
+ class PresenceValidator < Validator
6
+ def valid?
7
+ @valid ||= present?
8
+ end
9
+
10
+ def error_message
11
+ @error_message ||= "#{attribute} can't be blank"
12
+ end
13
+
14
+ private
15
+
16
+ def blank?
17
+ if value.is_a?(String)
18
+ value !~ /[^[:space:]]/
19
+ else
20
+ value.respond_to?(:empty?) ? value.empty? : !value
21
+ end
22
+ end
23
+
24
+ def present?
25
+ !blank?
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module DailyAffirmation
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,4 +1,13 @@
1
1
  require_relative "daily_affirmation/affirmations"
2
+ require_relative "daily_affirmation/validators/absence_validator"
3
+ require_relative "daily_affirmation/validators/acceptance_validator"
4
+ require_relative "daily_affirmation/validators/confirmation_validator"
5
+ require_relative "daily_affirmation/validators/exclusion_validator"
6
+ require_relative "daily_affirmation/validators/format_validator"
7
+ require_relative "daily_affirmation/validators/inclusion_validator"
8
+ require_relative "daily_affirmation/validators/length_validator"
9
+ require_relative "daily_affirmation/validators/numericality_validator"
10
+ require_relative "daily_affirmation/validators/presence_validator"
2
11
  require_relative "daily_affirmation/version"
3
12
 
4
13
  module DailyAffirmation
@@ -2,250 +2,6 @@ require_relative "spec_helper"
2
2
  require_relative "../lib/daily_affirmation"
3
3
 
4
4
  describe DailyAffirmation do
5
- describe ".affirms_presence_of" do
6
- let(:cls) do
7
- Class.new do
8
- include DailyAffirmation.affirmations
9
-
10
- affirms_presence_of :name
11
- end
12
- end
13
-
14
- it "fails validation if attribute is nil" do
15
- obj = double(:name => nil)
16
-
17
- affirmation = cls.new(obj)
18
- expect(affirmation).to_not be_valid
19
- end
20
-
21
- it "fails validation if attribute is empty" do
22
- obj = double(:name => " ")
23
-
24
- affirmation = cls.new(obj)
25
- expect(affirmation).to_not be_valid
26
- end
27
-
28
- it "passes validation if attribute is present" do
29
- obj = double(:name => :foo)
30
-
31
- affirmation = cls.new(obj)
32
- expect(affirmation).to be_valid
33
- end
34
- end
35
-
36
- describe ".affirms_absence_of" do
37
- let(:cls) do
38
- Class.new do
39
- include DailyAffirmation.affirmations
40
-
41
- affirms_absence_of :name
42
- end
43
- end
44
-
45
- it "passes validation if attribute is nil" do
46
- obj = double(:name => nil)
47
-
48
- affirmation = cls.new(obj)
49
- expect(affirmation).to be_valid
50
- end
51
-
52
- it "passes validation if attribute is empty" do
53
- obj = double(:name => " ")
54
-
55
- affirmation = cls.new(obj)
56
- expect(affirmation).to be_valid
57
- end
58
-
59
- it "fails validation if attribute is present" do
60
- obj = double(:name => :foo)
61
-
62
- affirmation = cls.new(obj)
63
- expect(affirmation).to_not be_valid
64
- end
65
- end
66
-
67
- describe ".affirms_inclusion_of" do
68
- let(:cls) do
69
- Class.new do
70
- include DailyAffirmation.affirmations
71
-
72
- affirms_inclusion_of :age, :list => 13..18
73
- end
74
- end
75
-
76
- it "fails validation if attribute is not in the list" do
77
- obj = double(:age => 12)
78
-
79
- affirmation = cls.new(obj)
80
- expect(affirmation).to_not be_valid
81
- end
82
-
83
- it "passes validation if attribute is in the list" do
84
- obj = double(:age => 13)
85
-
86
- affirmation = cls.new(obj)
87
- expect(affirmation).to be_valid
88
- end
89
- end
90
-
91
- describe ".affirms_exclusion_of" do
92
- let(:cls) do
93
- Class.new do
94
- include DailyAffirmation.affirmations
95
-
96
- affirms_exclusion_of :age, :list => 13..18
97
- end
98
- end
99
-
100
- it "passes validation if attribute is not in the list" do
101
- obj = double(:age => 12)
102
-
103
- affirmation = cls.new(obj)
104
- expect(affirmation).to be_valid
105
- end
106
-
107
- it "fails validation if attribute is in the list" do
108
- obj = double(:age => 13)
109
-
110
- affirmation = cls.new(obj)
111
- expect(affirmation).to_not be_valid
112
- end
113
- end
114
-
115
- describe ".affirms_format_of" do
116
- let(:cls) do
117
- Class.new do
118
- include DailyAffirmation.affirmations
119
-
120
- affirms_format_of :name, :regex => /Bobby/
121
- end
122
- end
123
-
124
- it "passes validation if the attribute matches the format" do
125
- obj = double(:name => "Bobby Tabbles")
126
-
127
- affirmation = cls.new(obj)
128
- expect(affirmation).to be_valid
129
- end
130
-
131
- it "fails validation if the attribute doesn't match the format" do
132
- obj = double(:name => "Tommy Tabbles")
133
-
134
- affirmation = cls.new(obj)
135
- expect(affirmation).to_not be_valid
136
- end
137
- end
138
-
139
- describe ".affirms_length_of" do
140
- let(:cls) do
141
- Class.new do
142
- include DailyAffirmation.affirmations
143
-
144
- affirms_length_of :name, :range => 1..10
145
- end
146
- end
147
-
148
- it "passes validation if the attribute's size is within range" do
149
- obj = double(:name => "Bobby")
150
-
151
- affirmation = cls.new(obj)
152
- expect(affirmation).to be_valid
153
- end
154
-
155
- it "fails validation if the attribute's size is lower than range" do
156
- obj = double(:name => "")
157
-
158
- affirmation = cls.new(obj)
159
- expect(affirmation).to_not be_valid
160
- end
161
-
162
- it "fails validation if the attribute's size is higher than range" do
163
- obj = double(:name => "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
164
-
165
- affirmation = cls.new(obj)
166
- expect(affirmation).to_not be_valid
167
- end
168
- end
169
-
170
- describe ".affirms_numericality_of" do
171
- let(:cls) do
172
- Class.new do
173
- include DailyAffirmation.affirmations
174
-
175
- affirms_numericality_of :age
176
- end
177
- end
178
-
179
- it "passes validation if the attribute is a Numeric" do
180
- obj = double(:age => 1.0)
181
-
182
- affirmation = cls.new(obj)
183
- expect(affirmation).to be_valid
184
- end
185
-
186
- it "fails validation if the attribute is not a Numeric" do
187
- obj = double(:age => "Bobby")
188
-
189
- affirmation = cls.new(obj)
190
- expect(affirmation).to_not be_valid
191
- end
192
- end
193
-
194
- describe ".affirms_acceptance_of" do
195
- let(:cls) do
196
- Class.new do
197
- include DailyAffirmation.affirmations
198
-
199
- affirms_acceptance_of :eula
200
- end
201
- end
202
-
203
- it "passes validation if attribute is not nil/false" do
204
- obj = double(:eula => true)
205
-
206
- affirmation = cls.new(obj)
207
- expect(affirmation).to be_valid
208
- end
209
-
210
- it "fails validation if attribute is nil" do
211
- obj = double(:eula => nil)
212
-
213
- affirmation = cls.new(obj)
214
- expect(affirmation).to_not be_valid
215
- end
216
-
217
- it "fails validation if attribute is false" do
218
- obj = double(:eula => false)
219
-
220
- affirmation = cls.new(obj)
221
- expect(affirmation).to_not be_valid
222
- end
223
- end
224
-
225
- describe ".affirms_confirmation_of" do
226
- let(:cls) do
227
- Class.new do
228
- include DailyAffirmation.affirmations
229
-
230
- affirms_confirmation_of :password
231
- end
232
- end
233
-
234
- it "passes validation if the attributes match" do
235
- obj = double(:password => 1, :password_confirmation => 1)
236
-
237
- affirmation = cls.new(obj)
238
- expect(affirmation).to be_valid
239
- end
240
-
241
- it "fails validation if the attributes don't match" do
242
- obj = double(:password => 1, :password_confirmation => 2)
243
-
244
- affirmation = cls.new(obj)
245
- expect(affirmation).to_not be_valid
246
- end
247
- end
248
-
249
5
  describe "#validate" do
250
6
  let(:cls) do
251
7
  Class.new do
@@ -261,7 +17,7 @@ describe DailyAffirmation do
261
17
  affirmation = cls.new(obj)
262
18
 
263
19
  is_valid = affirmation.validate[0]
264
- expect(is_valid).to eq(true)
20
+ expect(is_valid).to be(true)
265
21
  end
266
22
 
267
23
  it "returns validation error messages as the second result" do
@@ -274,7 +30,7 @@ describe DailyAffirmation do
274
30
  end
275
31
  end
276
32
 
277
- describe "#valid" do
33
+ describe "#valid?" do
278
34
  let(:cls) do
279
35
  Class.new do
280
36
  include DailyAffirmation.affirmations
@@ -326,4 +82,36 @@ describe DailyAffirmation do
326
82
  expect(messages).to eq([])
327
83
  end
328
84
  end
85
+
86
+ describe ".affirms_*_of" do
87
+ let(:cls) do
88
+ Class.new do
89
+ include DailyAffirmation.affirmations
90
+
91
+ affirms_absence_of :conflicts
92
+ affirms_acceptance_of :eula
93
+ affirms_confirmation_of :password
94
+ affirms_exclusion_of :age, :list => 13..18
95
+ affirms_format_of :name, :regex => /Bobby/
96
+ affirms_inclusion_of :age, :list => 1..21
97
+ affirms_length_of :password, :range => 8..40
98
+ affirms_numericality_of :age
99
+ affirms_presence_of :name
100
+ end
101
+ end
102
+
103
+ it "correct sets up affirmations" do
104
+ obj = double(
105
+ :conflicts => nil,
106
+ :eula => true,
107
+ :password => "test1234",
108
+ :password_confirmation => "test1234",
109
+ :age => 19,
110
+ :name => "Bobby Tabbles"
111
+ )
112
+
113
+ affirmation = cls.new(obj)
114
+ expect(affirmation).to be_valid
115
+ end
116
+ end
329
117
  end
@@ -0,0 +1,24 @@
1
+ require_relative "spec_helper"
2
+ require_relative "../lib/daily_affirmation/validator"
3
+
4
+ describe "Validator" do
5
+ it "requires #valid? to be implemented" do
6
+ cls = Class.new(DailyAffirmation::Validator)
7
+ obj = double(:age => 13)
8
+
9
+ expect { cls.new(obj, :age).affirm }.to raise_error(
10
+ StandardError, "must implement #valid?"
11
+ )
12
+ end
13
+
14
+ it "requires #error_message to be implemented" do
15
+ cls = Class.new(DailyAffirmation::Validator) do
16
+ def valid?; false; end
17
+ end
18
+ obj = double(:age => 13)
19
+
20
+ expect { cls.new(obj, :age).affirm }.to raise_error(
21
+ StandardError, "must implement #error_message"
22
+ )
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/absence_validator"
3
+
4
+ describe "AbsenceValidator" do
5
+ subject { DailyAffirmation::Validators::AbsenceValidator }
6
+
7
+ it "fails validation if the attribute is present" do
8
+ obj = double(:name => :foo)
9
+
10
+ validator = subject.new(obj, :name)
11
+ expect(validator).to_not be_valid
12
+ end
13
+
14
+ it "passes validation if the attribute is empty" do
15
+ obj = double(:name => " ")
16
+
17
+ validator = subject.new(obj, :name)
18
+ expect(validator).to be_valid
19
+ end
20
+
21
+ it "passes validation if the attribute is nil" do
22
+ obj = double(:name => nil)
23
+
24
+ validator = subject.new(obj, :name)
25
+ expect(validator).to be_valid
26
+ end
27
+
28
+ it "has the correct error message" do
29
+ obj = double(:name => :foo)
30
+
31
+ validator = subject.new(obj, :name)
32
+ expect(validator.error_message).to eq(
33
+ "name must be blank"
34
+ )
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/acceptance_validator"
3
+
4
+ describe "AcceptanceValidator" do
5
+ subject { DailyAffirmation::Validators::AcceptanceValidator }
6
+
7
+ it "passes validation if attribute is not nil/false" do
8
+ obj = double(:eula => true)
9
+
10
+ validator = subject.new(obj, :eula)
11
+ expect(validator).to be_valid
12
+ end
13
+
14
+ it "fails validation if attribute is nil" do
15
+ obj = double(:eula => nil)
16
+
17
+ validator = subject.new(obj, :eula)
18
+ expect(validator).to_not be_valid
19
+ end
20
+
21
+ it "fails validation if attribute is false" do
22
+ obj = double(:eula => false)
23
+
24
+ validator = subject.new(obj, :eula)
25
+ expect(validator).to_not be_valid
26
+ end
27
+
28
+ it "has the correct error message" do
29
+ obj = double(:eula => nil)
30
+
31
+ validator = subject.new(obj, :eula)
32
+ expect(validator.error_message).to eq(
33
+ "eula must be accepted"
34
+ )
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/confirmation_validator"
3
+
4
+ describe "ConfirmationValidator" do
5
+ subject { DailyAffirmation::Validators::ConfirmationValidator }
6
+
7
+ it "passes validation if the attributes match" do
8
+ obj = double(:password => 1, :password_confirmation => 1)
9
+
10
+ validator = subject.new(obj, :password)
11
+ expect(validator).to be_valid
12
+ end
13
+
14
+ it "fails validation if the attributes don't match" do
15
+ obj = double(:password => 1, :password_confirmation => 2)
16
+
17
+ validator = subject.new(obj, :password)
18
+ expect(validator).to_not be_valid
19
+ end
20
+
21
+ it "has the correct error message" do
22
+ obj = double(:password => 1, :password_confirmation => 2)
23
+
24
+ validator = subject.new(obj, :password)
25
+ expect(validator.error_message).to eq(
26
+ "password doesn't match confirmation"
27
+ )
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/exclusion_validator"
3
+
4
+ describe "ExclusionValidator" do
5
+ subject { DailyAffirmation::Validators::ExclusionValidator }
6
+
7
+ it "passes validation if attribute is not in the list" do
8
+ obj = double(:age => 12)
9
+
10
+ validator = subject.new(obj, :age, :list => 13..18)
11
+ expect(validator).to be_valid
12
+ end
13
+
14
+ it "fails validation if attribute is in the list" do
15
+ obj = double(:age => 13)
16
+
17
+ validator = subject.new(obj, :age, :list => 13..18)
18
+ expect(validator).to_not be_valid
19
+ end
20
+
21
+ it "has the correct error message" do
22
+ obj = double(:age => 13)
23
+
24
+ validator = subject.new(obj, :age, :list => 13..18)
25
+ expect(validator.error_message).to eq(
26
+ "age is reserved"
27
+ )
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/format_validator"
3
+
4
+ describe "FormatValidator" do
5
+ subject { DailyAffirmation::Validators::FormatValidator }
6
+
7
+ it "passes validation if the attribute matches the format" do
8
+ obj = double(:name => "Bobby Tabbles")
9
+
10
+ validator = subject.new(obj, :name, :regex => /Bobby/)
11
+ expect(validator).to be_valid
12
+ end
13
+
14
+ it "fails validation if the attribute doesn't match the format" do
15
+ obj = double(:name => "Tommy Tabbles")
16
+
17
+ validator = subject.new(obj, :name, :regex => /Bobby/)
18
+ expect(validator).to_not be_valid
19
+ end
20
+
21
+ it "has the correct error message" do
22
+ obj = double(:name => "Tommy Tabbles")
23
+
24
+ validator = subject.new(obj, :name, :regex => /Bobby/)
25
+ expect(validator.error_message).to eq(
26
+ "name is invalid"
27
+ )
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/inclusion_validator"
3
+
4
+ describe "InclusionValidator" do
5
+ subject { DailyAffirmation::Validators::InclusionValidator }
6
+
7
+ it "fails validation if attribute is not in the list" do
8
+ obj = double(:age => 12)
9
+
10
+ validator = subject.new(obj, :age, :list => 13..18)
11
+ expect(validator).to_not be_valid
12
+ end
13
+
14
+ it "passes validation if attribute is in the list" do
15
+ obj = double(:age => 13)
16
+
17
+ validator = subject.new(obj, :age, :list => 13..18)
18
+ expect(validator).to be_valid
19
+ end
20
+
21
+ it "has the correct error message" do
22
+ obj = double(:age => 13)
23
+
24
+ validator = subject.new(obj, :age, :list => 13..18)
25
+ expect(validator.error_message).to eq(
26
+ "age is not included in 13..18"
27
+ )
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/length_validator"
3
+
4
+ describe "LengthValidator" do
5
+ subject { DailyAffirmation::Validators::LengthValidator }
6
+
7
+ it "passes validation if the attribute's size is within range" do
8
+ obj = double(:name => "Bobby")
9
+
10
+ validator = subject.new(obj, :name, :range => 1..10)
11
+ expect(validator).to be_valid
12
+ end
13
+
14
+ it "fails validation if the attribute's size is lower than range" do
15
+ obj = double(:name => "")
16
+
17
+ validator = subject.new(obj, :name, :range => 1..10)
18
+ expect(validator).to_not be_valid
19
+ end
20
+
21
+ it "fails validation if the attribute's size is higher than range" do
22
+ obj = double(:name => "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
23
+
24
+ validator = subject.new(obj, :name, :range => 1..10)
25
+ expect(validator).to_not be_valid
26
+ end
27
+
28
+ it "has the correct error message" do
29
+ obj = double(:name => "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
30
+
31
+ validator = subject.new(obj, :name, :range => 1..10)
32
+ expect(validator.error_message).to eq(
33
+ "name is the wrong length (allowed 1..10)"
34
+ )
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/numericality_validator"
3
+
4
+ describe "NumericalityValidator" do
5
+ subject { DailyAffirmation::Validators::NumericalityValidator }
6
+
7
+ it "passes validation if the attribute is a Numeric" do
8
+ obj = double(:age => 1.0)
9
+
10
+ validator = subject.new(obj, :age)
11
+ expect(validator).to be_valid
12
+ end
13
+
14
+ it "fails validation if the attribute is not a Numeric" do
15
+ obj = double(:age => "Bobby")
16
+
17
+ validator = subject.new(obj, :age)
18
+ expect(validator).to_not be_valid
19
+ end
20
+
21
+ it "has the correct error message" do
22
+ obj = double(:age => "Bobby")
23
+
24
+ validator = subject.new(obj, :age)
25
+ expect(validator.error_message).to eq(
26
+ "age is not a number"
27
+ )
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "../spec_helper"
2
+ require_relative "../../lib/daily_affirmation/validators/presence_validator"
3
+
4
+ describe "PresenceValidator" do
5
+ subject { DailyAffirmation::Validators::PresenceValidator }
6
+
7
+ it "passes validation if the attribute is present" do
8
+ obj = double(:name => :foo)
9
+
10
+ validator = subject.new(obj, :name)
11
+ expect(validator).to be_valid
12
+ end
13
+
14
+ it "fails validation if the attribute is empty" do
15
+ obj = double(:name => " ")
16
+
17
+ validator = subject.new(obj, :name)
18
+ expect(validator).to_not be_valid
19
+ end
20
+
21
+ it "fails validation if the attribute is nil" do
22
+ obj = double(:name => nil)
23
+
24
+ validator = subject.new(obj, :name)
25
+ expect(validator).to_not be_valid
26
+ end
27
+
28
+ it "has the correct error message" do
29
+ obj = double(:name => nil)
30
+
31
+ validator = subject.new(obj, :name)
32
+ expect(validator.error_message).to eq(
33
+ "name can't be blank"
34
+ )
35
+ end
36
+ end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daily_affirmation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-22 00:00:00.000000000 Z
11
+ date: 2013-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: 3.0.0.beta1
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.0.0.beta1
55
55
  description: A simple library for external validations of POROs
@@ -59,7 +59,7 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
62
+ - ".gitignore"
63
63
  - Gemfile
64
64
  - LICENSE.txt
65
65
  - README.md
@@ -67,9 +67,29 @@ files:
67
67
  - daily_affirmation.gemspec
68
68
  - lib/daily_affirmation.rb
69
69
  - lib/daily_affirmation/affirmations.rb
70
+ - lib/daily_affirmation/validator.rb
71
+ - lib/daily_affirmation/validators/absence_validator.rb
72
+ - lib/daily_affirmation/validators/acceptance_validator.rb
73
+ - lib/daily_affirmation/validators/confirmation_validator.rb
74
+ - lib/daily_affirmation/validators/exclusion_validator.rb
75
+ - lib/daily_affirmation/validators/format_validator.rb
76
+ - lib/daily_affirmation/validators/inclusion_validator.rb
77
+ - lib/daily_affirmation/validators/length_validator.rb
78
+ - lib/daily_affirmation/validators/numericality_validator.rb
79
+ - lib/daily_affirmation/validators/presence_validator.rb
70
80
  - lib/daily_affirmation/version.rb
71
81
  - spec/daily_affirmation_spec.rb
72
82
  - spec/spec_helper.rb
83
+ - spec/validator_spec.rb
84
+ - spec/validators/absence_validator_spec.rb
85
+ - spec/validators/acceptance_validator_spec.rb
86
+ - spec/validators/confirmation_validator_spec.rb
87
+ - spec/validators/exclusion_validator_spec.rb
88
+ - spec/validators/format_validator_spec.rb
89
+ - spec/validators/inclusion_validator_spec.rb
90
+ - spec/validators/length_validator_spec.rb
91
+ - spec/validators/numericality_validator_spec.rb
92
+ - spec/validators/presence_validator_spec.rb
73
93
  homepage: https://github.com/teamsnap/daily_affirmation
74
94
  licenses:
75
95
  - MIT
@@ -80,20 +100,30 @@ require_paths:
80
100
  - lib
81
101
  required_ruby_version: !ruby/object:Gem::Requirement
82
102
  requirements:
83
- - - '>='
103
+ - - ">="
84
104
  - !ruby/object:Gem::Version
85
105
  version: '0'
86
106
  required_rubygems_version: !ruby/object:Gem::Requirement
87
107
  requirements:
88
- - - '>='
108
+ - - ">="
89
109
  - !ruby/object:Gem::Version
90
110
  version: '0'
91
111
  requirements: []
92
112
  rubyforge_project:
93
- rubygems_version: 2.0.14
113
+ rubygems_version: 2.2.0
94
114
  signing_key:
95
115
  specification_version: 4
96
116
  summary: A simple library for external validations of POROs
97
117
  test_files:
98
118
  - spec/daily_affirmation_spec.rb
99
119
  - spec/spec_helper.rb
120
+ - spec/validator_spec.rb
121
+ - spec/validators/absence_validator_spec.rb
122
+ - spec/validators/acceptance_validator_spec.rb
123
+ - spec/validators/confirmation_validator_spec.rb
124
+ - spec/validators/exclusion_validator_spec.rb
125
+ - spec/validators/format_validator_spec.rb
126
+ - spec/validators/inclusion_validator_spec.rb
127
+ - spec/validators/length_validator_spec.rb
128
+ - spec/validators/numericality_validator_spec.rb
129
+ - spec/validators/presence_validator_spec.rb