Thanks to a bit of RubyToRuby we can send along the block to CouchDb just fine.
But, how is this all done on the CouchDb side of things? It’s actually a whole lot easier than it looks; all CouchDb does when it receives a view query like the above is pass it on to whatever is defined as the JsServer in $COUCH_INSTALL/couch.ini, this is normally SpiderMonkey, but with the CouchObject gem installed it can be Ruby!

Here’s the relevant section from my couch.ini:

…

You need full, or relative to couch install dir, paths for now
JsServer=/opt/local/bin/couch_ruby_view_requestor

…

So have a go at it:

$ sudo gem install couchobject

Check out the Git source and have a play with it:

$ git clone git://gitorious.org/projects/couchobject/mainline.git

Comments:

james Says:

Oct 10 at 23:09
Looks like the .ini file has changed format since you wrote this; it’s now reading like this (on my system)

[Couch Query Servers]

text/javascript=/opt/local/share/couchdb/server/couchjs -f main.js

Thanks for your work on this. Makes it easier to play around and kick the wheels. I’m currently deciding whether to commit to couch for a big project and your plugin has helped get my head around things …

Oct 11 at 06:23
yeah, I need to bring it up to date with the latest couch trunk, I’ve been busy with other things for the past two weeks. The good news is that it’s now possible to use several view servers at once, so you don’t have to be “locked in” to just using ruby, or any other language, only.

Oct 29 at 22:32
The new syntax for adding a query server to couch.ini is this:

text/ruby=/wherever/couchobject/bin/couch_ruby_view_requestor

The ruby query server is, as Johan says, out of date. I just submitted a patch on Rubyforge that fixes the one minor change in couch_ruby_view_requestor, updates the Ruby proc for the example view, and revises the specs and code where necessary to conform to the way CouchDB currently processes view.

I just realized that I forgot to update the README. Johan, I’m using git, but I’m not very familiar with shared development practices. I can make my changes available in a public git repo, if that is helpful to you.