Squirrel - Once More, with Feeling!

Jon Yurek

I admit, the first iteration of Squirrel wasn’t really as super fabulous as I thought it would be. Because of how I was using the blocks, you couldn’t do a simple thing like access params. And since half the fun of it was being able to make good looking, flexible queries, not being able to use params was a giant pain.

Now, however, I’ve managed to mix the clean, functional look of instance_eval with the husky utilitarianism of params to come up with a happier, shinier Squirrel:

users = User.find(:all) do
  blog.title == params[:blog_name]
end

And what’s that in the trees, gracefully swinging from vine to vine? It’s our friends any and all here to give us grouping blocks!

site = Site.find(:first) do
  any {
    domains.hostname == params[:hostname]
    domains.hostname == "www.#{params[:hostname]}"
  }
end

All the functions you’d expect to be able to use in your controller are available (well, as long as you actually are in your controller, anyway; squirrels aren’t miracle workers) params, session, etc.

You can use the unary minus to negate any condition or block, and you can also use it to do a descending order_by.

Post.find(:all) do
  -any {
    title =~ /^OMG/
    id == 4
  }
  order_by -created_on
end

This is a major rewrite from the original code and cleaned up things in a very good way. And what’s more, we’re using it in real code now, so I have a fire under my ass to keep it in fighting shape.

The repo is here for all of you itching to try it out.