Module: Clevic::Emitter::ClassMethods

Defined in:
lib/clevic/emitter.rb

Instance Method Summary collapse

Instance Method Details

#emitter(emitter_name) ⇒ Object



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
# File 'lib/clevic/emitter.rb', line 11

def emitter( emitter_name )
  line, st = __LINE__, "    def \#{emitter_name}_listeners\n      @\#{emitter_name}_listeners ||= Set.new\n    end\n\n    # If msg is provided, yield to stored block.\n    # If block is provided, store it for later.\n    def emit_\#{emitter_name}( *args, &notifier_block )\n      if block_given?\n        \#{emitter_name}_listeners << notifier_block\n      else\n        puts \"emit_\#{emitter_name} called with \" + args.inspect\n        \#{emitter_name}_listeners.each do |notify|\n          notify.call( *args )\n        end\n      end\n    end\n\n    def remove_\#{emitter_name}( &notifier_block )\n      \#{emitter_name}_listeners.delete( notifier_block )\n    end\n  EOF\n  class_eval st, __FILE__, line + 1\nend\n"