ShyCouch 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -0
- data/ShyCouch.gemspec +6 -2
- data/VERSION +1 -1
- data/design_notes.rb +39 -0
- data/lib/ShyCouch.rb +80 -98
- data/lib/ShyCouch/data.rb +24 -8
- data/test/test_ShyCouch.rb +5 -5
- data/test/test_couch_document.rb +30 -30
- data/test/test_couchdb_api.rb +1 -1
- data/test/test_couchdb_factory.rb +1 -1
- data/test/test_design_documents.rb +1 -3
- data/test/test_views.rb +26 -26
- metadata +31 -19
data/Rakefile
CHANGED
@@ -22,6 +22,7 @@ Jeweler::Tasks.new do |gem|
|
|
22
22
|
gem.email = "danbryan@gmail.com"
|
23
23
|
gem.authors = ["Shy Inc.", "Daniel Bryan", "Cerales"]
|
24
24
|
gem.add_dependency "ShyRubyJS"
|
25
|
+
gem.add_dependency "rest-client", ">=1.6.7"
|
25
26
|
gem.add_dependency "sourcify", ">=0"
|
26
27
|
# dependencies defined in Gemfile
|
27
28
|
end
|
data/ShyCouch.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ShyCouch}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{Shy Inc.}, %q{Daniel Bryan}, %q{Cerales}]
|
12
|
-
s.date = %q{2011-09-
|
12
|
+
s.date = %q{2011-09-18}
|
13
13
|
s.description = %q{Ruby API for CouchDB, designed to work with the Camping micro-framework.}
|
14
14
|
s.email = %q{danbryan@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"Rakefile",
|
26
26
|
"ShyCouch.gemspec",
|
27
27
|
"VERSION",
|
28
|
+
"design_notes.rb",
|
28
29
|
"lib/ShyCouch.rb",
|
29
30
|
"lib/ShyCouch/data.rb",
|
30
31
|
"lib/ShyCouch/fields.rb",
|
@@ -56,6 +57,7 @@ Gem::Specification.new do |s|
|
|
56
57
|
s.add_development_dependency(%q<sourcify>, ["~> 0.5.0"])
|
57
58
|
s.add_development_dependency(%q<ShyRubyJS>, [">= 0"])
|
58
59
|
s.add_runtime_dependency(%q<ShyRubyJS>, [">= 0"])
|
60
|
+
s.add_runtime_dependency(%q<rest-client>, [">= 1.6.7"])
|
59
61
|
s.add_runtime_dependency(%q<sourcify>, [">= 0"])
|
60
62
|
else
|
61
63
|
s.add_dependency(%q<ShyRubyJS>, [">= 0"])
|
@@ -65,6 +67,7 @@ Gem::Specification.new do |s|
|
|
65
67
|
s.add_dependency(%q<sourcify>, ["~> 0.5.0"])
|
66
68
|
s.add_dependency(%q<ShyRubyJS>, [">= 0"])
|
67
69
|
s.add_dependency(%q<ShyRubyJS>, [">= 0"])
|
70
|
+
s.add_dependency(%q<rest-client>, [">= 1.6.7"])
|
68
71
|
s.add_dependency(%q<sourcify>, [">= 0"])
|
69
72
|
end
|
70
73
|
else
|
@@ -75,6 +78,7 @@ Gem::Specification.new do |s|
|
|
75
78
|
s.add_dependency(%q<sourcify>, ["~> 0.5.0"])
|
76
79
|
s.add_dependency(%q<ShyRubyJS>, [">= 0"])
|
77
80
|
s.add_dependency(%q<ShyRubyJS>, [">= 0"])
|
81
|
+
s.add_dependency(%q<rest-client>, [">= 1.6.7"])
|
78
82
|
s.add_dependency(%q<sourcify>, [">= 0"])
|
79
83
|
end
|
80
84
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/design_notes.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# just playing with possible syntax inspired by bluebie's implementation
|
2
|
+
settings = {blah blah blah}
|
3
|
+
db = ShyCouch.getDB(settings)
|
4
|
+
|
5
|
+
designs = db.getDesigns #should get a DocumentCollection thing
|
6
|
+
|
7
|
+
# long method of making a design
|
8
|
+
|
9
|
+
#defining a view w/ map
|
10
|
+
view = ShyCouch::Data::View.new :rocks do
|
11
|
+
map do
|
12
|
+
emit(doc.id, null) if doc.kind == "rock"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
#defining a view w/ reduce
|
17
|
+
view2 = ShyCouch::Data::View.new :rock_weight_average
|
18
|
+
map do
|
19
|
+
emit(doc.id, doc.weight) if doc.kind == "rock"
|
20
|
+
end
|
21
|
+
reduce do
|
22
|
+
# return the average
|
23
|
+
return sum(values) / values.length
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
#defining a view w/ literal JS instead of parsing
|
28
|
+
|
29
|
+
view3 = ShyCouch::Data::View.new :complicated
|
30
|
+
view3.map = "function(doc) { emit(doc.id, doc.weirdThing); }"
|
31
|
+
view3.reduce = "function(key, values, rereduce) { functiondfhs lambda calculs blahalahal; }"
|
32
|
+
|
33
|
+
newDesign = ShyCouch::Data::Design.new
|
34
|
+
# either:
|
35
|
+
newDesign << view
|
36
|
+
newDesign << view2
|
37
|
+
# or:
|
38
|
+
newDesign << view,view2
|
39
|
+
# or both!
|
data/lib/ShyCouch.rb
CHANGED
@@ -5,33 +5,29 @@
|
|
5
5
|
|
6
6
|
# Add the directory containing this file to the start of the load path if it
|
7
7
|
# isn't there already.
|
8
|
+
|
8
9
|
$:.unshift(File.dirname(__FILE__)) unless
|
9
10
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
10
11
|
|
11
|
-
require 'net/http'
|
12
12
|
require 'json'
|
13
13
|
require 'resolv'
|
14
|
-
|
15
|
-
require '
|
14
|
+
require 'rest-client'
|
15
|
+
require 'ShyRubyJS'
|
16
16
|
# require everything from the 'ShyCouch' subdirectory
|
17
|
-
Dir.new(File.dirname(__FILE__)+'/ShyCouch').each { |f| require 'shycouch/' + f.split('.')[0] unless f == '.' or f == '..' }
|
17
|
+
# Dir.new(File.dirname(__FILE__)+'/ShyCouch').each { |f| require 'shycouch/' + f.split('.')[0] unless f == '.' or f == '..' }
|
18
|
+
require 'ShyCouch/data'
|
19
|
+
require 'ShyCouch/fields'
|
18
20
|
|
19
21
|
|
20
22
|
module ShyCouch
|
21
23
|
class << self
|
22
|
-
|
23
|
-
|
24
|
-
$couchdb = ShyCouch.getDB(settings)
|
25
|
-
end
|
26
|
-
|
27
|
-
def getDB(settings=nil)
|
28
|
-
settings = $couch_settings unless settings
|
24
|
+
def getDB(settings)
|
25
|
+
# this is a wrapper for create a CouchDatabase object and testing that it can connect
|
29
26
|
database = CouchDatabase.new(settings)
|
30
27
|
puts database.connect unless database.connect["ok"] #TODO - hm
|
31
28
|
database.create unless database.on_server?
|
32
29
|
return database
|
33
30
|
end
|
34
|
-
|
35
31
|
end
|
36
32
|
attr_accessor :database
|
37
33
|
|
@@ -39,16 +35,16 @@ module ShyCouch
|
|
39
35
|
|
40
36
|
class CouchDatabase
|
41
37
|
def initialize(settings)
|
42
|
-
|
38
|
+
raise StandardError, "invalid settings" if settings == nil
|
43
39
|
init(settings)
|
44
40
|
end
|
45
41
|
|
46
42
|
attr_accessor :server, :name, :host, :port, :design_documents
|
47
43
|
|
48
44
|
def connect
|
49
|
-
@server = CouchServerConnection.new({"host"=>@host, "port"=>@port, "user"=>@user, "password"=>@password})
|
45
|
+
@server = CouchServerConnection.new({"host"=>@host, "port"=>@port, "user"=>@user, "password"=>@password, "database" => @name})
|
50
46
|
if @server.responds?
|
51
|
-
return {"ok"=>true, "message"=>"Successfully connected to the couch database at #{@host}
|
47
|
+
return {"ok"=>true, "message"=>"Successfully connected to the couch database at #{@host}:#{@port}"}
|
52
48
|
else
|
53
49
|
return {"ok"=>false, "message"=>"Could not connect to the couch database."}
|
54
50
|
end
|
@@ -77,20 +73,6 @@ module ShyCouch
|
|
77
73
|
get_document_by_id(doc["id"])
|
78
74
|
}
|
79
75
|
end
|
80
|
-
def all_docs_with(attribute, value=nil)
|
81
|
-
#TODO - change this to build a couch map query, cache it in couch then call it
|
82
|
-
# maybe?!?!
|
83
|
-
docs = []
|
84
|
-
all_docs.each do |doc|
|
85
|
-
if value
|
86
|
-
docs << doc if doc[attribute] == value
|
87
|
-
else
|
88
|
-
docs << doc if doc[attribute]
|
89
|
-
end
|
90
|
-
end
|
91
|
-
return docs
|
92
|
-
rescue NameError
|
93
|
-
end
|
94
76
|
def uri
|
95
77
|
return "http://#{host}/#{port}/#{name}"
|
96
78
|
end
|
@@ -140,11 +122,12 @@ module ShyCouch
|
|
140
122
|
@port = args["port"]
|
141
123
|
@user = args["user"]
|
142
124
|
@password = args["password"]
|
125
|
+
@db_name = args["database"]
|
143
126
|
@options = options
|
144
127
|
end
|
145
128
|
|
146
129
|
def responds?
|
147
|
-
if get
|
130
|
+
if req(:get, "/")['couchdb'] == "Welcome"
|
148
131
|
true
|
149
132
|
else
|
150
133
|
false
|
@@ -154,111 +137,110 @@ module ShyCouch
|
|
154
137
|
end
|
155
138
|
|
156
139
|
def has_database?(db_name)
|
157
|
-
|
140
|
+
req(:get, "/#{db_name}/")
|
158
141
|
true
|
159
|
-
rescue
|
142
|
+
rescue RestClient::ResourceNotFound
|
160
143
|
false
|
161
144
|
end
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
def pull_all_design_docs(db_name)
|
176
|
-
pull_all_doc_ids(db_name).map { get_document_by_id(db_name, id) if id[0,7] == "_design" }
|
145
|
+
|
146
|
+
# def get_database_info(db_name)
|
147
|
+
# get("/#{db+name}/")
|
148
|
+
# end
|
149
|
+
|
150
|
+
def req(kind, uri, data = nil)
|
151
|
+
raise TypeError unless [:get,:delete,:put,:post].include?(kind) # only support these 4 request methods currently
|
152
|
+
res = (@user and @password ? couch_req_with_auth(kind, uri, data) : couch_req_without_auth(kind, uri, data))
|
153
|
+
JSON.parse(res)
|
154
|
+
end
|
155
|
+
def req_host
|
156
|
+
"http://#{(@user + ':' + @password + '@') if @user and @password}#{@host}:#{@port}"
|
177
157
|
end
|
158
|
+
def couch_req_with_auth(kind, uri = nil, data = nil)
|
159
|
+
uri ? uri = req_host + uri : uri = req_host
|
160
|
+
if kind == :get or kind == :delete
|
161
|
+
RestClient.method(kind).call(uri, :content_type => :json, :user => @user, :password => @password)
|
162
|
+
else
|
163
|
+
RestClient.method(kind).call(uri, data, :content_type => :json)#, :user => @user, :password => @password)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
def couch_req_without_auth(kind, uri, data = nil)
|
167
|
+
uri ? uri = req_host + uri : uri = req_host
|
168
|
+
if kind == :get or kind == :delete
|
169
|
+
RestClient.method(kind).call(uri, :content_type => :json, :user => @user, :password => @password)
|
170
|
+
else
|
171
|
+
RestClient.method(kind).call(uri, data,:content_type => :json, :user => @user, :password => @password)
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
# this should be done w/ couch key stuff?
|
177
|
+
# def pull_all_design_docs(db_name)
|
178
|
+
# pull_all_doc_ids(db_name).map { get_document_by_id(db_name, id) if id[0,7] == "_design" }
|
179
|
+
# end
|
178
180
|
|
181
|
+
#TODO - this is screwed
|
179
182
|
def pull_all_doc_ids(db_name)
|
180
|
-
get
|
183
|
+
req(:get,"/#{db_name}/_all_docs")["rows"].map { |doc| doc["id"] }
|
181
184
|
end
|
182
185
|
|
183
186
|
def all_docs_from_database(db_name)
|
184
|
-
pull_all_doc_ids(db_name).map { |id| Data::CouchDocument.new(get
|
187
|
+
pull_all_doc_ids(db_name).map { |id| Data::CouchDocument.new(req(:get,"/#{db_name}/#{id}")) }
|
185
188
|
end
|
186
189
|
|
187
190
|
def get_document_by_id(db_name, id)
|
188
|
-
document = Data::CouchDocument.new(get
|
191
|
+
document = Data::CouchDocument.new(req(:get,"/#{db_name}/#{id}"))
|
189
192
|
end
|
190
193
|
def pull_document(db_name, document)
|
191
|
-
document = Data::CouchDocument.new(get
|
194
|
+
document = Data::CouchDocument.new(req(:get,"/#{db_name}/#{document._id}"))
|
192
195
|
end
|
193
196
|
|
194
197
|
def delete_document(db_name, id)
|
195
198
|
delete("/#{db_name}/#{id}")
|
199
|
+
RestClient.delete ""
|
196
200
|
end
|
197
201
|
|
198
202
|
# Haven't decided whether PUT/POST should take a CouchDocument or a JSON string.
|
199
|
-
def put( uri, json = nil )
|
200
|
-
#TODO - make this private
|
201
|
-
req = Net::HTTP::Put.new(uri)
|
202
|
-
req["content-type"] = "application/json"
|
203
|
-
req.body = json unless json == nil
|
204
|
-
JSON.parse(request(req).body)
|
205
|
-
end
|
206
|
-
|
207
|
-
def post(uri, json = nil)
|
208
|
-
# couch uses POST for new documents and gives them an ID
|
209
|
-
req = Net::HTTP::Post.new(uri)
|
210
|
-
req["content-type"] = "application/json"
|
211
|
-
req.body = json unless json == nil
|
212
|
-
JSON.parse(request(req).body)
|
213
|
-
#TODO - return success more meaningfully maybe?
|
214
|
-
end
|
215
|
-
|
216
203
|
|
217
|
-
|
218
204
|
def push_document(db_name, document)
|
219
205
|
raise TypeError unless document.kind_of?(Data::CouchDocument)
|
220
206
|
raise JSON::GeneratorError unless document.valid?
|
221
207
|
if document["_rev"]
|
222
|
-
put
|
208
|
+
req(:put, "/#{db_name}/#{document._id}?rev=#{document._rev}/", document.to_json)
|
223
209
|
else
|
224
|
-
post
|
210
|
+
req(:post, "/#{db_name}/", document.to_json)
|
225
211
|
end
|
226
212
|
end
|
227
213
|
|
228
214
|
def create_db(db_name)
|
229
|
-
put
|
215
|
+
req(:put, "/#{db_name}/")
|
230
216
|
end
|
231
217
|
def delete_db(db_name)
|
232
|
-
delete
|
233
|
-
end
|
234
|
-
|
235
|
-
def UUID
|
236
|
-
get('/_uuids/')['uuids'][0]
|
218
|
+
req(:delete,"/#{db_name}/")
|
237
219
|
end
|
238
220
|
|
239
221
|
private
|
240
222
|
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
223
|
+
# def handle_failure(req, res)
|
224
|
+
# raise RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
|
225
|
+
# end
|
226
|
+
#
|
227
|
+
# def handle_error(e)
|
228
|
+
# raise RuntimeError.new("#{e.inspect}\n Maybe be due to illegal rev or id change")
|
229
|
+
# end
|
230
|
+
|
231
|
+
# def request(req)
|
232
|
+
# res = Net::HTTP.start(@host, @port) { |http|
|
233
|
+
# req.basic_auth(@user, @password) if @user and @password
|
234
|
+
# http.request(req)
|
235
|
+
# }
|
236
|
+
# unless res.kind_of?(Net::HTTPSuccess)
|
237
|
+
# handle_failure(req, res)
|
238
|
+
# end
|
239
|
+
# res
|
240
|
+
# rescue Errno::ECONNRESET => e
|
241
|
+
# handle_error(e)
|
242
|
+
# end
|
261
243
|
end
|
262
244
|
|
263
245
|
end
|
264
|
-
end
|
246
|
+
end
|
data/lib/ShyCouch/data.rb
CHANGED
@@ -6,7 +6,7 @@ module ShyCouch
|
|
6
6
|
class << self
|
7
7
|
# allows instance.class.requirements to be called
|
8
8
|
end
|
9
|
-
@@needs, @@suggests = [], []
|
9
|
+
@@needs, @@suggests, @@views = [], [], []
|
10
10
|
|
11
11
|
def initialize(hash={})
|
12
12
|
# Assumes that the "kind" is the class name unless explicitly stated otherwise
|
@@ -14,7 +14,7 @@ module ShyCouch
|
|
14
14
|
hash["kind"] = self.class.to_s.split("::").last unless hash["kind"]
|
15
15
|
merge!(hash)
|
16
16
|
raise TypeError unless valid?
|
17
|
-
|
17
|
+
set_up_views
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.all
|
@@ -56,8 +56,7 @@ module ShyCouch
|
|
56
56
|
return self.class.requirements
|
57
57
|
end
|
58
58
|
|
59
|
-
def pull(database
|
60
|
-
database ||= $couchdb
|
59
|
+
def pull(database)
|
61
60
|
new_doc = database.pull_document(self)
|
62
61
|
if new_doc
|
63
62
|
self.clear
|
@@ -65,8 +64,7 @@ module ShyCouch
|
|
65
64
|
end
|
66
65
|
end
|
67
66
|
|
68
|
-
def push(database
|
69
|
-
database ||= $couchdb
|
67
|
+
def push(database)
|
70
68
|
res = database.push_document(self)
|
71
69
|
self["_id"] = res["id"] unless self["_id"]
|
72
70
|
self["_rev"] = res["rev"]
|
@@ -143,12 +141,30 @@ module ShyCouch
|
|
143
141
|
def initialize(name, views=[])
|
144
142
|
merge! "_id" => "_design/#{name.to_s}"
|
145
143
|
@parser = ShyRubyJS::ShySexpParser.new
|
146
|
-
|
147
|
-
|
144
|
+
@views = views
|
145
|
+
merge_views
|
146
|
+
end
|
147
|
+
|
148
|
+
def add_view(view)
|
149
|
+
raise TypeError unless view.kind_of?(ShyCouch::Data::View)
|
150
|
+
@views << view
|
151
|
+
merge_views
|
152
|
+
end
|
153
|
+
|
154
|
+
def view(view_name, &block)
|
155
|
+
add_view(ShyCouch::Data::View.new(view_name, &block))
|
156
|
+
end
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
def merge_views
|
161
|
+
h = { "views" => {}}
|
162
|
+
@views.each do |view|
|
148
163
|
h["views"][view.name] = view.functions
|
149
164
|
end
|
150
165
|
merge! h
|
151
166
|
end
|
167
|
+
|
152
168
|
end
|
153
169
|
|
154
170
|
end
|
data/test/test_ShyCouch.rb
CHANGED
@@ -22,12 +22,12 @@ require_relative 'test_couchdb_api'
|
|
22
22
|
# test ShyCouch::Fields
|
23
23
|
# some of the tests in here are disabled cos they involve attempting to resolve a bad domain name
|
24
24
|
require_relative 'test_fields'
|
25
|
-
|
26
|
-
# test ShyCouch::Data::CouchDocument
|
25
|
+
#
|
26
|
+
# # test ShyCouch::Data::CouchDocument
|
27
27
|
require_relative 'test_couch_document'
|
28
|
-
|
28
|
+
#
|
29
29
|
require_relative 'test_couchdb_factory'
|
30
|
-
|
30
|
+
#
|
31
31
|
require_relative 'test_design_documents'
|
32
|
-
|
32
|
+
#
|
33
33
|
require_relative 'test_views'
|
data/test/test_couch_document.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require_relative '../lib/ShyCouch.rb'
|
3
3
|
|
4
|
-
class CouchDocumentTests
|
4
|
+
class CouchDocumentTests
|
5
5
|
|
6
6
|
class TestDocumentCreation < Test::Unit::TestCase
|
7
7
|
def setup
|
8
8
|
valid_settings = $settings
|
9
|
-
@couchdb = ShyCouch.
|
9
|
+
@couchdb = ShyCouch.getDB(valid_settings)
|
10
10
|
end
|
11
11
|
def teardown
|
12
12
|
@couchdb.delete_database
|
@@ -53,7 +53,7 @@ class CouchDocumentTests# < Test::Unit::TestCase
|
|
53
53
|
# assumes success of the stuff in TestDocumentPulling
|
54
54
|
def setup
|
55
55
|
valid_settings = $settings
|
56
|
-
$couchdb = ShyCouch.
|
56
|
+
$couchdb = ShyCouch.getDB(valid_settings)
|
57
57
|
|
58
58
|
|
59
59
|
@valid_documents = [
|
@@ -69,7 +69,7 @@ class CouchDocumentTests# < Test::Unit::TestCase
|
|
69
69
|
ShyCouch::Data::CouchDocument.new("whatever"=>"yep"),
|
70
70
|
ShyCouch::Data::CouchDocument.new("is_a_document"=>true, "number_of_docs_this_is"=>1)
|
71
71
|
].each { |doc|
|
72
|
-
doc.push
|
72
|
+
doc.push($couchdb)
|
73
73
|
}
|
74
74
|
@invalid_documents = nil # make sure user can't set rev maybe? or is that legal?
|
75
75
|
end
|
@@ -80,30 +80,30 @@ class CouchDocumentTests# < Test::Unit::TestCase
|
|
80
80
|
end
|
81
81
|
|
82
82
|
def test_keys_as_attr_accessors
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
83
|
+
# tests that if there is a "phone" key on "doc" object you can do doc.phone
|
84
|
+
@valid_documents.each { |doc|
|
85
|
+
doc.keys.each { |key|
|
86
|
+
assert_respond_to(doc, key)
|
87
|
+
}
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
91
|
def test_push_new_documents
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
92
|
+
@valid_documents.each { |doc|
|
93
|
+
# put the document on the server, grab the server's response
|
94
|
+
res = doc.push($couchdb)
|
95
|
+
# check that the server included "ok"=>true in its response
|
96
|
+
assert(res["ok"])
|
97
|
+
# check that the doc now has an id and a rev
|
98
|
+
assert(doc["_id"])
|
99
|
+
assert(doc["_rev"])
|
100
|
+
# get the new doc
|
101
|
+
newDoc = $couchdb.pull_document(doc)
|
102
|
+
# test equality of all the attributes aside from id and rev on the new document
|
103
|
+
doc.attr_keys.each { |k|
|
104
|
+
assert_equal(doc["k"], newDoc["k"])
|
105
|
+
}
|
106
|
+
}
|
107
107
|
end
|
108
108
|
|
109
109
|
def test_change_existing_documents
|
@@ -116,7 +116,7 @@ class CouchDocumentTests# < Test::Unit::TestCase
|
|
116
116
|
doc.buttonCount = 5
|
117
117
|
doc.friends = ["alan", "alex", "all me other mates"]
|
118
118
|
|
119
|
-
res = doc.push
|
119
|
+
res = doc.push($couchdb)
|
120
120
|
assert(res["ok"])
|
121
121
|
|
122
122
|
# pull it from the database again
|
@@ -132,8 +132,8 @@ class CouchDocumentTests# < Test::Unit::TestCase
|
|
132
132
|
def test_illegal_change_to_rev
|
133
133
|
@existing_valid_documents.each { |doc|
|
134
134
|
doc._rev = "hurr"
|
135
|
-
assert_raise
|
136
|
-
res = doc.push
|
135
|
+
assert_raise RestClient::BadRequest do
|
136
|
+
res = doc.push($couchdb)
|
137
137
|
end
|
138
138
|
}
|
139
139
|
end
|
data/test/test_couchdb_api.rb
CHANGED
@@ -40,9 +40,7 @@ class DesignDocumentTests
|
|
40
40
|
design = setup_design_document
|
41
41
|
@couchdb.add_design_documents_and_push(design)
|
42
42
|
add_some_documents
|
43
|
-
puts design.views["count_recipes"]
|
44
|
-
require 'irb'
|
45
|
-
IRB.start
|
43
|
+
# puts design.views["count_recipes"]
|
46
44
|
end
|
47
45
|
|
48
46
|
def setup_views
|
data/test/test_views.rb
CHANGED
@@ -23,31 +23,31 @@ class CouchViewTests < Test::Unit::TestCase
|
|
23
23
|
|
24
24
|
end
|
25
25
|
|
26
|
-
def test_define_map_view
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_define_map_and_reduce_view
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
26
|
+
# def test_define_map_view
|
27
|
+
# view :five_star_butts do
|
28
|
+
# map do
|
29
|
+
# # def function(doc)
|
30
|
+
# emit(doc) if doc.kind == "butt" and doc.star_rating == 5
|
31
|
+
# # end
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
# expected_js = JS_MAP_FUNCTION_HEADER + %{if( doc.kind == 'butt' && doc.star_rating == 5 ) {\n emit(doc)\n}} + JS_FUNCTION_FOOTER
|
35
|
+
# assert_equal(expected_js, @couch_views[0].map)
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# def test_define_map_and_reduce_view
|
39
|
+
# view :beggar_count do
|
40
|
+
# map do
|
41
|
+
# emit(doc) if doc.kind == "beggar"
|
42
|
+
# end
|
43
|
+
# reduce do
|
44
|
+
# return sum(values)
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
# expected_map = JS_MAP_FUNCTION_HEADER + %{if( doc.kind == 'beggar' ) {\n emit(doc)\n}} + JS_FUNCTION_FOOTER
|
48
|
+
# expected_reduce = JS_REDUCE_FUNCTION_HEADER + %{return sum(values);} + JS_FUNCTION_FOOTER
|
49
|
+
# assert_equal(expected_map, @couch_views[0].map)
|
50
|
+
# assert_equal(expected_reduce, @couch_views[0].reduce)
|
51
|
+
# end
|
52
52
|
|
53
53
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ShyCouch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-09-
|
14
|
+
date: 2011-09-18 00:00:00.000000000Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: ShyRubyJS
|
18
|
-
requirement: &
|
18
|
+
requirement: &70144500883400 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ! '>='
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: '0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *70144500883400
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
|
-
requirement: &
|
29
|
+
requirement: &70144500899300 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ~>
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: 1.0.0
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *70144500899300
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: jeweler
|
40
|
-
requirement: &
|
40
|
+
requirement: &70144500898820 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
@@ -45,10 +45,10 @@ dependencies:
|
|
45
45
|
version: 1.6.4
|
46
46
|
type: :development
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *70144500898820
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: rcov
|
51
|
-
requirement: &
|
51
|
+
requirement: &70144500898340 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - ! '>='
|
@@ -56,10 +56,10 @@ dependencies:
|
|
56
56
|
version: '0'
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
|
-
version_requirements: *
|
59
|
+
version_requirements: *70144500898340
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
61
|
name: sourcify
|
62
|
-
requirement: &
|
62
|
+
requirement: &70144500897860 !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
64
64
|
requirements:
|
65
65
|
- - ~>
|
@@ -67,10 +67,10 @@ dependencies:
|
|
67
67
|
version: 0.5.0
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
|
-
version_requirements: *
|
70
|
+
version_requirements: *70144500897860
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
72
|
name: ShyRubyJS
|
73
|
-
requirement: &
|
73
|
+
requirement: &70144500897380 !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
76
76
|
- - ! '>='
|
@@ -78,10 +78,10 @@ dependencies:
|
|
78
78
|
version: '0'
|
79
79
|
type: :development
|
80
80
|
prerelease: false
|
81
|
-
version_requirements: *
|
81
|
+
version_requirements: *70144500897380
|
82
82
|
- !ruby/object:Gem::Dependency
|
83
83
|
name: ShyRubyJS
|
84
|
-
requirement: &
|
84
|
+
requirement: &70144500896900 !ruby/object:Gem::Requirement
|
85
85
|
none: false
|
86
86
|
requirements:
|
87
87
|
- - ! '>='
|
@@ -89,10 +89,21 @@ dependencies:
|
|
89
89
|
version: '0'
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
|
-
version_requirements: *
|
92
|
+
version_requirements: *70144500896900
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: rest-client
|
95
|
+
requirement: &70144500896420 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 1.6.7
|
101
|
+
type: :runtime
|
102
|
+
prerelease: false
|
103
|
+
version_requirements: *70144500896420
|
93
104
|
- !ruby/object:Gem::Dependency
|
94
105
|
name: sourcify
|
95
|
-
requirement: &
|
106
|
+
requirement: &70144500895940 !ruby/object:Gem::Requirement
|
96
107
|
none: false
|
97
108
|
requirements:
|
98
109
|
- - ! '>='
|
@@ -100,7 +111,7 @@ dependencies:
|
|
100
111
|
version: '0'
|
101
112
|
type: :runtime
|
102
113
|
prerelease: false
|
103
|
-
version_requirements: *
|
114
|
+
version_requirements: *70144500895940
|
104
115
|
description: Ruby API for CouchDB, designed to work with the Camping micro-framework.
|
105
116
|
email: danbryan@gmail.com
|
106
117
|
executables: []
|
@@ -117,6 +128,7 @@ files:
|
|
117
128
|
- Rakefile
|
118
129
|
- ShyCouch.gemspec
|
119
130
|
- VERSION
|
131
|
+
- design_notes.rb
|
120
132
|
- lib/ShyCouch.rb
|
121
133
|
- lib/ShyCouch/data.rb
|
122
134
|
- lib/ShyCouch/fields.rb
|
@@ -145,7 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
157
|
version: '0'
|
146
158
|
segments:
|
147
159
|
- 0
|
148
|
-
hash:
|
160
|
+
hash: 2267181098830076376
|
149
161
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
162
|
none: false
|
151
163
|
requirements:
|