{"id":1186,"date":"2009-12-01T10:28:15","date_gmt":"2009-12-01T09:28:15","guid":{"rendered":"http:\/\/www.devco.net\/?p=1186"},"modified":"2010-08-17T12:28:17","modified_gmt":"2010-08-17T11:28:17","slug":"ruby_plugin_architectures","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2009\/12\/01\/ruby_plugin_architectures.php","title":{"rendered":"Ruby Plugin Architectures"},"content":{"rendered":"
Most of the applications I write in Ruby are some kind of Framework, ruby-pdns<\/a> takes plugins, mcollective<\/a> takes plugins, my nagios notification bot takes plugins etc, yet I have not yet figured out a decent approach to handling plugins.<\/p>\n Google suggests many options, the most suggested one is something along these lines.<\/p>\n <\/code><\/p>\n Where PluginManager is some class or module that stores and later allows retrieval, when the FooPlugin class gets created it will trigger the hook in the base class.<\/p>\n This works ok, almost perfectly, except that at the time of the trigger the FooPlugin class is not 100% complete and your constructor will not be called, quite a pain. From what I can tell it calls the constructor on either Class or Object.<\/p>\n I ended up tweaking the pattern a bit and now have something that works well, essentially if you pass a String to the PluginManager it will just store that as a class name and later create you an instance of that class, else if it's not a string it will save it as a fully realized class assuming that you know what you did.<\/p>\n<\/p>\n
\r\nclass Plugin\r\n def self.inherited(klass)\r\n PluginManager << klass.new\r\n end\r\nend\r\n\r\nclass FooPlugin