Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Created December 13, 2009 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baroquebobcat/255583 to your computer and use it in GitHub Desktop.
Save baroquebobcat/255583 to your computer and use it in GitHub Desktop.
# My monkey patch to couchrest to make it work transparently
# with couchdb 0.8, the version currently in debian and ubuntu.
#
#----making couchrest view 0.8 friendly----
#autoloading for monkey patching
CouchRest::Server
CouchRest::Database
module CouchRest
class Server
def version
@version ||=CouchRest.get("#{@uri}/")['version']
end
def version_0_8?
version.include? '0.8'
end
end
class Database
def view_url dname,vname
if server.version_0_8?
"#{@root}/_view/#{dname}/#{vname}"
else
"#{@root}/_design/#{dname}/_view/#{vname}"
end
end
# Query a CouchDB view as defined by a <tt>_design</tt> document. Accepts
# paramaters as described in http://wiki.apache.org/couchdb/HttpViewApi
def view(name, params = {}, &block)
if server.version_0_8? && limit = params.delete(:limit)
params[:count] = limit
end
keys = params.delete(:keys)
name = name.split('/') # I think this will always be length == 2, but maybe not...
dname = name.shift
vname = name.join('/')
url = CouchRest.paramify_url view_url(dname,vname) , params
if keys
CouchRest.post(url, {:keys => keys})
else
if block_given?
@streamer.view(view_url(dname,vname), params, &block)
else
CouchRest.get url
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment