Tuesday, April 7, 2009

More Helper Specs

‹prev | My Chain | next›

A long softball game (8 walks an inning was typical), precludes me from making much progress tonight. Nevertheless I must offer up some work to the gods of #mychain, lest they take offense and smite me.

As fate would have it, I left off last night with some fairly simple helper specs pending implementation. So let's implement...

First up is that the wiki helper should convert textile to HTML. To specify this, I use an example of some very simple textile, which should be converted to HTML. The RedCloth library can take care of the full specification, I am only interested in specifying that it quacks like a textile converter:
  it "should convert textile to HTML" do
textile = <<_TEXTILE
paragraph 1 *bold text*

paragraph 2
_TEXTILE

wiki(textile).
should have_selector("p", :content => "paragraph 1 bold text") do |p|
p.should have_selector("strong", :content => "bold text")
end
end
Implementation of that is as easy as wrapping the wiki method around a RedCloth call:
    def wiki(text)
RedCloth.new(text).to_html
end
Next up is that the wiki call should wikify temperatures, which can be specified as:
  it "should wikify temperatures" do
wiki("250F").should contain("250° F")
end
This can be implemented as:
    def wiki(original)
text = original.dup
text.gsub!(/\b(\d+)F/, "\\1° F")
RedCloth.new(text).to_html
end
Seeing this capricious use of gsub immediately makes me think of the Eigenclass class article on markdown conversion.

I am doing bad things.
I am doing bad things.
I am doing bad things.

But, I must not prematurely optimize. If this does cost me in performance, I will deal with it later.

Staying focused on implementation rather than optimization, I move on to writing the example for wikifying kid's nicknames:
  it "should wikify kids names" do
self.stub!(:kid_nicknames).
and_return({"marsha" => "the oldest Brady girl"})

wiki("[kid:marsha]").should contain("the oldest Brady girl")
end
Implementing the code to make this example pass is a simple matter of another gsub:
    def wiki(original)
text = original.dup
text.gsub!(/\b(\d+)F/, "\\1° F")
text.gsub!(/\[kid:(\w+)\]/m) { |kid| kid_nicknames[$1] }
RedCloth.new(text).to_html
end

def kid_nicknames
{ }
end
There is still the matter of how to populate the kid_nicknames method. Hard-coding it will not do—there is not much point in obfuscating names if I make them publicly accessible elsewhere. I either need to make it a YAML configuration file or store it in CouchDB. I think I will ultimately choose to store it in CouchDB since that is easier to update (most days I think I have enough kids already, but you never know).

Since implementation will have to wait for tomorrow, I leave myself a note:
  it "should lookup kid nicknames in CouchDB"
and call it a day.
(commit)

No comments:

Post a Comment