Sha256: 49236c64188ad15cd1f33a1611a1b6e25dd8163bcd51fbcc743f6dc4abbb46f6

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

require 'set'

module Clevic
  # This is a quickie emitter for Swing mostly. Qt has this built in.
  module Emitter
    def self.included( base )
      base.extend( ClassMethods )
    end
    
    module ClassMethods
      def emitter( emitter_name )
        line, st = __LINE__, <<-EOF
          def #{emitter_name}_listeners
            @#{emitter_name}_listeners ||= Set.new
          end
          
          # If msg is provided, yield to stored block.
          # If block is provided, store it for later.
          def emit_#{emitter_name}( *args, &notifier_block )
            if block_given?
              #{emitter_name}_listeners << notifier_block
            else
              puts "emit_#{emitter_name} called with " + args.inspect
              #{emitter_name}_listeners.each do |notify|
                notify.call( *args )
              end
            end
          end
          
          def remove_#{emitter_name}( &notifier_block )
            #{emitter_name}_listeners.delete( notifier_block )
          end
        EOF
        class_eval st, __FILE__, line + 1
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
clevic-0.13.0.b9 lib/clevic/emitter.rb
clevic-0.13.0.b6 lib/clevic/emitter.rb
clevic-0.13.0.b5 lib/clevic/emitter.rb
clevic-0.13.0.b3 lib/clevic/emitter.rb
clevic-0.13.0.b2 lib/clevic/emitter.rb
clevic-0.13.0.b1 lib/clevic/emitter.rb