mattly-couchrest 0.12.1

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 +105 -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 +606 -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 +175 -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,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mattly-couchrest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.12.1
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
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.2
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: rest-client
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0.5"
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: mime-types
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "1.15"
41
+ version:
42
+ - !ruby/object:Gem::Dependency
43
+ name: extlib
44
+ version_requirement:
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.9.6
50
+ version:
51
+ 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.
52
+ email: jchris@apache.org
53
+ executables:
54
+ - couchdir
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - README.md
59
+ - LICENSE
60
+ - THANKS.md
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - THANKS.md
66
+ - bin/couchdir
67
+ - examples/model
68
+ - examples/model/example.rb
69
+ - examples/word_count
70
+ - examples/word_count/markov
71
+ - examples/word_count/views
72
+ - examples/word_count/views/books
73
+ - examples/word_count/views/books/chunked-map.js
74
+ - examples/word_count/views/books/united-map.js
75
+ - examples/word_count/views/markov
76
+ - examples/word_count/views/markov/chain-map.js
77
+ - examples/word_count/views/markov/chain-reduce.js
78
+ - examples/word_count/views/word_count
79
+ - examples/word_count/views/word_count/count-map.js
80
+ - examples/word_count/views/word_count/count-reduce.js
81
+ - examples/word_count/word_count.rb
82
+ - examples/word_count/word_count_query.rb
83
+ - lib/couchrest
84
+ - lib/couchrest/commands
85
+ - lib/couchrest/commands/generate.rb
86
+ - lib/couchrest/commands/push.rb
87
+ - lib/couchrest/core
88
+ - lib/couchrest/core/database.rb
89
+ - lib/couchrest/core/design.rb
90
+ - lib/couchrest/core/document.rb
91
+ - lib/couchrest/core/model.rb
92
+ - lib/couchrest/core/server.rb
93
+ - lib/couchrest/core/view.rb
94
+ - lib/couchrest/helper
95
+ - lib/couchrest/helper/pager.rb
96
+ - lib/couchrest/helper/streamer.rb
97
+ - lib/couchrest/helper/template-app
98
+ - lib/couchrest/helper/template-app/_attachments
99
+ - lib/couchrest/helper/template-app/foo
100
+ - lib/couchrest/helper/template-app/forms
101
+ - lib/couchrest/helper/template-app/lib
102
+ - lib/couchrest/helper/template-app/lib/helpers
103
+ - lib/couchrest/helper/template-app/lib/templates
104
+ - lib/couchrest/helper/template-app/views
105
+ - lib/couchrest/helper/template-app/views/example
106
+ - lib/couchrest/helper/templates
107
+ - lib/couchrest/monkeypatches.rb
108
+ - lib/couchrest.rb
109
+ - spec/couchrest
110
+ - spec/couchrest/core
111
+ - spec/couchrest/core/couchrest_spec.rb
112
+ - spec/couchrest/core/database_spec.rb
113
+ - spec/couchrest/core/design_spec.rb
114
+ - spec/couchrest/core/document_spec.rb
115
+ - spec/couchrest/core/model_spec.rb
116
+ - spec/couchrest/helpers
117
+ - spec/couchrest/helpers/pager_spec.rb
118
+ - spec/couchrest/helpers/streamer_spec.rb
119
+ - spec/fixtures
120
+ - spec/fixtures/attachments
121
+ - spec/fixtures/attachments/couchdb.png
122
+ - spec/fixtures/attachments/README
123
+ - spec/fixtures/attachments/test.html
124
+ - spec/fixtures/couchapp
125
+ - spec/fixtures/couchapp/attachments
126
+ - spec/fixtures/couchapp/attachments/index.html
127
+ - spec/fixtures/couchapp/views
128
+ - spec/fixtures/couchapp/views/example-map.js
129
+ - spec/fixtures/couchapp/views/example-reduce.js
130
+ - spec/fixtures/couchapp-test
131
+ - spec/fixtures/couchapp-test/my-app
132
+ - spec/fixtures/couchapp-test/my-app/attachments
133
+ - spec/fixtures/couchapp-test/my-app/attachments/index.html
134
+ - spec/fixtures/couchapp-test/my-app/views
135
+ - spec/fixtures/couchapp-test/my-app/views/example-map.js
136
+ - spec/fixtures/couchapp-test/my-app/views/example-reduce.js
137
+ - spec/fixtures/views
138
+ - spec/fixtures/views/lib.js
139
+ - spec/fixtures/views/test_view
140
+ - spec/fixtures/views/test_view/lib.js
141
+ - spec/fixtures/views/test_view/only-map.js
142
+ - spec/fixtures/views/test_view/test-map.js
143
+ - spec/fixtures/views/test_view/test-reduce.js
144
+ - spec/spec.opts
145
+ - spec/spec_helper.rb
146
+ - utils/remap.rb
147
+ - utils/subset.rb
148
+ has_rdoc: true
149
+ homepage: http://github.com/jchris/couchrest
150
+ post_install_message:
151
+ rdoc_options: []
152
+
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: "0"
160
+ version:
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: "0"
166
+ version:
167
+ requirements: []
168
+
169
+ rubyforge_project:
170
+ rubygems_version: 1.2.0
171
+ signing_key:
172
+ specification_version: 2
173
+ summary: Lean and RESTful interface to CouchDB.
174
+ test_files: []
175
+