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_watcherNext, I add a
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
-w
option to the command line:opts.on("-w", "Watch for changes") doThen, when
@options[:watch] = true
end
:watch
is true, run with the directory_watcher gem:if @options[:watch]With that, I run with the new command line option:
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
...
cstrom@whitefall:~/repos/couch_docs$ ./bin/couch-docs push -w http://localhost:5984/seed ~/tmp/seed/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
Change detected pushing anew...
.js
file function (doc) {After a looooong wait (I will have to investigate frequency options later), I see:
// TESTING!!!!!!!!!!!!!
if (doc['type'] == 'Meal' && doc['published']) {
emit(doc['date'], doc);
}
}
cstrom@whitefall:~/repos/couch_docs$ ./bin/couch-docs push -w http://localhost:5984/seed ~/tmp/seed/And, sure enough the change shows up automatically in the design document on the CouchDB server:
Change detected pushing anew...
Change detected pushing anew...
Nice! I like that feature. I will dump this prototype code and implement for real tomorrow.
Day #38
No comments:
Post a Comment