Don't use ERB, instead use straight-Ruby. When we have complex YAML files to create at work, I start in Ruby, and have it emit the boilerplate:
require 'yaml'
# octopus:
# environments:
# - <%= MyApp::Config::deployment %>
# replicated: true
# fully_replicated: true
# development:
# slave1:
# host: <%= MyApp::Config::DatabaseSlaves.host(0) %>
# adapter: <%= MyApp::Config::DatabaseSlaves.adapter(0) %>
# database: <%= MyApp::Config::DatabaseSlaves.database(0) %>
# username: <%= MyApp::Config::DatabaseSlaves.username(0) %>
# password: <%= MyApp::Config::DatabaseSlaves.password(0) %>
# reconnect: <%= MyApp::Config::DatabaseSlaves.reconnect(0) %>
config = {
'octopus' => {
'environments' => 'MyApp::Config.deployment',
'replicated' => true,
'fully_replicated' => true,
'development' => {
'slave1' => {
'host' => 'MyApp::Config::DatabaseSlaves.host(0)',
'adapter' => 'MyApp::Config::DatabaseSlaves.adapter(0)',
'database' => 'MyApp::Config::DatabaseSlaves.database(0)',
'username' => 'MyApp::Config::DatabaseSlaves.username(0)',
'password' => 'MyApp::Config::DatabaseSlaves.password(0)',
'reconnect' => 'MyApp::Config::DatabaseSlaves.reconnect(0)',
}
}
}
}
puts config.to_yaml
Which generates:
---
octopus:
environments: MyApp::Config.deployment
replicated: true
fully_replicated: true
development:
slave1:
host: MyApp::Config::DatabaseSlaves.host(0)
adapter: MyApp::Config::DatabaseSlaves.adapter(0)
database: MyApp::Config::DatabaseSlaves.database(0)
username: MyApp::Config::DatabaseSlaves.username(0)
password: MyApp::Config::DatabaseSlaves.password(0)
reconnect: MyApp::Config::DatabaseSlaves.reconnect(0)
Once I have the basic template I can either modify it by hand or flesh out the code to actually generate everything I need. I do this because some applications have a lot of configuration parameters, and having to generate that configuration by hand would be too error-prone. Instead a YAML-file generator can do it in a second then we can tweak as necessary.