More Where Clause goodness.
Posted by ezmobius Wed, 04 Jan 2006 23:44:00 GMT
I have been working on the where plugin some more trying to make the syntax a bit more natural ruby. Here is what the latest version looks like to use:
Model.find_with_conditons( :all, :limit => ..., :order => ... ) do foo == 'bar' # ["foo = ?", 'bar] baz <=> (1..100) # ["baz BETWEEN ?AND ?", 1,100] woo =~ 'substri%' # ["woo LIKE ?", 'substri%'] id === (1..4) # ["id IN (?)", [1,2,3,4]] fiz < 10 # lt, gt, leq, geq, etc., should all "just work" end
I like this because now it really just looks like ruby. Gotta love using the spaceship operator <=> to sugnify a [BETWEEN ? and ?, 1, 10]! The =~ or regex match operatpr in ruby gets converted into a LIKE query in the db. Also just added IN (?) support using the === operator. And you can still use :limit, :order and :joins plus :all or :first and it just works. I still need to convert the tests to use the new syntax so that will be done this weekend.
You can get the development version here if you want to play now. Or wait until the weekend when I add a few more features like the ability to use OR instead of AND for arbitrary items in the :conditions clause.