couchpillow 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +11 -0
- data/lib/couchpillow/document.rb +60 -3
- data/lib/couchpillow/version.rb +1 -1
- data/test/test_document.rb +13 -0
- 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: 34fd1e52575f6546ba76d939d3a14144fed532d4
|
4
|
+
data.tar.gz: 33b2e8b67740bda40d43d152347466a18aa31ec9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c081d50a39666f337b3a487ed08d892df31724705919ff26fe52ac33ca4a53ea68610b7f3abaf9684e979fb30ba95b890efd264cab04b3301a5e27c71e98fd0
|
7
|
+
data.tar.gz: a634742fd125c01fe9b84e67c1cdfba916667a7a701d4725b0ca1b7b451b0e47db06657db11c8f504fbd1547b5d1bb8e95bc253ac2cb1b91a9ac97b34e39399c
|
data/README.markdown
CHANGED
@@ -103,6 +103,17 @@ Using `rename` to rename keys. Useful to maintain document integrity.
|
|
103
103
|
u = User.new( { :username => 'jdoe' } )
|
104
104
|
u.nickname # 'jdoe'
|
105
105
|
|
106
|
+
Using `whitelist` to whitelist keys. Also useful to maintain document integrity
|
107
|
+
and keeping other fields from being saved. Keep in mind that when loading
|
108
|
+
existing documents and if they have keys that are not listed on the whitelist,
|
109
|
+
those keys will be dropped upon `save!`.
|
110
|
+
|
111
|
+
class User < CouchPillow::Document
|
112
|
+
whitelist :name
|
113
|
+
end
|
114
|
+
u = User.new( { :age => 10, :name => 'John' } )
|
115
|
+
u.has?(:age) # false
|
116
|
+
|
106
117
|
|
107
118
|
## Design Docs and Views
|
108
119
|
|
data/lib/couchpillow/document.rb
CHANGED
@@ -32,6 +32,7 @@ module CouchPillow
|
|
32
32
|
raise TypeError if @data[:_type] && @data[:_type] != self.class._type
|
33
33
|
@data[:_type] = self.class._type
|
34
34
|
rename!
|
35
|
+
whitelist!
|
35
36
|
ensure_types!
|
36
37
|
end
|
37
38
|
|
@@ -54,7 +55,11 @@ module CouchPillow
|
|
54
55
|
ms.gsub!('=', '')
|
55
56
|
@data[ms.to_sym] = args[0]
|
56
57
|
else
|
57
|
-
|
58
|
+
if @data.has_key?(m)
|
59
|
+
@data[m]
|
60
|
+
else
|
61
|
+
super
|
62
|
+
end
|
58
63
|
end
|
59
64
|
end
|
60
65
|
|
@@ -75,6 +80,7 @@ module CouchPillow
|
|
75
80
|
# Save this document to the server
|
76
81
|
#
|
77
82
|
def save!
|
83
|
+
whitelist!
|
78
84
|
validate
|
79
85
|
sort!
|
80
86
|
timestamp!
|
@@ -100,6 +106,7 @@ module CouchPillow
|
|
100
106
|
# exist in the database.
|
101
107
|
#
|
102
108
|
def update!
|
109
|
+
whitelist!
|
103
110
|
validate
|
104
111
|
sort!
|
105
112
|
timestamp!
|
@@ -115,6 +122,12 @@ module CouchPillow
|
|
115
122
|
hash.each do |k,v|
|
116
123
|
@data[k.to_sym] = v
|
117
124
|
end
|
125
|
+
whitelist!
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
def has? key
|
130
|
+
@data.has_key?(key)
|
118
131
|
end
|
119
132
|
|
120
133
|
|
@@ -164,6 +177,16 @@ module CouchPillow
|
|
164
177
|
end
|
165
178
|
|
166
179
|
|
180
|
+
def whitelist!
|
181
|
+
unless self.class.whitelist_keys.empty?
|
182
|
+
@data.select! do |k, v|
|
183
|
+
RESERVED_KEYS.include?(k) ||
|
184
|
+
self.class.whitelist_keys.include?(k)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
|
167
190
|
# Get a Document given an id.
|
168
191
|
#
|
169
192
|
# @returns nil if Document is of a different type.
|
@@ -187,7 +210,8 @@ module CouchPillow
|
|
187
210
|
end
|
188
211
|
|
189
212
|
|
190
|
-
# Rename an existing key to a new key. This is invoked right after
|
213
|
+
# Rename an existing key to a new key. This is invoked right after
|
214
|
+
# initialize.
|
191
215
|
#
|
192
216
|
def self.rename from, to
|
193
217
|
raise ArgumentError, "Cannot rename reserved keys" if
|
@@ -214,7 +238,16 @@ module CouchPillow
|
|
214
238
|
|
215
239
|
|
216
240
|
# Validate the presence of a particular key using a custom validation method.
|
217
|
-
#
|
241
|
+
# If block is omitted, it only validates the existence of the key.
|
242
|
+
#
|
243
|
+
# @param key Key to be validated.
|
244
|
+
# @param message Message that will be displayed when validation fails.
|
245
|
+
# Will be appended after the key.
|
246
|
+
# @yield [v] Value of the key.
|
247
|
+
# The block must return truthy for it to pass the validation.
|
248
|
+
#
|
249
|
+
# @example
|
250
|
+
# validate :first_name, 'does not exist', lambda { |v| !v.nil? }
|
218
251
|
#
|
219
252
|
def self.validate key, message, block
|
220
253
|
raise ValidationError, "Provide validation method for key #{key}" unless block
|
@@ -222,6 +255,25 @@ module CouchPillow
|
|
222
255
|
end
|
223
256
|
|
224
257
|
|
258
|
+
# This Document should only accept keys that are specified here.
|
259
|
+
# The existence of these keys are optional, and won't trigger any validation
|
260
|
+
# unless specified by the validate methods. Hashes passed to update and
|
261
|
+
# initialize will be filtered through this list.
|
262
|
+
#
|
263
|
+
# If you don't specify a whitelist, Document will accept any keys, but
|
264
|
+
# once you specify it, only those keys will be accepted and the rest will
|
265
|
+
# be dropped.
|
266
|
+
#
|
267
|
+
# @example
|
268
|
+
# whitelist :first_name, :last_name, :address
|
269
|
+
#
|
270
|
+
def self.whitelist *list
|
271
|
+
list.each do |k|
|
272
|
+
whitelist_keys << k.to_s.to_sym
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
|
225
277
|
private
|
226
278
|
|
227
279
|
|
@@ -240,6 +292,11 @@ module CouchPillow
|
|
240
292
|
end
|
241
293
|
|
242
294
|
|
295
|
+
def self.whitelist_keys
|
296
|
+
@whitelist_keys ||= []
|
297
|
+
end
|
298
|
+
|
299
|
+
|
243
300
|
def self.symbolize hash
|
244
301
|
hash.inject({}) do |memo,(k,v)|
|
245
302
|
memo[k.to_sym] = v
|
data/lib/couchpillow/version.rb
CHANGED
data/test/test_document.rb
CHANGED
@@ -215,6 +215,19 @@ class TestDocument < Minitest::Test
|
|
215
215
|
end
|
216
216
|
|
217
217
|
|
218
|
+
def test_whitelist
|
219
|
+
d = Class.new(Document) do
|
220
|
+
whitelist :a, :b, :c
|
221
|
+
end.new
|
222
|
+
d.update({a: 1, b: 2, c: 3, d: 4})
|
223
|
+
assert d.save!
|
224
|
+
assert_equal 1, d.a
|
225
|
+
assert_equal 2, d.b
|
226
|
+
assert_equal 3, d.c
|
227
|
+
assert_equal false, d.has?(:d)
|
228
|
+
end
|
229
|
+
|
230
|
+
|
218
231
|
def test_to_json
|
219
232
|
mock_time
|
220
233
|
d = Document.new({}, "1")
|