Saturday, October 11, 2008

Ruby ORM API Design Clusterfuck

DataMapper criticizes ActiveRecord like Palin going after Obama. I'm going to avoid getting into that vortex of negativity, but I will say this: less ego. More ambition.

This is how you tell ActiveRecord you want to find all the Person models in your database whose age attributes are greater than or equal to 30.

Person.find(:all, :conditions => "age >= 30")

Or

Person.find(:all, :conditions => ["age >= ?", 30])

DataMapper thinks that's too much typing. Here's the DataMapper approach:

Person.all(:age.gte => 30)

Making code more compact is a worthy goal, but who writes a method named gte in a language which allows you to define >=? Where ActiveRecord is too much typing, DataMapper is too much remembering.

You can fork DataMapper and alias gte to >=. But I wouldn't recommend it.

Person.all(:age.>= => 30)

Here's how you do it in Ambition:

Person.select {|p| p.age >= 30}