Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Configatron: Simple, Persistent Configs for your Ruby App(s)

By Peter Cooper / August 31, 2008

Configatron is a new Ruby library that makes it easy to have persistently accessible configuration data available through your Ruby application. It bears some similarities to the Rails pluginSimpleConfig, but being distributed as a gem, is suitable for non-Rails applications. To install: gem install configatron

Once configatron is installed, the following code will get things going:

configatron do |config|
  config.app_name = "My Awesomely Jazzy App"
  config.database_url = "postgres://localhost/somedb"
  # etc...
end

And then you can access these configuration variables throughout your application, like so:

configatron.app_name     # => "My Awesomely Jazzy App"
configatron.database_url # => "postgres://localhost/somedb"

Note that this only gives a very cursory look at what Configatron offers, however, as it has many other features - including namespacing and nested configurations.

Configatron comes from Mark Bates, creator of the Mack Web application framework.

Post supported by thoughtbot training thoughtbot is a five year old web development consultancy, specializing exclusively in Ruby since Rails 1.0. We now provide an Advanced Rails training class, sharing our lessons from the trenches and interactively taking participants through the source and development process of a real-world app, Umbrella Today.

Comments

  1. Jackson says:

    So it's a verbose DSL for making a global hash?

    Do not want.

  2. Mark Bates says:

    Thanks Peter for the great write up. Jackson, it's more than just a DSL for creating a global hash. It provides n-level nesting of namespaced configurations, with the ability to override a single value, n-levels deep as well. it provides the ability to revert the last set of configurations, great for testing, you can reload the configurations, great if you don't want to have to restart your application if something changes, and a bit more. If you don't like it, that's cool. I'm just putting it out there because it makes my day a bit nicer to use it, and I like to share. :)

  3. Alex Zavalny says:

    Hi there! just wanted to say that there is an error in your code.
    This -> config.app_name # => "My Awesomely Jazzy App"
    Should be -> configatron.app_name

    Check it and fix it ;-)

  4. Jamie Flournoy says:

    Since there's no link to Configatron in the article:

    http://github.com/markbates/configatron/

  5. Mark Bates says:

    You can also find the RDoc at http://configatron.mackframework.com

  6. Peter Cooper says:

    Can't believe I forgot to link it - thanks!

    Alex: What's the error? It's meant to be a comment.

  7. Robin Kaarsgaard says:

    What he means is that one should use configatron.app_name instead of config.app_name when accessing the configuration ;)

  8. Peter Cooper says:

    Ohh, thanks :)

    I think I'm going to write this post off as a calamity from start to finish. Quick, need another one ;-)

  9. Brendan says:

    @Mark Awesome work, and more importantly great timing. I've been looking for something just like this. Namespaces!

    @Jackson I hope you took the time to click on the link, because you'll see it's pretty amazing. It's not for everyone, so if you don't need it, just don't use it.

  10. 7rans says:

    Ditch the global. Instead allow

    myconfigatron = Configatron.new do ...

    If you want to use it globally then use:

    $configatron = Configatron.new do ...

    or at the toplevel:

    def configatron
    @configatron ||= Configatron.new
    end

    But I'd encourage you to move past globals. I know it is very convenient, especially for a Web Framework, but in the end you'll be much better off encapsulating. Think DI.

    (NOTE: This is much like an OpenCascade).

    T.

Other Posts to Enjoy

Twitter Mentions