couchpillow 0.4.8 → 0.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7cd317f3b612baf2716008f1bddcc02381a9eaad
4
- data.tar.gz: f773b200f9f8f75f5620184d3d28fafef7b09036
3
+ metadata.gz: 2d7345235a3c65ff275b24df3e7c3a2633445460
4
+ data.tar.gz: 0fe1d51ac229b905db59d7c4b6099c813a4412c3
5
5
  SHA512:
6
- metadata.gz: 66d608fe08195ca9e61b6005f31f5792f7126c259785a48f9db2a57f6714a43fd86a572d80a586ecdeef232efcb710828ede3735b99929e8009ae6be0e073310
7
- data.tar.gz: 0ad341af2a487c15c0737eee339404ab0b268d93e60898a3558678a2a0cd3f369df36ec8595f47b93ad0b175c45fd8865bca1e80ea2bf6ef14c77e315673bd2c
6
+ metadata.gz: af5e607cf055c06484a7ee18f24e91a28de8afcf9d9b5fed6ef44cf210283487153d86b118ce72af27073116f4016e17ffd528a19d09afe0f3ac53ec135f5ba2
7
+ data.tar.gz: 5c3633aba46a77245abaaade884b9d83dc9ade01e836cdd8cd5c6c505091ff1a23e101e3777706ec2284d80346fffa3532c145646aed41b7a588231d325745da
@@ -52,7 +52,7 @@ module CouchPillow
52
52
 
53
53
  time = Time.now.utc
54
54
  @data[:_created_at] ||= time
55
- @data[:_updated_at] = time
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 = ids.map do |id|
263
- id = _sanitize_id(id)
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 and
268
- type = result[:_type] || result["_type"] and
269
- type == _doc_type and
270
- new(result, id, cas) or
271
- nil
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 && results.size == 1 ? results.first : 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
@@ -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
@@ -1,5 +1,5 @@
1
1
  module CouchPillow
2
2
  GEM_NAME = "couchpillow"
3
3
  NAME = "CouchPillow"
4
- VERSION = "0.4.8"
4
+ VERSION = "0.4.9"
5
5
  end
data/test/helper.rb CHANGED
@@ -35,8 +35,27 @@ class FakeCouchbaseServer
35
35
  end
36
36
 
37
37
 
38
- def get id, opts = {}
39
- return [@storage[id], nil, @cas[id]] if opts[:extended] == true
40
- @storage[id]
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
@@ -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.8
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-05-18 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: couchbase