module Test::Unit::Util::Observable
def add_listener(channel_name, listener_key=NOTHING, &listener) # :yields: value
remove_listener("Channel", listener)
listener = add_listener("Channel") { ... }
itself as the listener_key:
returned, making it very easy to use the proc
Whatever is used as the listener_key is
specified, the proc itself is used.
is used to remove the listener later; if none is
channel indicated by channel_name. listener_key
Adds the passed proc as a listener on the
def add_listener(channel_name, listener_key=NOTHING, &listener) # :yields: value unless(block_given?) raise ArgumentError.new("No callback was passed as a listener") end key = listener_key if (listener_key == NOTHING) listener_key = listener key = ProcWrapper.new(listener) end channels[channel_name] ||= {} channels[channel_name][key] = listener return listener_key end
def channels
def channels @channels ||= {} return @channels end
def notify_listeners(channel_name, *arguments)
make sense for an external class to call this
Perhaps this should be private? Would it ever
--
otherwise they are called with no arguments.
specified, it is passed in to the procs,
indicated by channel_name. If value is
Calls all the procs registered on the channel
def notify_listeners(channel_name, *arguments) channel = channels[channel_name] return 0 unless (channel) channel.each_value { |listener| listener.call(*arguments) } return channel.size end
def remove_listener(channel_name, listener_key)
channel_name. Returns the registered proc, or
from the channel indicated by
Removes the listener indicated by listener_key
def remove_listener(channel_name, listener_key) channel = channels[channel_name] return nil unless (channel) key = listener_key if (listener_key.instance_of?(Proc)) key = ProcWrapper.new(listener_key) end return channel.delete(key) end