From: Jan 'jast' Krueger Date: 2005-04-05T04:49:43+09:00 Subject: Re: Wrote "Hookable" mixin, what now? >> I think my approach, though not quite finished yet, is somewhat more >> powerful than that, and also slightly more powerful than AspectR. I >> haven't done a full analysis on Hookable's performance yet but I think >> it should be okay. > > Could you explain in which ways it is more powerful? I see from your > example that one has to inherit from a certain class and that you use > super to invoke original methods. That might not work in all cases, > i.e., when you cannot determine the class of instances created somewhere > else. Wrapping methods with an approach like the one I have demonstrated > works always because you can open a class any time. The syntax shown in the example is one of two possible general approaches. You can either write a class that inherits Hookable::Wrapper with a number of methods and hook all (or a specific) methods into another class, or hook a single Proc into a hookable method; see the [1]RDoc documentation for an example. As for why it's more powerful: single hooks (or all methods in Hookable::Wrapper descendents) can be disabled temporarily, which is probably useful when a hook should only run on sundays (contrived example ;)). Also, when the Proc interface is used, a Proc can be exchanged with another without having to remove the old Proc from the chain and add the new one to the top. In either case, hooks can also be removed. The def method:pre/post approach doesn't allow for this AFAIK. The super in my example doesn't do what you might think it does. In fact, Hookable::Wrapper "catches" the super and calls the next hook in the chain (or the actual hooked method; I said chain because hooks can be nested). I can't currently think of an example where this doesn't work except for the cases documented in [2] at the bottom. I added this "super" construct to allow for the easiest possible syntax within the Hookable::Wrapper hooks; it's not used with the other interface (again, see [1]). [1] [2] -- Best regards, Jan 'jast' Krueger , Aachen, Germany (mails to go through less strict spam checking)