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