Wednesday, March 10, 2010

Watch Prototype

‹prev | My Chain | next›

I promised myself in yesterday's retrospective that I would limit the number of couch_docs version 1.1 features to the bare minimum. Between that and a desire to have something to work on at the next B'more on Rails open source hack night, one might think that I would just call it a release and move on.

But no, I want just one more feature...

Specifically I would like the ability to watch a couch_docs document directory on the file system such that any changes cause an upload to the CouchDB server.

Rather than rolling my own directory watcher, I think I will give directory_watcher a whirl. I have not used it before, so I will prototype to learn here.

First up, install the gem:
cstrom@whitefall:~/repos/couch_docs$ gem install directory_watcher
WARNING: Installing to ~/.gem since /var/lib/gems/1.8 and
/var/lib/gems/1.8/bin aren't both writable.
Successfully installed directory_watcher-1.3.1
1 gem installed
Next, I add a -w option to the command line:
        opts.on("-w", "Watch for changes") do 
@options[:watch] = true
end
Then, when :watch is true, run with the directory_watcher gem:
        if @options[:watch]
dw = DirectoryWatcher.new @options[:target_dir]
dw.glob = '**/*.js'
dw.add_observer do |*args|
puts "Change detected pushing anew..."
CouchDocs.put_dir(@options[:couchdb_url],
@options[:target_dir])
end

dw.start
gets # when the user hits "enter",
# the script will terminate
dw.stop
else
...
With that, I run with the new command line option:
cstrom@whitefall:~/repos/couch_docs$ ./bin/couch-docs push -w http://localhost:5984/seed ~/tmp/seed/
Change detected pushing anew...
Hmmm... That is a little weird. There is not a change per se, just an initial load. No matter, this is just prototype code. I can worry about cleaning it up later. To test that this works, I edit a .js file
function (doc) {
// TESTING!!!!!!!!!!!!!
if (doc['type'] == 'Meal' && doc['published']) {
emit(doc['date'], doc);
}
}
After a looooong wait (I will have to investigate frequency options later), I see:
cstrom@whitefall:~/repos/couch_docs$ ./bin/couch-docs push -w http://localhost:5984/seed ~/tmp/seed/
Change detected pushing anew...
Change detected pushing anew...
And, sure enough the change shows up automatically in the design document on the CouchDB server:


Nice! I like that feature. I will dump this prototype code and implement for real tomorrow.

Day #38

No comments:

Post a Comment