mental / minicomic

A tool for assembling web and print comics.

This URL has Read+Write access

minicomic / lib / minicomic / util / broadcast.rb
100644 50 lines (43 sloc) 0.94 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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