module Minicomic
module Util
class Broadcast
class << self
def broadcast_method(*names)
names.each do |name|
name = name.to_sym
define_method name, &_broadcast_method_body(name)
end
nil
end
private :broadcast_method
def _broadcast_method_body(name) #:nodoc:
proc do |*args|
@recipients.each_key do |recipient|
begin
recipient.send(name, *args)
rescue Exception => exception
if $DEBUG
$stderr.puts "#{exception.class}: #{exception}"
$stderr.puts exception.backtrace
end
end
end
nil
end
end
private :_broadcast_method_body
end
def initialize
@recipients = {}
end
def add_recipient(recipient)
@recipients[recipient] = nil
self
end
def remove_recipient(recipient)
@recipients.delete recipient
self
end
end
end
end