Wednesday, July 8, 2009

Dem Bones

‹prev | My Chain | next›

I happened to come across Jamie van Dyke's Building a Gem Using BDD article today. It seems the gods of my chain are telling me something and I must listen...

You can probably safely ignore this article, Jamie's write up is far superior...

First up, I install bones:
cstrom@jaynestown:~/repos$ gem install bones
--------------------------
Keep rattlin' dem bones!
--------------------------
Successfully installed bones-2.5.1
1 gem installed
Then I create my gem template:
cstrom@jaynestown:~/repos$ bones create couch_design_docs
Created 'couch_design_docs'
Now you need to fix these files
(in /home/cstrom/repos/couch_design_docs)
README.txt:
* [ 2] [FIXME] (your name)
* [ 3] [FIXME] (url)
* [ 7] [FIXME] (describe your package)
* [ 11] [FIXME] (list of features or problems)
* [ 15] [FIXME] (code sample of usage)
* [ 19] [FIXME] (list of requirements)
* [ 23] [FIXME] (sudo gem install, anything else)
* [ 29] [FIXME] (different license?)

Rakefile:
* [ 22] [FIXME] (who is writing this software)'
* [ 23] [FIXME] (your e-mail)'
* [ 24] [FIXME] (project homepage)'
After fixing as many of those FIXMEs as possible, I move onto implementation. First up is directory parsing. In order to directory parse, the CouchDesignDocs::Directory object is going to need a valid directory. After creating a fixture directory, I begin driving development with spec/couch_design_docs_spec.rb:
require File.join(File.dirname(__FILE__), %w[spec_helper])

describe Directory do
it "should require a root directory for instantiation" do
lambda { Directory.new }.
should raise_error

lambda { Directory.new("foo") }.
should raise_error

lambda { Directory.new("fixtures")}.
should_not raise_error
end
end
Note: I added include CouchDesignDocs to spec/spec_helper.rb so that I could access Directory without the CouchDesignDocs:: namespace.

I implement that code with:
module CouchDesignDocs
class Directory
attr_accessor :couch_view_dir
def initialize(path)
Dir.new(path) # Just checkin'
@couch_view_dir = path
end
end
end
The Dir.new call is made only to raise an exception for invalid paths. It is a quick, cheap way to get the code to behave as desired. Next I create a "valid directory" context and drive the file path expansion needed to build the CouchDB design document JSON structure:
  context "a valid directory" do
before(:each) do
@it = Directory.new("fixtures")
end
it "should list dirs, basename and contents of a file" do
@it.expand_file("fixtures/a/b/c.js").
should == ['a', 'b', 'c', "function(doc) { return true; }\n"]
end
end
After creating the fixtures/a/b/ directory and populating it with a very simple javascript function, I make the example pass with this code:
    def expand_file(filename)
File.dirname(filename).
gsub(/#{couch_view_dir}\/?/, '').
split(/\//) +
[
File.basename(filename, '.js'),
File.new(filename).read
]
end
That will do for a stopping point tonight, I will continue with dem bones gem development tomorrow.

2 comments:

  1. I love bones. So much better organized than some of the other gem-starter tools. One of the side benefits of spending time in Boulder was having the opportunity to meet its creator, Tim Pease. Nice guy.

    ReplyDelete
  2. Definitely digging it so far. I have not done enough other gems - at least not with enough regularity -- to get a good feel for them. Still, it was _much_ easier to jump into my normal BDD workflow with bones than when I got started with other starter kits.

    I'll have to give you beer money so you can thank Tim for me. Sounds like a lot of cool things are going on in Co. nowadays!

    ReplyDelete