From: Benjohn Barnes Date: 2006-03-24T07:59:14+09:00 Subject: Re: A long call chain... On 21 Mar 2006, at 07:35, Eric Hodel wrote: > On Mar 20, 2006, at 10:48 PM, Benjohn Barnes wrote: > >> Hello everyone. I'm hoping to do something like this (and then >> automate it up by putting something neat in to a mix in module)... >> >> class Test >> def xxx; [1]; end >> alias xxx_old xxx >> def xxx; xxx_old + [2]; end >> alias xxx_old xxx >> def xxx; xxx_old + [3]; end >> alias xxx_old xxx >> ... >> end > > Yuck. :) Yeah, sorry about that. *snip* >> I'd like to use a mechanism like this to enable me to have a kind >> of hookable function in a class. As I said, the alias process >> would be encapsulated by waving my hands and working that out >> later, but probably making use of method_defined. >> >> The mechanism allows you to build up an event handler: you send an >> event to a class instance, and any receivers that are registered >> for that event (by having defined the method to pick it up) will >> get it. Example hookable methods for my system will be: >> is_being_setup, will_be_torn_down, has_recieved_message >> will_send_message. > > observer.rb provides something similar to what you want, event > notification. *nods* It's extremely close to the observer pattern. I wanted to build a very modular, and very easy to extend application. I was trying to use Ruby's open classes to pull lots of extra things in to a class. What I noticed was that generally, the observers of a message sent by an object x, were in fact other parts of the object x. I was hoping to make use of this fact to avoid needing to subscribe things to a broadcaster. However, I'm pretty sure now that I was going in a bad direction, and instead, these bolt on / plug in features should be a system of collaborating objects, rather than a monolithic whole. This will also make subsystems much more testable too, I think. What I'd like though, is to avoid needing to unnecessarily describe how things should get stitched together. Anyway - I've just seen mention of Needle in the ruby quiz summary. It sounds distinctly close to the kind of things I'm thinking about, and the gem's just finished pulling itself down, so I'll go and have a look. Cheers, Benjohn