From: Robert Klemme Date: 2010-03-22T03:35:11+09:00 Subject: Re: A plugin system using extend On 03/21/2010 06:14 PM, James Edward Gray II wrote: > On Mar 21, 2010, at 2:13 AM, Jean-denis Vauguet wrote: > >> Another idea I had is the following: >> - any loaded lugin registers as a "callback" for the classes it wants to >> alter (instances of the classes, actually!); >> - through the Plugins module, which is mix-ined within the Base module >> using :extend, have any class nested within the base module to be able >> (forced?), everytime they're initialized, to have their instances >> :extend the plugins which have registered as callback for the class. >> >> Not really sure about it, but... >> Theoretically it would allow for per-instance :extend, thus overriding >> behavior of any instance without the need for aliasing. Same behavior as >> my standalone and Josh codes, but same flexibility as my previous gist >> attempted to achieve. >> >> I'll give it a try when I get the time to, unless I'm told this is BS ;) > > If I understand what you are saying correctly, I've used this exact strategy in the past. > > I allowed plugins to register themselves on the class object (you can also do that automatically as they are defined with an inherited hook), and then I just extended all instances with the registered plugins as they were created. > > It worked great and felt very natural to me when using it. James, what's the advantage of this over simply using "include"? If all instances get to use the plugin module then you can as well include it in the class. I see it like this: if all instances of a class should be extended with plugin behavior, then simply use "include". If only some instances should, then use "extend". If plugins also need to be removed at runtime, then a more complicated solution is necessary (e.g. cooking your own version of "extend" and using method_missing to delegate to plugged in code - access to instance variables can then be tricky but you can cope by explicitly passing self to each method). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/