Sha256: 237e7b810f3ea382176d1b02fbb658a4e708b0566f78416f910ae5bdcc76409a

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Onfire
  # An event carries a type, source, and optional payload. Calling #bubble! it starts at 
  # its source and traverses up the hierarchy using #parent. 
  # On every node it calls #handlers_for_event on the node while calling returned procs.
  class Event
    
    attr_accessor :type, :source, :data
    
    def initialize(type, source, data=nil)
      @type       = type
      @source     = source
      @data       = data
    end
    
    def stopped?
      @stopped ||= false
    end
    
    # Stop event bubbling.
    def stop!
      @stopped = true
    end
    
    module ProcessingMethods
      def bubble!
        node = source
        # in a real visitor pattern, the visited would call #process_node.
        begin process_node(node) end while node = node.parent
      end
      
      def process_node(node)
        node.handlers_for_event(self).each do |proc|
          return if stopped?
          call_handler(proc, node)
        end
      end
      
      def call_handler(proc, node)
        proc.call(self)
      end
    end
    
    include ProcessingMethods
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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