Tarot for easier Rails configurations

Once upon a time, I wrote a quick-and-dirty Rails plugin for site configuration. Since then, I’ve continued to use variants on this pattern, and it’s evolved to the point that it deserved a revisit.

After continually slimming down the code, I realized that even though it’s tiny, it’s danged useful to be able to just drop this into a Rails app and go. Thus, I’d like to present Tarot, my Rails configuration solution.

Tarot’s current form is heavily inspired by the Rails I18n usage, and is very quick and easy to use in your app. The generator installs a sample yaml file at config/tarot.yml, as well as an initializer to bootstrap your configuration and provide a handy helper method for quick access to those config values.

Assuming you have a config file like so:

base: &base
  foo: bar
    tree: value
    - value 1
    - value 2

development: &development
  <<: *base

test: &test
  <<: *base

production: &production
  <<: *base
  foo: baz

You’ll notice that all the environments inherit from your base environment; this gives you an easy way to define common settings once, then override them per environment. Handy!

You could can access values by key, or by dot-delimited path:

config('foo') => 'bar'
config('nested.tree') => value

Default values are similarly easy.

config('foo.missing', 42) => 42

Finally, while Tarot will read your current application environment’s config, if you want to reach into another environment, that’s likewise easy:

config('foo', nil, 'production') => 'baz'

As of 0.1.2, Tarot also supports method_missing invocation:

Config = Tarot::Config.new('settings.yml', Rails.env)
Config.foo.bar.baz => "bin"

It also supports default values:

# Assuming foo has no subkey bar
Config.foo.bar("default") => "default"

But it’ll fail if you try to invoke method_missing on a non-leaf node

# Assuming that there is no `blaze` tree
Config.blaze.blarg => NameError

That’s about all there is to it – config isn’t (or shouldn’t be) a hard problem, so there’s not a whole lot to it, but it should get you up and running with easily-configured Rails apps in seconds.