Sha256: 4c4494996fff75115a89750d3ce6452898d19a0ce95e483d304ed653984ff45c

Contents?: true

Size: 720 Bytes

Versions: 1

Compression:

Stored size: 720 Bytes

Contents

module Onfire
  # Keeps all event handlers attached to one object.
  class EventTable
    attr_accessor :source2evt
    
    def initialize
      @source2evt = {}
    end
    
    def add_handler(handler, opts)
      event_type    = opts[:event_type]
      source_name   = opts[:from] || nil
      
      handlers_for(event_type, source_name) << handler
    end
    
    def handlers_for(event_type, source_name=nil)
      evt_types = source2evt[source_name] ||= {}
      evt_types[event_type] ||= []
    end
    
    # Returns all handlers, with :from first, then catch-all.
    def all_handlers_for(event_type, source_name)
      handlers_for(event_type, source_name) + handlers_for(event_type, nil)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
onfire-0.2.0 lib/onfire/event_table.rb