scrivener 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 +4 -4
- data/README.md +8 -0
- data/lib/scrivener.rb +1 -1
- data/lib/scrivener/validations.rb +22 -0
- data/test/scrivener_test.rb +130 -69
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a106b8c9aa9ba83fc66fec4afc4c030da9f25291
|
4
|
+
data.tar.gz: d5918f87defb857d0870932c1914db935497aca8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5afc2e5969db3a93670a20ede2992d3a1e095369d4407292bda46956f3d50fa8bc857d1dc9233adaf45fb044f99bdc672414fc0dc58655cc3ee1dc6d02295cd6
|
7
|
+
data.tar.gz: cab3e3c0e586c192768ce7788359cfcb0fd36a8b8681f7097a5cd4deeeddf544fc0482fffba70e09cf3708dbee3b752c5726649c1c3e89ccd56a8e6eb6e2413e
|
data/README.md
CHANGED
@@ -184,6 +184,14 @@ end
|
|
184
184
|
Checks that the given field is not nil or empty. The error code for this
|
185
185
|
assertion is `:not_present`.
|
186
186
|
|
187
|
+
### assert_equal
|
188
|
+
|
189
|
+
Check that the attribute has the expected value. It uses === for
|
190
|
+
comparison, so type checks are possible too. Note that in order to
|
191
|
+
make the case equality work, the check inverts the order of the
|
192
|
+
arguments: `assert_equal :foo, Bar` is translated to the expression
|
193
|
+
`Bar === send(:foo)`.
|
194
|
+
|
187
195
|
### assert_format
|
188
196
|
|
189
197
|
Checks that the given field matches the provided regular expression.
|
data/lib/scrivener.rb
CHANGED
@@ -12,6 +12,7 @@ class Scrivener
|
|
12
12
|
# * assert_member
|
13
13
|
# * assert_length
|
14
14
|
# * assert_decimal
|
15
|
+
# * assert_equal
|
15
16
|
#
|
16
17
|
# The core tenets that Scrivener::Validations advocates can be summed up in a
|
17
18
|
# few bullet points:
|
@@ -168,6 +169,27 @@ class Scrivener
|
|
168
169
|
assert_format att, DECIMAL, error
|
169
170
|
end
|
170
171
|
|
172
|
+
# Check that the attribute has the expected value. It uses === for
|
173
|
+
# comparison, so type checks are possible too. Note that in order
|
174
|
+
# to make the case equality work, the check inverts the order of
|
175
|
+
# the arguments: `assert_equal :foo, Bar` is translated to the
|
176
|
+
# expression `Bar === send(:foo)`.
|
177
|
+
#
|
178
|
+
# @example
|
179
|
+
#
|
180
|
+
# def validate
|
181
|
+
# assert_equal :status, "pending"
|
182
|
+
# assert_equal :quantity, Fixnum
|
183
|
+
# end
|
184
|
+
#
|
185
|
+
# @param [Symbol] att The attribute you wish to verify for equality.
|
186
|
+
# @param [Object] value The value you want to test against.
|
187
|
+
# @param [Array<Symbol, Symbol>] error The error that should be returned
|
188
|
+
# when the validation fails.
|
189
|
+
def assert_equal(att, value, error = [att, :not_equal])
|
190
|
+
assert value === send(att), error
|
191
|
+
end
|
192
|
+
|
171
193
|
# The grand daddy of all assertions. If you want to build custom
|
172
194
|
# assertions, or even quick and dirty ones, you can simply use this method.
|
173
195
|
#
|
data/test/scrivener_test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.expand_path("../lib/scrivener", File.dirname(__FILE__))
|
2
2
|
|
3
|
-
class
|
3
|
+
class A < Scrivener
|
4
4
|
attr_accessor :a
|
5
5
|
attr_accessor :b
|
6
6
|
end
|
@@ -10,34 +10,34 @@ scope do
|
|
10
10
|
atts = { :a => 1, :b => 2, :c => 3 }
|
11
11
|
|
12
12
|
assert_raise NoMethodError do
|
13
|
-
|
13
|
+
filter = A.new(atts)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
test "not raise when there are less fields" do
|
18
18
|
atts = { :a => 1 }
|
19
19
|
|
20
|
-
assert
|
20
|
+
assert filter = A.new(atts)
|
21
21
|
end
|
22
22
|
|
23
23
|
test "return attributes" do
|
24
24
|
atts = { :a => 1, :b => 2 }
|
25
25
|
|
26
|
-
|
26
|
+
filter = A.new(atts)
|
27
27
|
|
28
|
-
assert_equal atts,
|
28
|
+
assert_equal atts, filter.attributes
|
29
29
|
end
|
30
30
|
|
31
31
|
test "return only the required attributes" do
|
32
32
|
atts = { :a => 1, :b => 2 }
|
33
33
|
|
34
|
-
|
34
|
+
filter = A.new(atts)
|
35
35
|
|
36
|
-
assert_equal
|
36
|
+
assert_equal filter.slice(:a), { :a => 1 }
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
class
|
40
|
+
class B < Scrivener
|
41
41
|
attr_accessor :a
|
42
42
|
attr_accessor :b
|
43
43
|
|
@@ -51,54 +51,54 @@ scope do
|
|
51
51
|
test "validations" do
|
52
52
|
atts = { :a => 1, :b => 2 }
|
53
53
|
|
54
|
-
|
54
|
+
filter = B.new(atts)
|
55
55
|
|
56
|
-
assert
|
56
|
+
assert filter.valid?
|
57
57
|
end
|
58
58
|
|
59
59
|
test "validation errors" do
|
60
60
|
atts = { :a => 1 }
|
61
61
|
|
62
|
-
|
62
|
+
filter = B.new(atts)
|
63
63
|
|
64
|
-
assert_equal false,
|
65
|
-
assert_equal [],
|
66
|
-
assert_equal [:not_present],
|
64
|
+
assert_equal false, filter.valid?
|
65
|
+
assert_equal [], filter.errors[:a]
|
66
|
+
assert_equal [:not_present], filter.errors[:b]
|
67
67
|
end
|
68
68
|
|
69
69
|
test "attributes without @errors" do
|
70
70
|
atts = { :a => 1, :b => 2 }
|
71
71
|
|
72
|
-
|
72
|
+
filter = B.new(atts)
|
73
73
|
|
74
|
-
|
75
|
-
assert_equal atts,
|
74
|
+
filter.valid?
|
75
|
+
assert_equal atts, filter.attributes
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
class
|
79
|
+
class C
|
80
80
|
include Scrivener::Validations
|
81
81
|
|
82
|
-
attr_accessor :
|
82
|
+
attr_accessor :a
|
83
83
|
|
84
84
|
def validate
|
85
|
-
assert_present :
|
85
|
+
assert_present :a
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
89
|
scope do
|
90
90
|
test "validations without Scrivener" do
|
91
|
-
|
92
|
-
|
93
|
-
assert
|
91
|
+
filter = C.new
|
92
|
+
filter.a = 1
|
93
|
+
assert filter.valid?
|
94
94
|
|
95
|
-
|
96
|
-
assert_equal false,
|
97
|
-
assert_equal [:not_present],
|
95
|
+
filter = C.new
|
96
|
+
assert_equal false, filter.valid?
|
97
|
+
assert_equal [:not_present], filter.errors[:a]
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
class
|
101
|
+
class D < Scrivener
|
102
102
|
attr_accessor :url, :email
|
103
103
|
|
104
104
|
def validate
|
@@ -109,52 +109,52 @@ end
|
|
109
109
|
|
110
110
|
scope do
|
111
111
|
test "email & url" do
|
112
|
-
|
112
|
+
filter = D.new({})
|
113
113
|
|
114
|
-
assert !
|
115
|
-
assert_equal [:not_url],
|
116
|
-
assert_equal [:not_email],
|
114
|
+
assert ! filter.valid?
|
115
|
+
assert_equal [:not_url], filter.errors[:url]
|
116
|
+
assert_equal [:not_email], filter.errors[:email]
|
117
117
|
|
118
|
-
|
118
|
+
filter = D.new(url: "google.com", email: "egoogle.com")
|
119
119
|
|
120
|
-
assert !
|
121
|
-
assert_equal [:not_url],
|
122
|
-
assert_equal [:not_email],
|
120
|
+
assert ! filter.valid?
|
121
|
+
assert_equal [:not_url], filter.errors[:url]
|
122
|
+
assert_equal [:not_email], filter.errors[:email]
|
123
123
|
|
124
|
-
|
125
|
-
assert
|
124
|
+
filter = D.new(url: "http://google.com", email: "me@google.com")
|
125
|
+
assert filter.valid?
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
129
|
-
class
|
130
|
-
attr_accessor :
|
129
|
+
class E < Scrivener
|
130
|
+
attr_accessor :a
|
131
131
|
|
132
132
|
def validate
|
133
|
-
assert_length :
|
133
|
+
assert_length :a, 3..10
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
137
|
scope do
|
138
138
|
test "length validation" do
|
139
|
-
|
139
|
+
filter = E.new({})
|
140
140
|
|
141
|
-
assert !
|
142
|
-
assert
|
141
|
+
assert ! filter.valid?
|
142
|
+
assert filter.errors[:a].include?(:not_in_range)
|
143
143
|
|
144
|
-
|
145
|
-
assert !
|
146
|
-
assert
|
144
|
+
filter = E.new(a: "fo")
|
145
|
+
assert ! filter.valid?
|
146
|
+
assert filter.errors[:a].include?(:not_in_range)
|
147
147
|
|
148
|
-
|
149
|
-
assert !
|
150
|
-
assert
|
148
|
+
filter = E.new(a: "foofoofoofo")
|
149
|
+
assert ! filter.valid?
|
150
|
+
assert filter.errors[:a].include?(:not_in_range)
|
151
151
|
|
152
|
-
|
153
|
-
assert
|
152
|
+
filter = E.new(a: "foo")
|
153
|
+
assert filter.valid?
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
157
|
-
class
|
157
|
+
class F < Scrivener
|
158
158
|
attr_accessor :status
|
159
159
|
|
160
160
|
def validate
|
@@ -164,38 +164,99 @@ end
|
|
164
164
|
|
165
165
|
scope do
|
166
166
|
test "member validation" do
|
167
|
-
|
168
|
-
assert !
|
169
|
-
assert_equal [:not_valid],
|
167
|
+
filter = F.new({})
|
168
|
+
assert ! filter.valid?
|
169
|
+
assert_equal [:not_valid], filter.errors[:status]
|
170
170
|
|
171
|
-
|
172
|
-
assert !
|
173
|
-
assert_equal [:not_valid],
|
171
|
+
filter = F.new(status: "foo")
|
172
|
+
assert ! filter.valid?
|
173
|
+
assert_equal [:not_valid], filter.errors[:status]
|
174
174
|
|
175
175
|
%w{pending paid delivered}.each do |status|
|
176
|
-
|
177
|
-
assert
|
176
|
+
filter = F.new(status: status)
|
177
|
+
assert filter.valid?
|
178
178
|
end
|
179
179
|
end
|
180
180
|
end
|
181
181
|
|
182
|
-
class
|
183
|
-
attr_accessor :
|
182
|
+
class G < Scrivener
|
183
|
+
attr_accessor :a
|
184
184
|
|
185
185
|
def validate
|
186
|
-
assert_decimal :
|
186
|
+
assert_decimal :a
|
187
187
|
end
|
188
188
|
end
|
189
189
|
|
190
190
|
scope do
|
191
191
|
test "decimal validation" do
|
192
|
-
|
193
|
-
assert !
|
194
|
-
assert_equal [:not_decimal],
|
192
|
+
filter = G.new({})
|
193
|
+
assert ! filter.valid?
|
194
|
+
assert_equal [:not_decimal], filter.errors[:a]
|
195
195
|
|
196
|
-
%w{10 10.1 10.100000 0.100000 .1000}.each do |
|
197
|
-
|
198
|
-
assert
|
196
|
+
%w{10 10.1 10.100000 0.100000 .1000}.each do |a|
|
197
|
+
filter = G.new(a: a)
|
198
|
+
assert filter.valid?
|
199
199
|
end
|
200
200
|
end
|
201
201
|
end
|
202
|
+
|
203
|
+
class H < Scrivener
|
204
|
+
attr_accessor :a
|
205
|
+
|
206
|
+
def validate
|
207
|
+
assert_length :a, 3..10
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
scope do
|
212
|
+
test "length validation" do
|
213
|
+
filter = H.new({})
|
214
|
+
|
215
|
+
assert ! filter.valid?
|
216
|
+
assert filter.errors[:a].include?(:not_in_range)
|
217
|
+
|
218
|
+
filter = H.new(a: "fo")
|
219
|
+
assert ! filter.valid?
|
220
|
+
assert filter.errors[:a].include?(:not_in_range)
|
221
|
+
|
222
|
+
filter = H.new(a: "foofoofoofo")
|
223
|
+
assert ! filter.valid?
|
224
|
+
assert filter.errors[:a].include?(:not_in_range)
|
225
|
+
|
226
|
+
filter = H.new(a: "foo")
|
227
|
+
assert filter.valid?
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
class I < Scrivener
|
232
|
+
attr_accessor :a
|
233
|
+
attr_accessor :b
|
234
|
+
|
235
|
+
def validate
|
236
|
+
assert_equal :a, "foo"
|
237
|
+
assert_equal :b, Fixnum
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
|
242
|
+
scope do
|
243
|
+
test "equality validation" do
|
244
|
+
filter = I.new({})
|
245
|
+
|
246
|
+
assert ! filter.valid?
|
247
|
+
assert filter.errors[:a].include?(:not_equal)
|
248
|
+
assert filter.errors[:b].include?(:not_equal)
|
249
|
+
|
250
|
+
filter = I.new(a: "foo", b: "bar")
|
251
|
+
assert ! filter.valid?
|
252
|
+
|
253
|
+
filter = I.new(a: "foo")
|
254
|
+
assert ! filter.valid?
|
255
|
+
assert filter.errors[:a].empty?
|
256
|
+
assert filter.errors[:b].include?(:not_equal)
|
257
|
+
|
258
|
+
filter = I.new(a: "foo", b: 42)
|
259
|
+
filter.valid?
|
260
|
+
assert filter.valid?
|
261
|
+
end
|
262
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scrivener
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cutest
|