Sha256: 3186866e88f44a15129baf52a8b7595655ab84a06b8a432f9d968cba7dc11311

Contents?: true

Size: 727 Bytes

Versions: 6

Compression:

Stored size: 727 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[:source_name] || 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

6 entries across 6 versions & 2 rubygems

Version Path
onfire-0.1.2 lib/onfire/event_table.rb
onfire-0.1.1 lib/onfire/event_table.rb
nilclass-onfire-0.1.3.nc lib/onfire/event_table.rb
nilclass-onfire-0.1.2.nc lib/onfire/event_table.rb
nilclass-onfire-0.1.1.nc lib/onfire/event_table.rb
onfire-0.1.0 lib/onfire/event_table.rb