rpheath / acts_as_configurable

Rails plugin: supports dynamic configuration for any class or model (does not depend on ActiveRecord).

This URL has Read+Write access

acts_as_configurable / README.textile
100644 57 lines (42 sloc) 1.335 kb

ActsAsConfigurable

Rails plugin that adds dynamic (non-database) configuration to any ActiveRecord model.

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}

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