S.E.A.N.I.C.U.S.

Thursday, July 13, 2006

Potential DSLs

Sidebar: Yes I haven't posted here in a while -- I've been trying to get RadiantCMS to bend to my needs in terms of blogging, but that may be a ways off. We now return you to your regularly scheduled blog post...

One of the recent patterns with Rails has been to move most things into Ruby. Most recently, developers are encouraged (but not required) to move their database definitions into ActiveRecord::Migration format, both through a change in the default environment.rb file, and through automatic migration creation when generating a model. "Cool!" I said when I saw that.

In listening to this week's Ruby on Rails Podcast the issue of YAML came up, and it got me thinking. Yes, YAML is very simple and cleanly defines both the database configuration and fixtures, but couldn't the same thing be accomplished in Ruby? Jamis and others have encouraged the use of DSLs to simplify and clarify code. Since Django uses Python to describe its database configuration, why not use Ruby for Rails'?

I'm not sure whether I know how to accomplish it, but I'm going to try! Here's a sample of my proposed database configuration DSL:
common {
driver "mysql"
username "root"
password ""
host "example.com"
port "3306"
}

development {
<< "common"
database "app_development"
}

production {
<< "common"
database "app_production"
}

test {
<< "common"
database "app_test"
}
Since the specifying of common attributes and the "including" of them into other configurations seems popular, I decided to allow that with the "<<" operator, similarly to the way it's done in YAML.

The fixtures DSL would follow a pretty similar pattern, with the fixture name, followed with a block and corresponding attributes.

I'll probably post the code here when I finish prototyping it.

Your thoughts?