Saturday, April 25, 2009

Pagination, Page 4

‹prev | My Chain | next›

I find myself struggling quite a bit of late against Sinatra. Perhaps I am trying to do more that it is really meant to do. Perhaps I am foisting my Rails mindset too much upon it. Probably a little bit of both, but I begin to suspect it is more of the latter.

I continue to see strict MVC where it may not apply. I also try to perform unit tests in isolation rather than taking a more holistic approach. So I take a step back and refactor some of my work from yesterday.

So instead of a pagination method signature like this:
pagination(query, skip, limit, total)
I stick with the results that come directly from the couchdb-lucene JSON results:
pagination(query, results)
With that done (and all the specs updated accordingly), I move from the helper back into the Sinatra app itself. I specify that pagination options passed to the Sinatra application should drive couchdb-lucene:
    it "should paginate" do

get "/recipes/search?q=title:eggs&page=2"
To get that passing without breaking any of the previous examples, I calculate the skip offset thusly:
get '/recipes/search' do
skip = (((params[:page] ? params[:page].to_i : 1) - 1) * 20) + 1
data = RestClient.get "#{@@db}/_fti?limit=20&skip=#{skip}&q=#{params[:q]}"
@results = JSON.parse(data)

@query = params[:q]

haml :search
With that, I have all of my examples passing:
cstrom@jaynestown:~/repos/eee-code$ rake
(in /home/cstrom/repos/eee-code)

Finished in 0.095031 seconds

7 examples, 0 failures

Finished in 0.014986 seconds

16 examples, 0 failures

Finished in 0.178643 seconds

31 examples, 0 failures
So it's back out to the feature. I wrote the step implementation last night describing what should happen on the last page of the results. That is what clued me in to the fact that the Sinatra app was not driving couchdb-lucene pagination. Tonight, I can use that same implementation to verify that this is now working:

The next steps in that scenario ought to all be working, only in need of step definitions to verify. Then it is on to a few boundary conditions.


No comments:

Post a Comment