Skip to content
A super cool, simple, and feature rich configuration system for Ruby apps.
Pull request Compare This branch is 195 commits behind markbates:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Configatron makes configuring your applications and scripts incredibly easy. No longer is a there a need to use constants or global variables. Now you can use a simple and painless system to configure your life. And, because it’s all Ruby, you can do any crazy thing you would like to!


Installation of Configatron is easy, as it is just a RubyGem:

  $ sudo gem install configatron

If you’d like to live on the bleedin’ edge you can install the development version from GitHub:

  $ sudo gem install markbates-configatron --source=

Once installed you just need to require it:

  require 'configatron'


Simple = ''
  configatron.database_url = "postgres://localhost/mack_framework_rocks"

Now, anywhere in your code you can do the following: # => ""
  configatron.database_url # => "postgres://localhost/mack_framework_rocks"

Viola! Simple as can be.

Now you’re saying, what if I want to have a ‘default’ set of options, but then override them later, based on other information? Simple again. Let’s use our above example. We’ve configured our database_url option to be postgres://localhost/mack_framework_rocks. The problem with that is that is our production database url, not our development url. Fair enough, all you have to do is redeclare it:

  configatron.database_url = "postgres://localhost/mack_framework_rocks_development"

becomes: # => ""
  configatron.database_url # => "postgres://localhost/mack_framework_rocks_development"

Notice how our other configuration parameters haven’t changed? Cool, eh?


You can configure configatron from a hash as well:

  configatron.configure_from_hash({:email => {:pop => {:address => '', :port => 110}}, :smtp => {:address => ''}}) # => '' # => 110
  # and so on...

Notice how they’re all namespaced for your as well. The same holds true for YAML files:



The question that should be on your lips is what I need to have namespaced configuration parameters. It’s easy! Configatron allows you to create namespaces.

  configatron.website_url = "" = "" = 110 = "" = 25

becomes: # => "" # => ""
  configatron.website_url # => ""

Configatron allows you to nest namespaces to your hearts content! Just keep going, it’s that easy.

Of course you can update a single parameter n levels deep as well: = "" # => "" # => ""

Temp Configurations

Sometimes in testing, or other situations, you want to temporarily change some settings. You can do this with the temp method: = 1
  configatron.letters.a = 'A'
  configatron.letters.b = 'B'
  configatron.temp do
    configatron.letters.b = 'bb'
    configatron.letters.c = 'c' # => 1
    configatron.letters.a # => 'A'
    configatron.letters.b # => 'bb'
    configatron.letters.c # => 'c'
  end # => 1
  configatron.letters.a # => 'A'
  configatron.letters.b # => 'B'
  configatron.letters.c # => nil

You can also pass in an optional Hash to the temp: = 1
  configatron.letters.a = 'A'
  configatron.letters.b = 'B'
  configatron.temp(:letters => {:b => 'bb', :c => 'c'}) do == 1
    configatron.letters.a # => 'A'
    configatron.letters.b # => 'bb'
    configatron.letters.c # => 'c'
  end == 1
  configatron.letters.a # => 'A'
  configatron.letters.b # => 'B'
  configatron.letters.c # => nil

Delayed and Dynamic Configurations

There are times when you want to refer to one configuration setting in another configuration setting. Let’s look at a fairly contrived example:

  configatron.memcached.servers = ['']
  configatron.page_caching.servers = configatron.memcached.servers
  configatron.object_caching.servers = configatron.memcached.servers

  if Rails.env == 'production'
    configatron.memcached.servers = ['']
    configatron.page_caching.servers = configatron.memcached.servers
    configatron.object_caching.servers = configatron.memcached.servers
  elsif Rails.env == 'staging'
    configatron.memcached.servers = ['']
    configatron.page_caching.servers = configatron.memcached.servers
    configatron.object_caching.servers = configatron.memcached.servers

Now, we could’ve written that slightly differently, but it helps to illustrate the point. With Configatron you can create Delayed and Dynamic settings.


With Delayed settings execution of the setting doesn’t happen until the first time it is executed.

  configatron.memcached.servers = ['']
  configatron.page_caching.servers = {configatron.memcached.servers}
  configatron.object_caching.servers = {configatron.memcached.servers}

  if Rails.env == 'production'
    configatron.memcached.servers = ['']
  elsif Rails.env == 'staging'
    configatron.memcached.servers = ['']

Execution occurs once and after that the result of that execution is returned. So in our case the first time someone calls the setting configatron.page_caching.servers it will find the configatron.memcached.servers setting and return that. After that point if the configatron.memcached.servers setting is changed, the original settings are returned by configatron.page_caching.servers.


Dynamic settings are very similar to Delayed settings, but with one big difference. Every time you call a Dynamic setting is executed. Take this example:

  configatron.current.time = {}

Each time you call configatron.current.time it will return a new value to you. While this seems a bit useless, it is pretty useful if you have ever changing configurations.


Even if parameters haven’t been set, you can still call them, but you’ll get a Configatron::Store object back. The Configatron::Store class, however, will respond true to .nil? if there are no parameters configured on it.

  configatron.i.dont.exist.nil? # => true
  configatron.i.dont.exist # => Configatron::Store

If you want to get back an actual nil then you can use the retrieve method: = [:some, :array]
  configatron.i.dont.retrieve(:exist, nil) # => nil, :foo) # => [:some, :array]

You can set ‘default’ values for parameters. If there is already a setting, it won’t be replaced. This is useful if you’ve already done your ‘configuration’ and you call a library, that needs to have parameters set. The library can set its defaults, without worrying that it might have overridden your custom settings.

  configatron.set_default(:name, 'Mark Bates') # => 'Mark Bates'
  configatron.set_default(:name, 'Me') # => 'Mark Bates'



Please mail bugs, suggestions and patches to

On the web at:

Something went wrong with that request. Please try again.