Config So Simple Your Mama Could Use It

Tonight, Kastner asked me if I had anything to do some simple configuration for something he was working on. I’ve got a simple module and yaml file that I’ve been using so I gist’d it. It then occurred to me that I might as well share it here too.

The Yaml

Below is an example of the yaml file. Basically, I setup some defaults and then customize each environment as needed.

DEFAULTS: &DEFAULTS
  email: no-reply@harmonyapp.com
  email_signature: |
    Regards,
    The Harmony Team
 
development:
  domain: harmonyapp.local
  <<: *DEFAULTS
  
test:
  domain: harmonyapp.com
  <<: *DEFAULTS
 
production:
  domain: harmonyapp.com
  <<: *DEFAULTS

The Module

The module can read and write to the config and even loads the Yaml file the first time you try to read a configuration key.

module Harmony
  # Allows accessing config variables from harmony.yml like so:
  # Harmony[:domain] => harmonyapp.com
  def self.[](key)
    unless @config
      raw_config = File.read(RAILS_ROOT + "/config/harmony.yml")
      @config = YAML.load(raw_config)[RAILS_ENV].symbolize_keys
    end
    @config[key]
  end
  
  def self.[]=(key, value)
    @config[key.to_sym] = value
  end
end

If I wanted to get the domain, I would do the following:

Harmony[:domain]

Nothing fancy, but it gets the job done. I just drop the yaml file in config/ and the module in lib/. Obviously, you would rename the module and yaml file to whatever constant you want, such as App or something related to your application’s name. I know there are gems and plugins to do configuration, but when something this simple gets the job done, I figure why bother.

What do you all use for app configuration? What do you like about what you use?