Sha256: e7d3fbeb6fd989b861a3c837d0704ce2c44b32368cdd628f24295dfdd4616e61

Contents?: true

Size: 1.05 KB

Versions: 19

Compression:

Stored size: 1.05 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?

      if respond_to?(event)
        return :handled if send(event, self, *args) == :handled
      end

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

      return nil
    end

    def event(event)
      @event_handler ||= Hash.new
      @event_handler[event] ||= []
    end
  end

  class Subscription
    attr_reader :publisher, :event, :handler

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

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
cyberarm_engine-0.12.1 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.12.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.11.1 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.11.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.10.2 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.10.1 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.10.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.9.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.8.1 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.8.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.7.1 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.7.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.6.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.5.1 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.5.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.4.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.3.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.2.0 lib/cyberarm_engine/ui/event.rb
cyberarm_engine-0.1.0 lib/cyberarm_engine/ui/event.rb