Saturday, July 11, 2009

Working with the New Gem

‹prev | My Chain | next›

I continue working on couch_design_docs, a gem to load javascript files from the file system into my CouchDB store as design docs.

Creating a gemspec (needed by github) is easy with bones:
cstrom@jaynestown:~/repos/couch_design_docs$ rake gem:spec
(in /home/cstrom/repos/couch_design_docs)
cstrom@jaynestown:~/repos/couch_design_docs$ git status
# On branch master
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# couch_design_docs.gemspec
After pushing the changes to github and a little wait, I can now install my own gem:
cstrom@jaynestown:~$ gem install eee-c-couch_design_docs
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
Successfully installed eee-c-couch_design_docs-1.0.0
1 gem installed
I could also have installed from my local copy using bones's rake gem:install.

With the gem installed, I can take it for a ride:
cstrom@jaynestown:~/repos/eee-code$ irb
>> require 'couch_design_docs'
=> true
Cool! That was easy enough. To instantiate a Store object:
>> store = CouchDesignDocs::Store.new("http://localhost:5984/eee")
=> #<CouchDesignDocs::Store:0xb7a07c58 @url="http://localhost:5984/eee">
And a local document store:
>> dir = CouchDesignDocs::Directory.new("/home/cstrom/repos/eee-code/couch/_design")
=> #<CouchDesignDocs::Directory:0xb7970948 @couch_view_dir="/home/cstrom/repos/eee-code/couch/_design">
>> dir.to_hash
=> {"lucene"=>{"transform"=>"function(doc) { … }"}}
Nice! So, can I get the store to upload the design document?
>> store.load(dir.to_hash)
RestClient::RequestFailed: HTTP status code 409
from /usr/lib/ruby/gems/1.8/gems/rest-client-1.0/lib/restclient/request.rb:193:in `process_result'
from /usr/lib/ruby/gems/1.8/gems/rest-client-1.0/lib/restclient/request.rb:123:in `transmit'
from /usr/lib/ruby/1.8/net/http.rb:543:in `start'
Conflict, bummer. That is something missing from the gem—the ability to replace existing documents. For now, I will manually delete the couchdb-lucene design document:



With that, I can retry:
>> store.load(dir.to_hash)
=> {"lucene"=>{"transform"=>"function(doc) { … }" }}
Yay! The .js file from the filesystem was actually PUT in the CouchDB data store as desired.

I spend a little more time with the document store class—driving by example the ability to replace an existing design document. I hope to be able to finish up the gem tomorrow by adding some convenience methods and the ability to replace existing documents.
(commit)

2 comments:

  1. Are you working on a Ruby version of CouchApp (http://groups.google.com/group/couchapp) or is the scope of your project different?

    ReplyDelete
  2. I was not planning to do something as encompassing as CouchApp.

    I am more comfortable using CouchDB as a data store and something else (like Sinatra) as a presentation / application layer. That is what I am looking to accomplish here -- a means to manage the design docs as part of a Sinatra app. I am keeping a eye toward being able to better test the javascript functions and possibly allow for code re-use (via Erb).

    Still, from what I have seen of CouchApp, this is very similar. So who knows? Maybe it will evolve into a CouchApp for ruby.

    ReplyDelete