Implement a basic event dispatcher
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
require 'uh'
|
||||
|
||||
require 'uh/wm/cli'
|
||||
require 'uh/wm/dispatcher'
|
||||
require 'uh/wm/env'
|
||||
require 'uh/wm/manager'
|
||||
require 'uh/wm/runner'
|
||||
|
27
lib/uh/wm/dispatcher.rb
Normal file
27
lib/uh/wm/dispatcher.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module Uh
|
||||
module WM
|
||||
class Dispatcher
|
||||
attr_reader :hooks
|
||||
|
||||
def initialize hooks = Hash.new
|
||||
@hooks = hooks
|
||||
end
|
||||
|
||||
def [] *key
|
||||
@hooks[translate_key key] or []
|
||||
end
|
||||
|
||||
def on *key, &block
|
||||
@hooks[translate_key key] ||= []
|
||||
@hooks[translate_key key] << block
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def translate_key key
|
||||
key.one? ? key[0] : key
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user