daily_affirmation 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -11
- data/lib/daily_affirmation/affirmations.rb +91 -1
- data/lib/daily_affirmation/version.rb +1 -1
- data/spec/daily_affirmation_spec.rb +191 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44d20be607b16937aac801de19c1973221a42cf4
|
4
|
+
data.tar.gz: 941f6d4296dde5aaa5fb754dcc418df3786dea44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae1c976af688895ad58f82c7aa718a1fa4a7f719d43d0cca1fc1036049940d2a8efd515c4016bc73104e9142e019ca529d4e2fc7735e5b9b49dae31b0b5e9418
|
7
|
+
data.tar.gz: 6531765cd121565299d0ad104584d60ed84a65c160e6be6f527a89b9efc59ce30283c77585e7cec23f7cf39a80415b602a03663102089dd5815046ab32fd94dc
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# DailyAffirmation
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/daily_affirmation.png)](http://badge.fury.io/rb/daily_affirmation)
|
3
4
|
[![Semaphore](https://semaphoreapp.com/api/v1/projects/7268def52c792e50bfc60e1b1a6e905ed4e2a80d/118940/shields_badge.png)](https://semaphoreapp.com/minter/daily_affirmation)
|
4
5
|
[![Code Climate](https://codeclimate.com/github/teamsnap/daily_affirmation.png)](https://codeclimate.com/github/teamsnap/daily_affirmation)
|
5
6
|
[![Coverage Status](https://coveralls.io/repos/teamsnap/daily_affirmation/badge.png?branch=master)](https://coveralls.io/r/teamsnap/daily_affirmation?branch=master)
|
@@ -30,21 +31,23 @@ Or install it yourself as:
|
|
30
31
|
|
31
32
|
## Usage
|
32
33
|
|
33
|
-
|
34
|
+
```ruby
|
35
|
+
require "daily_affirmations"
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
37
|
+
class Team
|
38
|
+
attr_accessor :name, :status_cd
|
39
|
+
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
+
class TeamAffirmation
|
42
|
+
include DailyAffirmation.affirmations
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
44
|
+
affirms_presence_of :name
|
45
|
+
affirms_inclusion_of :status_cd, :list => 0..2
|
46
|
+
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
+
team1 = OpenStruct.new(:name => "", :status_cd => 3)
|
49
|
+
TeamAffirmation.new(team1).valid? #=> false
|
50
|
+
```
|
48
51
|
|
49
52
|
## Roadmap
|
50
53
|
|
@@ -43,7 +43,15 @@ module DailyAffirmation
|
|
43
43
|
if present?(attribute)
|
44
44
|
[true, nil]
|
45
45
|
else
|
46
|
-
[false, "#{attribute}
|
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]
|
47
55
|
end
|
48
56
|
end
|
49
57
|
|
@@ -55,6 +63,54 @@ module DailyAffirmation
|
|
55
63
|
end
|
56
64
|
end
|
57
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
|
+
|
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
|
+
|
58
114
|
def blank?(attribute)
|
59
115
|
value = object.send(attribute)
|
60
116
|
case value
|
@@ -74,12 +130,46 @@ module DailyAffirmation
|
|
74
130
|
affirmations << {:attribute => attribute, :type => :presence}
|
75
131
|
end
|
76
132
|
|
133
|
+
def affirms_absence_of(attribute)
|
134
|
+
affirmations << {:attribute => attribute, :type => :absence}
|
135
|
+
end
|
136
|
+
|
77
137
|
def affirms_inclusion_of(attribute, list: [])
|
78
138
|
affirmations << {
|
79
139
|
:attribute => attribute, :type => :inclusion, :list => list
|
80
140
|
}
|
81
141
|
end
|
82
142
|
|
143
|
+
def affirms_acceptance_of(attribute)
|
144
|
+
affirmations << {:attribute => attribute, :type => :acceptance}
|
145
|
+
end
|
146
|
+
|
147
|
+
def affirms_confirmation_of(attribute)
|
148
|
+
affirmations << {:attribute => attribute, :type => :confirmation}
|
149
|
+
end
|
150
|
+
|
151
|
+
def affirms_exclusion_of(attribute, list: [])
|
152
|
+
affirmations << {
|
153
|
+
:attribute => attribute, :type => :exclusion, :list => list
|
154
|
+
}
|
155
|
+
end
|
156
|
+
|
157
|
+
def affirms_format_of(attribute, regex: //)
|
158
|
+
affirmations << {
|
159
|
+
:attribute => attribute, :type => :format, :regex => regex
|
160
|
+
}
|
161
|
+
end
|
162
|
+
|
163
|
+
def affirms_length_of(attribute, range: 0..0)
|
164
|
+
affirmations << {
|
165
|
+
:attribute => attribute, :type => :length, :range => range
|
166
|
+
}
|
167
|
+
end
|
168
|
+
|
169
|
+
def affirms_numericality_of(attribute)
|
170
|
+
affirmations << {:attribute => attribute, :type => :numericality}
|
171
|
+
end
|
172
|
+
|
83
173
|
def affirmations
|
84
174
|
@affirmations ||= []
|
85
175
|
end
|
@@ -33,6 +33,37 @@ describe DailyAffirmation do
|
|
33
33
|
end
|
34
34
|
end
|
35
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
|
+
|
36
67
|
describe ".affirms_inclusion_of" do
|
37
68
|
let(:cls) do
|
38
69
|
Class.new do
|
@@ -57,6 +88,164 @@ describe DailyAffirmation do
|
|
57
88
|
end
|
58
89
|
end
|
59
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
|
+
|
60
249
|
describe "#validate" do
|
61
250
|
let(:cls) do
|
62
251
|
Class.new do
|
@@ -80,7 +269,7 @@ describe DailyAffirmation do
|
|
80
269
|
affirmation = cls.new(obj)
|
81
270
|
|
82
271
|
messages = affirmation.validate[1]
|
83
|
-
expect(messages).to include("name
|
272
|
+
expect(messages).to include("name can't be blank")
|
84
273
|
expect(messages).to include("age is not included in 13..18")
|
85
274
|
end
|
86
275
|
end
|
@@ -125,7 +314,7 @@ describe DailyAffirmation do
|
|
125
314
|
affirmation = cls.new(obj)
|
126
315
|
messages = affirmation.error_messages
|
127
316
|
|
128
|
-
expect(messages).to include("name
|
317
|
+
expect(messages).to include("name can't be blank")
|
129
318
|
expect(messages).to include("age is not included in 13..18")
|
130
319
|
end
|
131
320
|
|