couchrest 0.12.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.
Files changed (47) hide show
  1. data/LICENSE +176 -0
  2. data/README.md +69 -0
  3. data/Rakefile +103 -0
  4. data/THANKS.md +18 -0
  5. data/bin/couchdir +20 -0
  6. data/examples/model/example.rb +138 -0
  7. data/examples/word_count/markov +38 -0
  8. data/examples/word_count/views/books/chunked-map.js +3 -0
  9. data/examples/word_count/views/books/united-map.js +1 -0
  10. data/examples/word_count/views/markov/chain-map.js +6 -0
  11. data/examples/word_count/views/markov/chain-reduce.js +7 -0
  12. data/examples/word_count/views/word_count/count-map.js +6 -0
  13. data/examples/word_count/views/word_count/count-reduce.js +3 -0
  14. data/examples/word_count/word_count.rb +67 -0
  15. data/examples/word_count/word_count_query.rb +39 -0
  16. data/lib/couchrest/commands/generate.rb +71 -0
  17. data/lib/couchrest/commands/push.rb +103 -0
  18. data/lib/couchrest/core/database.rb +239 -0
  19. data/lib/couchrest/core/design.rb +89 -0
  20. data/lib/couchrest/core/document.rb +63 -0
  21. data/lib/couchrest/core/model.rb +607 -0
  22. data/lib/couchrest/core/server.rb +51 -0
  23. data/lib/couchrest/core/view.rb +4 -0
  24. data/lib/couchrest/helper/pager.rb +103 -0
  25. data/lib/couchrest/helper/streamer.rb +44 -0
  26. data/lib/couchrest/monkeypatches.rb +24 -0
  27. data/lib/couchrest.rb +140 -0
  28. data/spec/couchrest/core/couchrest_spec.rb +201 -0
  29. data/spec/couchrest/core/database_spec.rb +630 -0
  30. data/spec/couchrest/core/design_spec.rb +131 -0
  31. data/spec/couchrest/core/document_spec.rb +130 -0
  32. data/spec/couchrest/core/model_spec.rb +855 -0
  33. data/spec/couchrest/helpers/pager_spec.rb +122 -0
  34. data/spec/couchrest/helpers/streamer_spec.rb +23 -0
  35. data/spec/fixtures/attachments/README +3 -0
  36. data/spec/fixtures/attachments/couchdb.png +0 -0
  37. data/spec/fixtures/attachments/test.html +11 -0
  38. data/spec/fixtures/views/lib.js +3 -0
  39. data/spec/fixtures/views/test_view/lib.js +3 -0
  40. data/spec/fixtures/views/test_view/only-map.js +4 -0
  41. data/spec/fixtures/views/test_view/test-map.js +3 -0
  42. data/spec/fixtures/views/test_view/test-reduce.js +3 -0
  43. data/spec/spec.opts +6 -0
  44. data/spec/spec_helper.rb +20 -0
  45. data/utils/remap.rb +27 -0
  46. data/utils/subset.rb +30 -0
  47. metadata +156 -0
@@ -0,0 +1,122 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe CouchRest::Pager do
4
+ before(:all) do
5
+ @cr = CouchRest.new(COUCHHOST)
6
+ @db = @cr.database(TESTDB)
7
+ @db.delete! rescue nil
8
+ @db = @cr.create_db(TESTDB) rescue nil
9
+ @pager = CouchRest::Pager.new(@db)
10
+ end
11
+
12
+ after(:all) do
13
+ begin
14
+ @db.delete!
15
+ rescue RestClient::Request::RequestFailed
16
+ end
17
+ end
18
+
19
+ it "should store the db" do
20
+ @pager.db.should == @db
21
+ end
22
+
23
+ describe "paging all docs" do
24
+ before(:all) do
25
+ @docs = []
26
+ 100.times do |i|
27
+ @docs << ({:number => (i % 10)})
28
+ end
29
+ @db.bulk_save(@docs)
30
+ end
31
+ it "should yield total_docs / limit times" do
32
+ n = 0
33
+ @pager.all_docs(10) do |doc|
34
+ n += 1
35
+ end
36
+ n.should == 10
37
+ end
38
+ it "should yield each docrow group without duplicate docs" do
39
+ docids = {}
40
+ @pager.all_docs(10) do |docrows|
41
+ docrows.each do |row|
42
+ docids[row['id']].should be_nil
43
+ docids[row['id']] = true
44
+ end
45
+ end
46
+ docids.keys.length.should == 100
47
+ end
48
+ it "should yield each docrow group" do
49
+ @pager.all_docs(10) do |docrows|
50
+ doc = @db.get(docrows[0]['id'])
51
+ doc['number'].class.should == Fixnum
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "Pager with a view and docs" do
57
+ before(:all) do
58
+ @docs = []
59
+ 100.times do |i|
60
+ @docs << ({:number => (i % 10)})
61
+ end
62
+ @db.bulk_save(@docs)
63
+ @db.save({
64
+ '_id' => '_design/magic',
65
+ 'views' => {
66
+ 'number' => {
67
+ 'map' => 'function(doc){emit(doc.number,null)}'
68
+ }
69
+ }
70
+ })
71
+ end
72
+
73
+ it "should have docs" do
74
+ @docs.length.should == 100
75
+ @db.documents['rows'].length.should == 101
76
+ end
77
+
78
+ it "should have a view" do
79
+ @db.view('magic/number', :limit => 10)['rows'][0]['key'].should == 0
80
+ end
81
+
82
+ it "should yield once per key" do
83
+ results = {}
84
+ @pager.key_reduce('magic/number', 20) do |k,vs|
85
+ results[k] = vs.length
86
+ end
87
+ results[0].should == 10
88
+ results[3].should == 10
89
+ end
90
+
91
+ it "with a small step size should yield once per key" do
92
+ results = {}
93
+ @pager.key_reduce('magic/number', 7) do |k,vs|
94
+ results[k] = vs.length
95
+ end
96
+ results[0].should == 10
97
+ results[3].should == 10
98
+ results[9].should == 10
99
+ end
100
+ it "with a large step size should yield once per key" do
101
+ results = {}
102
+ @pager.key_reduce('magic/number', 1000) do |k,vs|
103
+ results[k] = vs.length
104
+ end
105
+ results[0].should == 10
106
+ results[3].should == 10
107
+ results[9].should == 10
108
+ end
109
+ it "with a begin and end should only yield in the range (and leave out the lastkey)" do
110
+ results = {}
111
+ @pager.key_reduce('magic/number', 1000, 4, 7) do |k,vs|
112
+ results[k] = vs.length
113
+ end
114
+ results[0].should be_nil
115
+ results[4].should == 10
116
+ results[6].should == 10
117
+ results[7].should be_nil
118
+ results[8].should be_nil
119
+ results[9].should be_nil
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe CouchRest::Streamer do
4
+ before(:all) do
5
+ @cr = CouchRest.new(COUCHHOST)
6
+ @db = @cr.database(TESTDB)
7
+ @db.delete! rescue nil
8
+ @db = @cr.create_db(TESTDB) rescue nil
9
+ @streamer = CouchRest::Streamer.new(@db)
10
+ @docs = (1..1000).collect{|i| {:integer => i, :string => i.to_s}}
11
+ @db.bulk_save(@docs)
12
+ end
13
+
14
+ it "should yield each row in a view" do
15
+ count = 0
16
+ sum = 0
17
+ @streamer.view("_all_docs") do |row|
18
+ count += 1
19
+ end
20
+ count.should == 1001
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ This is an example README file.
2
+
3
+ More of the README, whee.
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Test</title>
5
+ </head>
6
+ <body>
7
+ <p>
8
+ Test
9
+ </p>
10
+ </body>
11
+ </html>
@@ -0,0 +1,3 @@
1
+ function globalLib() {
2
+ return "fixture";
3
+ };
@@ -0,0 +1,3 @@
1
+ function justThisView() {
2
+ return "fixture";
3
+ };
@@ -0,0 +1,4 @@
1
+ function(doc) {
2
+ //include-lib
3
+ emit(null, null);
4
+ };
@@ -0,0 +1,3 @@
1
+ function(doc) {
2
+ emit(null, null);
3
+ };
@@ -0,0 +1,3 @@
1
+ function(ks,vs,co) {
2
+ return vs.length;
3
+ };
data/spec/spec.opts ADDED
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,20 @@
1
+ require "rubygems"
2
+ require "spec" # Satisfies Autotest and anyone else not using the Rake tasks
3
+
4
+ require File.dirname(__FILE__) + '/../lib/couchrest'
5
+
6
+ unless defined?(FIXTURE_PATH)
7
+ FIXTURE_PATH = File.dirname(__FILE__) + '/fixtures'
8
+ SCRATCH_PATH = File.dirname(__FILE__) + '/tmp'
9
+
10
+ COUCHHOST = "http://127.0.0.1:5984"
11
+ TESTDB = 'couchrest-test'
12
+ end
13
+
14
+ def reset_test_db!
15
+ cr = CouchRest.new(COUCHHOST)
16
+ db = cr.database(TESTDB)
17
+ db.delete! rescue nil
18
+ db = cr.create_db(TESTDB) rescue nin
19
+ db
20
+ end
data/utils/remap.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'couchrest'
3
+
4
+ # set the source db and map view
5
+ source = CouchRest.new("http://127.0.0.1:5984").database('source-db')
6
+ source_view = 'mydesign/view-map'
7
+
8
+ # set the target db
9
+ target = CouchRest.new("http://127.0.0.1:5984").database('target-db')
10
+
11
+
12
+ pager = CouchRest::Pager.new(source)
13
+
14
+ # pager will yield once per uniq key in the source view
15
+
16
+ pager.key_reduce(source_view, 10000) do |key, values|
17
+ # create a doc from the key and the values
18
+ example_doc = {
19
+ :key => key,
20
+ :values => values.uniq
21
+ }
22
+
23
+ target.save(example_doc)
24
+
25
+ # keep us up to date with progress
26
+ puts k if (rand > 0.9)
27
+ end
data/utils/subset.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'couchrest'
3
+
4
+ # subset.rb replicates a percentage of a database to a fresh database.
5
+ # use it to create a smaller dataset on which to prototype views.
6
+
7
+ # specify the source database
8
+ source = CouchRest.new("http://127.0.0.1:5984").database('source-db')
9
+
10
+ # specify the target database
11
+ target = CouchRest.new("http://127.0.0.1:5984").database('target-db')
12
+
13
+ # pager efficiently yields all view rows
14
+ pager = CouchRest::Pager.new(source)
15
+
16
+ pager.all_docs(1000) do |rows|
17
+ docs = rows.collect do |r|
18
+ # the percentage of docs to clone
19
+ next if rand > 0.1
20
+ doc = source.get(r['id'])
21
+ doc.delete('_rev')
22
+ doc
23
+ end.compact
24
+ puts docs.length
25
+ next if docs.empty?
26
+
27
+ puts docs.first['_id']
28
+ target.bulk_save(docs)
29
+ end
30
+
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: couchrest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.12.2
5
+ platform: ruby
6
+ authors:
7
+ - J. Chris Anderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-22 00:00:00 -08:00
13
+ default_executable: couchdir
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rest-client
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0.5"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mime-types
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "1.15"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: extlib
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.6
54
+ version:
55
+ description: CouchRest provides a simple interface on top of CouchDB's RESTful HTTP API, as well as including some utility scripts for managing views and attachments.
56
+ email: jchris@apache.org
57
+ executables:
58
+ - couchdir
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - README.md
63
+ - LICENSE
64
+ - THANKS.md
65
+ files:
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - THANKS.md
70
+ - bin/couchdir
71
+ - examples/model
72
+ - examples/model/example.rb
73
+ - examples/word_count
74
+ - examples/word_count/markov
75
+ - examples/word_count/views
76
+ - examples/word_count/views/books
77
+ - examples/word_count/views/books/chunked-map.js
78
+ - examples/word_count/views/books/united-map.js
79
+ - examples/word_count/views/markov
80
+ - examples/word_count/views/markov/chain-map.js
81
+ - examples/word_count/views/markov/chain-reduce.js
82
+ - examples/word_count/views/word_count
83
+ - examples/word_count/views/word_count/count-map.js
84
+ - examples/word_count/views/word_count/count-reduce.js
85
+ - examples/word_count/word_count.rb
86
+ - examples/word_count/word_count_query.rb
87
+ - lib/couchrest
88
+ - lib/couchrest/commands
89
+ - lib/couchrest/commands/generate.rb
90
+ - lib/couchrest/commands/push.rb
91
+ - lib/couchrest/core
92
+ - lib/couchrest/core/database.rb
93
+ - lib/couchrest/core/design.rb
94
+ - lib/couchrest/core/document.rb
95
+ - lib/couchrest/core/model.rb
96
+ - lib/couchrest/core/server.rb
97
+ - lib/couchrest/core/view.rb
98
+ - lib/couchrest/helper
99
+ - lib/couchrest/helper/pager.rb
100
+ - lib/couchrest/helper/streamer.rb
101
+ - lib/couchrest/monkeypatches.rb
102
+ - lib/couchrest.rb
103
+ - spec/couchrest
104
+ - spec/couchrest/core
105
+ - spec/couchrest/core/couchrest_spec.rb
106
+ - spec/couchrest/core/database_spec.rb
107
+ - spec/couchrest/core/design_spec.rb
108
+ - spec/couchrest/core/document_spec.rb
109
+ - spec/couchrest/core/model_spec.rb
110
+ - spec/couchrest/helpers
111
+ - spec/couchrest/helpers/pager_spec.rb
112
+ - spec/couchrest/helpers/streamer_spec.rb
113
+ - spec/fixtures
114
+ - spec/fixtures/attachments
115
+ - spec/fixtures/attachments/couchdb.png
116
+ - spec/fixtures/attachments/README
117
+ - spec/fixtures/attachments/test.html
118
+ - spec/fixtures/views
119
+ - spec/fixtures/views/lib.js
120
+ - spec/fixtures/views/test_view
121
+ - spec/fixtures/views/test_view/lib.js
122
+ - spec/fixtures/views/test_view/only-map.js
123
+ - spec/fixtures/views/test_view/test-map.js
124
+ - spec/fixtures/views/test_view/test-reduce.js
125
+ - spec/spec.opts
126
+ - spec/spec_helper.rb
127
+ - utils/remap.rb
128
+ - utils/subset.rb
129
+ has_rdoc: true
130
+ homepage: http://github.com/jchris/couchrest
131
+ post_install_message:
132
+ rdoc_options: []
133
+
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: "0"
141
+ version:
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: "0"
147
+ version:
148
+ requirements: []
149
+
150
+ rubyforge_project:
151
+ rubygems_version: 1.2.0
152
+ signing_key:
153
+ specification_version: 2
154
+ summary: Lean and RESTful interface to CouchDB.
155
+ test_files: []
156
+