BT

Your opinion matters! Please fill in the InfoQ Survey!

Abstracting Data Query in Ruby with Ambition Ambition

| by Sebastien Auvray Follow 0 Followers on Sep 26, 2007. Estimated reading time: 1 minute |

A note to our readers: As per your request we have developed a set of features that allow you to reduce the noise, while not losing sight of anything that is important. Get email and web notifications by choosing the topics you are interested in.

One of today's challenges is to reduce the complexity of accessing and integrating information from various sources. While .NET already has such LINQ, Ruby had no such solution. There are existing DSL projects for querying with Ruby but not as Ruby.

Chris Wanstrath brings his own solution: Ambition.  Ambition takes advantage of ParseTree to walk the parse tree and translate plain Ruby queries into SQL (when the target is a Relational Database), so at the end you'll be able to write your queries the Ruby way:

User.first
# which will translate into "SELECT * FROM users LIMIT 1"
User.select { |u| u.karma > 20 }.sort_by(&:karma).first(5)
# which will translate into "SELECT * FROM users WHERE (users.`karma` > 20)
# ORDER BY users.karma LIMIT 5"


While originally Chris was gunning for Rack, he changed his target since he discovered LINQ.
We’ve moved our sights from Rack to LINQ. That is, we don’t want to only support other ORMs—we want Ambition to be a query language for SQL, LDAP, XPath, the works. The 1.0 release will be backend-agnostic. Maybe then we’ll change the name to Hubris? Time will tell.
You can find latest sources at git://errtheblog.com/git/ambition

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Another Groovy plug :) by Thom Nichols

GORM does this very well too, although the approach is a bit different. I think I like this syntax better.

At some point GORM will support JPA, which (if I understand correctly) means it should be able to query XML and such as well.

Re: Another Groovy plug :) by Werner Schuster

There might be overlap in the querying features, but not in the approach. Following your link, GORM seems to use Builders or string queries, or something that seems to use the Groovy version of method_missing.

However, Ambition works by actually looking at the AST of the queries, and as such is similar to Macros in languages like LISP or Scheme. The link to LINQ (pun intended) is that LINQ uses the same approach.

Of course, the results of the various solutions are the same, but the approaches are different. Question is how well Ruby syntax lends itself to Macros: the Ambition queries still need to be legal Ruby code, otherwise it couldn't be parsed. This is different in LISP, since there isn't much syntax to speak of in the first place. Of course, taking a peek at LINQ can help here.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

2 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT