uh-wm/lib/uh/wm/dispatcher.rb
2015-04-09 00:30:13 +00:00

32 lines
539 B
Ruby

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
def emit *key
@hooks[translate_key key].tap { |o| o.each { |e| e.call } if o }
end
private
def translate_key key
key.one? ? key[0] : key
end
end
end
end