Sha256: fbe7fc753178499a3942d47b6e1519bbbd9d9987a2571ba0161b600f8d23e23d

Contents?: true

Size: 1.24 KB

Versions: 10

Compression:

Stored size: 1.24 KB

Contents

module CyberarmEngine
  module Event # Gets included into Element
    def subscribe(event, method = nil, &block)
      handler = method || block
      @event_handler[event] << handler

      Subscription.new(self, event, handler)
    end

    def unsubscribe(subscription)
    end

    def publish(event, *args)
      raise ArgumentError, "#{self.class} does not handle #{event.inspect}" unless @event_handler.include?(event)

      return unless enabled?

      was_handled = false

      was_handled = true if respond_to?(event) && (send(event, self, *args) == :handled)

      @event_handler[event].reverse_each do |handler|
        if handler.call(self, *args) == :handled
          was_handled = true
          break
        end
      end

      return :handled if was_handled

      parent.publish(event, *args) if parent
      nil
    end

    def event(event)
      @event_handler ||= {}
      @event_handler[event] ||= []
    end
  end

  class Subscription
    attr_reader :publisher, :event, :handler

    def initialize(publisher, event, handler)
      @publisher = publisher
      @event = event
      @handler = handler
    end

    def unsubscribe
      @publisher.unsubscribe(self)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cyberarm_engine-0.24.4 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.24.3 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.24.2 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.24.1 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.24.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.23.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.22.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.21.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.20.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.19.1 lib/cyberarm_engine/ui/event.rb