Sha256: 351d8fbf6efecf20cf0f2b5dc847c17f4944c9bbaf5578f5f9e5eba746ca445f

Contents?: true

Size: 1.36 KB

Versions: 48

Compression:

Stored size: 1.36 KB

Contents

module FlydataCore
  module Event
    class EventHandlerBase
      EVENT_VERB_ANY = "__ANY__"
      DEFAULT_ORDER = 500

      @@handlers_hash = {}

      def self.handle(event)
        raise "handle method not implemented"
      end

      protected

      def self.register_handler(opts)
        @noun = opts[:noun]
        @verb = opts[:verb] || EVENT_VERB_ANY
        @execution_order = opts[:execution_order] || DEFAULT_ORDER

        handlers = @@handlers_hash[self.key] ||= []
        insert_handler(handlers,  self)
      end

      private

      def self.handlers(noun, verb)
        handlers = (@@handlers_hash[key(noun, verb)] || []).clone

        any_handlers = @@handlers_hash[key(noun,EVENT_VERB_ANY)]
        if any_handlers
          any_handlers.each { |any_handler| self.insert_handler(handlers,any_handler) }
        end
        handlers
      end

      def self.key(noun = nil, verb = nil)
        noun ||= @noun
        verb ||= @verb
        "#{noun}_#{verb}"
      end

      def self.insert_handler(handlers, handler)
        handlers.each_with_index do |h, i|
          if handler.execution_order < h.execution_order
            handlers.insert(i, handler)
            handler = nil
            break
          end
        end
        handlers << handler if handler
      end

      def self.execution_order
        @execution_order
      end
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
flydata-0.8.10.2 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.10.1 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.9.11 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.10 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.9 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.8 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.7 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.6 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.5 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.4 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.3 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.2 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.1 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.8.0 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.7.19 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.7.18 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.7.17 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.7.16 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.7.15 flydata-core/lib/flydata-core/event/event_handler_base.rb
flydata-0.7.14 flydata-core/lib/flydata-core/event/event_handler_base.rb