Implement Dispatcher#emit

This commit is contained in:
Thibault Jouan 2015-04-08 23:37:02 +00:00
parent ca8f60365f
commit 7becda9f31
2 changed files with 24 additions and 0 deletions

View File

@ -16,6 +16,10 @@ module Uh
@hooks[translate_key key] << block
end
def emit *key
@hooks[translate_key key].tap { |o| o.each { |e| e.call } if o }
end
private

View File

@ -39,6 +39,26 @@ module Uh
expect(dispatcher.hooks[%i[hook key]]).to be
end
end
describe '#emit' do
it 'calls hooks registered for given key' do
dispatcher.on(:hook_key) { throw :hook_code }
expect { dispatcher.emit :hook_key }.to throw_symbol :hook_code
end
context 'when no hooks are registered for given key' do
it 'does not call another hook' do
dispatcher.on(:hook_key) { throw :hook_code }
expect { dispatcher.emit :other_hook_key }.not_to throw_symbol
end
end
context 'when no hooks are registered at all' do
it 'does not raise any error' do
expect { dispatcher.emit :hook_key }.not_to raise_error
end
end
end
end
end
end