public
Description: Rails plugin: supports dynamic configuration for any class or model (does not depend on ActiveRecord).
Homepage: http://rpheath.com/posts/357-rails-plugin-acts-as-configurable
Clone URL: git://github.com/rpheath/acts_as_configurable.git
README.textile

ActsAsConfigurable

Rails plugin that adds dynamic (non-database) configuration settings to any model or class.

Examples

Basic usage:


class User < ActiveRecord::Base
  acts_as_configurable
  
  configuration do |config|
    config.passwords.min_length = 6
    config.passwords.max_length = 12  
  end
end

And then to access that configuration, simply call:


$> User.configuration
$> => {:passwords => { :min_length => 6, :max_length => 12 }}

Or just keep going down the chain to get to the specifics:


$> User.configuration.passwords
$> => {:min_length => 6, :max_length => 12}
$> User.configuration.passwords.min_length
$> => 6

Now, granted that a “configuration” class method is common enough that there may be a case
where you have a conflict. Well, you can also configure that part:


class User < ActiveRecord::Base
  acts_as_configurable :with => :settings
  
  settings do |setting|
    setting.passwords.min_length = 6
    setting.passwords.max_length = 12
  end
end

Then just use “settings” in place of “configuration” when accessing the data.


$> User.settings.passwords
$> => {:min_length => 6, :max_length => 12}

Nested configuration

You can also nest configuration settings to keep things grouped nicely.


class User < ActiveRecord::Base
  acts_as_configurable
  
  configuration do |config|
    config.passwords do |p|
      p.min_length = 6
      p.max_length = 12
    end
    config.accounts do |a|
      # ...
    end
  end
end

Dependencies

This plugin mixes directly into Object, so there’s no dependency on another library. While the
examples above are all ActiveRecord models, they don’t have to be. Any class can act as configurable.

Actually, ActiveSupport is required (thanks to class_inheritable_accessor), but since this is a
plugin for a Rails application, there shouldn’t be any issues there.

Credits

Chris Scharf is the original author of this “on-the-fly” configuration.
His original implementation was intended for application wide configuration settings. You can find
his post about it here.

With my version, you can have the convenience of his configuration, but exclusive to the class in
which it’s contained. Plus it’s now a plugin.

© 2008 Ryan Heath and Chris Scharf, released under the MIT license