From: itsme213 Date: 2004-12-16T05:42:15+09:00 Subject: Re: Can I make a new function like attr_accessor? It may be useful to look at this Ruby version of property change connectors http://raa.ruby-lang.org/project/observable/0.3 "Jack, Paul" wrote in message news:F4F2EBDF8C16D5118B6500508BD32B4D02F8E1AA@earth.sfaf.org... > I'm writing a program that has a bunch of components I'd like > to link together using Java-style property change events. > > So I created a mixin module: > > module Emitter > > def addListener(listener) > @listeners.push(listener) > end > > def removeLister(listener) > @listeners.delete(listener) > end > > def emit(sym, oldValue) > @listeners.each { |x| x.propertyChanged(self, sym, oldValue) } > end > > end > > In my component objects, I just make them include Emitter and invoke > "emit" on any mutator: > > class ExampleComponent > > include Emitter > > def foo > @foo > end > > def foo=(x) > old = @foo > @foo = x > emit(:foo, old) > end > > end > > I'd like to automate the creation of emitter properties, though. Just > like I can use attr_accessor to quickly create readable/writeable > attributes, I'd like a new attr_emitter function to quickly create > readable/writeable/emitable attributes. > > But I can't figure out how to do that. :( Is it possible? It would > save me a LOT of redundant typing... > > Thanks all, > > -Paul