-
Pages
-
Categories
-
Archives
- March 2009
- February 2009
- May 2008
- October 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- October 2006
- September 2006
- April 2006
- January 2006
- December 2005
- July 2005
- July 2004
- June 2004
- March 2003
- February 2003
- January 2003
- June 2002
- April 2002
- March 2002
- October 2001
- July 2001
Ruby on Rails plugin configuration
I work on Ruby on Rails plugins fairly often, and I’m always curious how people make their plugins configurable.
For example, ActiveMessaging uses several config files – some are yaml, some are ruby – all of them get loaded when the plugin gets initialized. ActiveMessaging has more than a bit of configuration, so distinct files for it makes sense. Also, it has less singular values that get set, and more sets of configuration values (e.g. all the values for a message broker connection).
Comatose uses a singleton with attributes that get defined in a block – so like the Rails initializer, you can call this define method in an environment.rb. Again, this makes sense, there are only a few values to fiddle with, and you very often don’t have to mess with them at all.
Not sure if I’ll use it, but I’ve been playing with an idea where you can use constants to override config values using constants. Makes sense for a plugin where you have default values that would rarely, and often individually, be overridden. Nice thing about this is that constants can be defined even before the config class, so if you need them to be set before the plugin is loaded, this provides a way.
Anyway, to make it easier, I wrote a little helper method to define class attributes, set the default value, and check for constants to override the default, all at one time.
class Config
class <
cattr_accessor var_sym
const_name = var_sym.to_s.upcase
value = (Object.constants.include?(const_name)) ? Object.const_get(const_name): default
class_variable_set("@@#{var_sym}".intern, value)
end
end
# SOME_CONFIG_VALUE set as a constant will override the default
set_config_value :some_config_value, "this is a default"
end