Configatron: Simple, Persistent Configs for your Ruby App(s)
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.
August 31, 2008 at 1:57 am
So it's a verbose DSL for making a global hash?
Do not want.
August 31, 2008 at 3:14 am
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. :)
August 31, 2008 at 8:22 am
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 ;-)
August 31, 2008 at 10:18 am
Since there's no link to Configatron in the article:
http://github.com/markbates/configatron/
August 31, 2008 at 1:03 pm
You can also find the RDoc at http://configatron.mackframework.com
August 31, 2008 at 4:40 pm
Can't believe I forgot to link it - thanks!
Alex: What's the error? It's meant to be a comment.
August 31, 2008 at 10:27 pm
What he means is that one should use configatron.app_name instead of config.app_name when accessing the configuration ;)
August 31, 2008 at 10:34 pm
Ohh, thanks :)
I think I'm going to write this post off as a calamity from start to finish. Quick, need another one ;-)
September 1, 2008 at 8:09 pm
@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.
September 9, 2008 at 6:27 am
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.