YAML Configuration Files
December 28, 2008
Sometimes it’s nice to have an easy place you can update options without having them hard coded into files buried in your application.
I find it somewhat bizarre that this is not included in the Rails framework, however it’s probably to do with keeping the directory tree clean. Anyway, it’s not difficult to implement yourself.
Creating the file
First we’ll need to create our file. I find the most suitable place for it to be located is _RAILS_ROOT/config/config.yml_, but you can save it wherever. The file will be converted to a hash, with strings as keys so you can nest your configuration as such:
uploads:
timeout: 600
max_size_mb: 100
Loading the file
Rather than loading the file in for each request, we only need load it once when the application is booted up, so past the following code into an initializer or the bottom of your config/environment.rb file:
Now you can reference your configuration settings using the constant SETTINGS, e.g:
SETTINGS['uploads']['max_size_mb']
Nobody has commented yet, be the first? Don't be shy.