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 (whose author was on
the podcast) uses Python to describe its database configuration, why not
use Ruby for Rails’?
Here’s a sample of a DB config in the DSL:
common {
adapter “mysql”
username “root”
password “”
host “example.com”
port “3306”
}
development {
<< “common”
database “app_development”
}
production {
<< “common”
database “app_production”
}
test {
<< “common”
database “app_test”
}
I did get this working as an instance_eval style DSL, except that the <<
lines had to have “self” preceding them; Ruby expects the << operator to
be operating on something (Does anyone know a way around this?).
Basically it produces a Hash-of-Hashes as the result.
I could forsee that this would be a great way to describe fixtures as
well, since it’s already Hash data, it could just be passed to
ActiveRecord and “BAM!” you have a fixture object.
Your thoughts?
Sean C.