Sunday, May 24, 2009

Ah, Sweet Re-Use

‹prev | My Chain | next›

Having finished up work inside the meals-by-month Haml template last night, it's back out to the Cucumber scenario to double check things and see what's next.

After a quick fix (adding menu items to the Given step), I am at:



Looking at those next two steps, I get to thinking that they are very similar to the link to next / previous years work that I already did. The current implementation:
    def link_to_year_in_set(current, couch_view, options={})
compare_years = options[:previous] ?
Proc.new { |year, current_year| year < current_year} :
Proc.new { |year, current_year| year > current_year}

next_result = couch_view.
send(options[:previous] ? :reverse : :map).
detect{|result| compare_years[result['key'].to_i, current.to_i]}

if next_result
%Q|<a href="/meals/#{next_result['key']}">#{next_result['key']}</a>|
else
""
end
end
Aside from the method name and variable names seemingly tied to a year, there is nothing in there that is specific to years—except the to_i call on the years (won't work on "2009-05"). If I use strings instead, that helper ought to work just as well for ISO 8601 substrings (e.g. 2009-05). The above line, reworked for strings:
        detect{|result| compare_years[result['key'], current.to_s]}
After running all of my specs and Cucumber scenarios to verify that the change had no effect on the underlying behavior, I add this to meals-by-month Haml template:
%div.navigation
=link_to_year_in_set("#{@year}-#{@month}", @count_by_year, :previous => true)
|
=link_to_year_in_set("#{@year}-#{@month}", @count_by_year)
To verify that this is working, I implement the next two Cucumber scenario steps as:
Then /^I should not see a link to June of 2009$/ do
response.should_not have_selector("a", :content => "2009-06")
end

When /^I follow the link to the list of meals in April of 2009$/ do
click_link "2009-04"
end
And they pass!



Not sure why that next scenario is failing, but it'll serve as an obvious spot to pick up tomorrow.

2 comments:

  1. You are a shining example of behaviour driven development!

    I'm really enjoying this series especially as it is teaching me so many CouchDB tricks. It's going to be an excellent reference point for so many people.

    ReplyDelete
  2. Aimee, thanks so much for the kinds words! Although it can be something of a chore (no pun intended) to keep up with this daily, I'm having fun and learning tons—making it completely worth it.

    Hearing that others find it useful is wonderful! It goes a long way toward encouraging me on those days when it feels more like a chore. So thanks!

    ReplyDelete