couchpillow 0.4.8 → 0.4.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/couchpillow/document.rb +29 -9
- data/lib/couchpillow/helper.rb +20 -1
- data/lib/couchpillow/version.rb +1 -1
- data/test/helper.rb +22 -3
- data/test/test_document.rb +16 -0
- 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: 2d7345235a3c65ff275b24df3e7c3a2633445460
|
4
|
+
data.tar.gz: 0fe1d51ac229b905db59d7c4b6099c813a4412c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af5e607cf055c06484a7ee18f24e91a28de8afcf9d9b5fed6ef44cf210283487153d86b118ce72af27073116f4016e17ffd528a19d09afe0f3ac53ec135f5ba2
|
7
|
+
data.tar.gz: 5c3633aba46a77245abaaade884b9d83dc9ade01e836cdd8cd5c6c505091ff1a23e101e3777706ec2284d80346fffa3532c145646aed41b7a588231d325745da
|
data/lib/couchpillow/document.rb
CHANGED
@@ -52,7 +52,7 @@ module CouchPillow
|
|
52
52
|
|
53
53
|
time = Time.now.utc
|
54
54
|
@data[:_created_at] ||= time
|
55
|
-
@data[:_updated_at]
|
55
|
+
@data[:_updated_at] ||= time
|
56
56
|
|
57
57
|
@cas = cas
|
58
58
|
|
@@ -259,19 +259,30 @@ module CouchPillow
|
|
259
259
|
# If multiple ids are passed, returns an array.
|
260
260
|
#
|
261
261
|
def self.get *ids
|
262
|
-
results =
|
263
|
-
|
262
|
+
results = []
|
263
|
+
if ids.length == 1
|
264
|
+
id = _sanitize_id(ids.first)
|
264
265
|
tid = _is_type_prefixed? ? "#{_doc_type}::#{id}" : id
|
265
266
|
result, _, cas = _default_db.get(tid, extended: true)
|
266
267
|
|
267
|
-
result
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
268
|
+
results << create(result, id, cas)
|
269
|
+
|
270
|
+
elsif ids.length > 1
|
271
|
+
# Sanitize ids first
|
272
|
+
sanitized_ids = ids.map do |id|
|
273
|
+
id = _sanitize_id(id)
|
274
|
+
_is_type_prefixed? ? "#{_doc_type}::#{id}" : id
|
275
|
+
end
|
276
|
+
|
277
|
+
# Query the db
|
278
|
+
db_result = _default_db.get(*sanitized_ids, extended: true)
|
279
|
+
results = sanitized_ids.map do |k|
|
280
|
+
result, _, cas = db_result[k]
|
281
|
+
create(result, k, cas)
|
282
|
+
end
|
272
283
|
end
|
273
284
|
|
274
|
-
results
|
285
|
+
results.size == 1 ? results.first : results
|
275
286
|
end
|
276
287
|
|
277
288
|
|
@@ -353,6 +364,15 @@ module CouchPillow
|
|
353
364
|
end
|
354
365
|
|
355
366
|
|
367
|
+
def self.create result, id, cas
|
368
|
+
result and
|
369
|
+
type = result[:_type] || result["_type"] and
|
370
|
+
type == _doc_type and
|
371
|
+
new(result, id, cas) or
|
372
|
+
nil
|
373
|
+
end
|
374
|
+
|
375
|
+
|
356
376
|
def self.event_listeners
|
357
377
|
@event_listeners ||= {}
|
358
378
|
end
|
data/lib/couchpillow/helper.rb
CHANGED
@@ -2,9 +2,28 @@ module CouchPillow
|
|
2
2
|
|
3
3
|
def self.symbolize hash
|
4
4
|
hash.inject({}) do |memo,(k,v)|
|
5
|
-
memo[k.to_sym] = v
|
5
|
+
memo[k.to_sym] = sanitize(v)
|
6
6
|
memo
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
|
11
|
+
def self.sanitize value
|
12
|
+
sanitized = nil
|
13
|
+
if value.class == Hash
|
14
|
+
sanitized = {}
|
15
|
+
value.each do |k, v|
|
16
|
+
sanitized[k] = sanitize(v)
|
17
|
+
end
|
18
|
+
elsif value.class == Array
|
19
|
+
sanitized = []
|
20
|
+
value.each do |e|
|
21
|
+
sanitized << sanitize(e)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
sanitized = value
|
25
|
+
end
|
26
|
+
sanitized
|
27
|
+
end
|
28
|
+
|
10
29
|
end
|
data/lib/couchpillow/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -35,8 +35,27 @@ class FakeCouchbaseServer
|
|
35
35
|
end
|
36
36
|
|
37
37
|
|
38
|
-
def get
|
39
|
-
|
40
|
-
|
38
|
+
def get *args
|
39
|
+
opts = has_opts?(args) ? args.pop : {}
|
40
|
+
ids = args
|
41
|
+
if ids.length == 1
|
42
|
+
id = ids.first
|
43
|
+
opts[:extended] == true and
|
44
|
+
retval = [@storage[id], nil, @cas[id]] or
|
45
|
+
retval = @storage[id]
|
46
|
+
else
|
47
|
+
retval = ids.inject({}) do |memo, id|
|
48
|
+
opts[:extended] == true and
|
49
|
+
memo[id] = [@storage[id], nil, @cas[id]] or
|
50
|
+
memo[id] = @storage[id]
|
51
|
+
memo
|
52
|
+
end
|
53
|
+
end
|
54
|
+
retval
|
41
55
|
end
|
56
|
+
|
57
|
+
def has_opts? args
|
58
|
+
args.any? { |v| v.is_a?(Hash) }
|
59
|
+
end
|
60
|
+
|
42
61
|
end
|
data/test/test_document.rb
CHANGED
@@ -31,12 +31,28 @@ class TestDocument < Minitest::Test
|
|
31
31
|
end
|
32
32
|
|
33
33
|
|
34
|
+
def test_updated_at_doesnt_get_updated_at_initialize
|
35
|
+
t = Time.parse("2008-06-21 13:30:00 UTC")
|
36
|
+
d = Document.new({"_updated_at" => t}, "1")
|
37
|
+
assert_equal t, d._updated_at
|
38
|
+
end
|
39
|
+
|
40
|
+
|
34
41
|
def test_default_type
|
35
42
|
d = Document.new({}, "1")
|
36
43
|
assert_equal "couchpillow", d.doc_type
|
37
44
|
end
|
38
45
|
|
39
46
|
|
47
|
+
def test_hash_with_proc_should_not_throw_error
|
48
|
+
hsh = Hash.new { |hash,key| hash[key.to_s] if Symbol === key }
|
49
|
+
hsh['foo'] = Hash.new { |hash,key| hash[key.to_s] if Symbol === key }
|
50
|
+
hsh['bar'] = 200
|
51
|
+
d = Document.new(hsh, "1")
|
52
|
+
d.save!
|
53
|
+
end
|
54
|
+
|
55
|
+
|
40
56
|
def test_type_subclasses
|
41
57
|
d = Class.new(Document) do
|
42
58
|
type 'test'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couchpillow
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Albert Tedja
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: couchbase
|