jchris-couchrest 0.8.1 → 0.8.2

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.
data/lib/pager.rb CHANGED
@@ -16,6 +16,13 @@ module Enumerable
16
16
  grouped
17
17
  end
18
18
  end unless [].respond_to?(:group_by)
19
+
20
+ def group_by_fast
21
+ inject({}) do |grouped, element|
22
+ (grouped[yield(element)] ||= []) << element
23
+ grouped
24
+ end
25
+ end
19
26
  end
20
27
 
21
28
  class CouchRest
@@ -25,6 +32,21 @@ class CouchRest
25
32
  @db = db
26
33
  end
27
34
 
35
+ def all_docs(count=100, &block)
36
+ startkey = nil
37
+ oldend = nil
38
+
39
+ while docrows = request_all_docs(count+1, startkey)
40
+ startkey = docrows.last['key']
41
+ docrows.pop if docrows.length > count
42
+ if oldend == startkey
43
+ break
44
+ end
45
+ yield(docrows)
46
+ oldend = startkey
47
+ end
48
+ end
49
+
28
50
  def key_reduce(view, count, firstkey = nil, lastkey = nil, &block)
29
51
  # start with no keys
30
52
  startkey = firstkey
@@ -74,6 +96,17 @@ class CouchRest
74
96
  end
75
97
  end
76
98
 
99
+ private
100
+
101
+ def request_all_docs count, startkey = nil
102
+ opts = {}
103
+ opts[:count] = count if count
104
+ opts[:startkey] = startkey if startkey
105
+ results = @db.documents(opts)
106
+ rows = results['rows']
107
+ rows unless rows.length == 0
108
+ end
109
+
77
110
  def request_view view, count = nil, startkey = nil, endkey = nil
78
111
  opts = {}
79
112
  opts[:count] = count if count
@@ -4,14 +4,12 @@ describe CouchRest::Database do
4
4
  before(:each) do
5
5
  @cr = CouchRest.new(COUCHHOST)
6
6
  begin
7
- @db = @cr.create_db(TESTDB)
7
+ @db = @cr.database(TESTDB)
8
+ @db.delete!
8
9
  rescue RestClient::Request::RequestFailed
9
10
  end
10
- end
11
-
12
- after(:each) do
13
11
  begin
14
- @db.delete!
12
+ @db = @cr.create_db(TESTDB)
15
13
  rescue RestClient::Request::RequestFailed
16
14
  end
17
15
  end
data/spec/pager_spec.rb CHANGED
@@ -25,6 +25,39 @@ describe CouchRest::Pager do
25
25
  @pager.db.should == @db
26
26
  end
27
27
 
28
+ describe "paging all docs" do
29
+ before(:all) do
30
+ @docs = []
31
+ 100.times do |i|
32
+ @docs << ({:number => (i % 10)})
33
+ end
34
+ @db.bulk_save(@docs)
35
+ end
36
+ it "should yield total_docs / count times" do
37
+ n = 0
38
+ @pager.all_docs(10) do |doc|
39
+ n += 1
40
+ end
41
+ n.should == 10
42
+ end
43
+ it "should yield each docrow group without duplicate docs" do
44
+ docids = {}
45
+ @pager.all_docs(10) do |docrows|
46
+ docrows.each do |row|
47
+ docids[row['id']].should be_nil
48
+ docids[row['id']] = true
49
+ end
50
+ end
51
+ docids.keys.length.should == 100
52
+ end
53
+ it "should yield each docrow group" do
54
+ @pager.all_docs(10) do |docrows|
55
+ doc = @db.get(docrows[0]['id'])
56
+ doc['number'].class.should == Fixnum
57
+ end
58
+ end
59
+ end
60
+
28
61
  describe "Pager with a view and docs" do
29
62
  before(:all) do
30
63
  @docs = []
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require File.dirname(__FILE__) + '/../lib/couchrest'
2
2
 
3
- COUCHHOST = "http://localhost:5985"
3
+ COUCHHOST = "http://localhost:5984"
4
4
  TESTDB = 'couchrest-test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jchris-couchrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Chris Anderson