AppConfig

Rails framework default configuration way is, as we all know, flexible and simple at the same time. Troubles begins when Your application starts to grow - some form of app specific configuration is then required to keep all as DRY as possible.

AppConfig plugin provides a convenient way for configuration application wide settings. Extra custom files in config/ or other unusual configuration hacks are no longer needed. AppConfig extends Rails configuration process in a way that allows to setup application specific options within default configuration block.

Installation

It’s been posted on the Rails Plugin wiki page, so theoretically You could install it with:


$ script/plugin discover
$ script/plugin install app_config

Otherwise You can install directly from the repository: http://svn.jarmark.org/rails/app_config

$ script/plugin install http://svn.jarmark.org/rails/app_config

NOTE:because AppConfig modifies Rails configuration internals, must be included before Rails::Initializer#run. If You are using Rails v1.1+ this step is taken automatically during installation. Otherwise add the following line to conf/environment.rb file:
require 'plugins/app_config/lib/configuration'
(somewhere between line require File.join(File.dirname(__FILE__), 'boot') and Rails::Initializer.run)

Usage

You can specify app-wide settings in configuration file(s) using ’app_config’ prefix e.g. environment.rb:

RUBY:
  1. Rails::Initializer.run do |config|
  2.     ...
  3.        # --- My killer-app specific settings:
  4.        config.app_config.my_key = 'my value'
  5.        config.app_config.boot_time = Time.now
  6.        config.app_config.default_mail_subject = '[appconfig] '
  7.        config.app_config.my_hash = {:a=>1, :b=>2}
  8.     ...

but also in e.g. environment/development.rb

RUBY:
  1. config.app_config.default_message="I'm now in development mode"

Access to previously configured settings from within application code is also dead easy:

RUBY:
  1. AppConfig.my_key
  2. AppConfig[:my_key]
  3. AppConfig['my_key']
  4. AppConfig.param('my_key')
  5. AppConfig.param(:my_key)
  6. AppConfig.param('my_key', 'value if my_key is nil')
  7. AppConfig.param(:my_key) do
  8.  ... #executed when my_key is nil
  9. end

Happy (re)configurations!

Author:: Daniel Owsianski, (daniel-at-jarmark-d0t-org)
Version:: 0.1
Home page:: http://jarmark.org/pages/app_config
License:: Copyright (c) 2006 Daniel Owsiański.
Released under MIT-LICENCE (included).

[]
Spodobało się? Podziel się z innymi: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Wykop
  • Gwar
  • Digg
  • Technorati