From 7becda9f31cac399172058e0ed4daddcb25c768e Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Wed, 8 Apr 2015 23:37:02 +0000 Subject: [PATCH] Implement Dispatcher#emit --- lib/uh/wm/dispatcher.rb | 4 ++++ spec/uh/wm/dispatcher_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/uh/wm/dispatcher.rb b/lib/uh/wm/dispatcher.rb index a7b0298..697e5af 100644 --- a/lib/uh/wm/dispatcher.rb +++ b/lib/uh/wm/dispatcher.rb @@ -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 diff --git a/spec/uh/wm/dispatcher_spec.rb b/spec/uh/wm/dispatcher_spec.rb index 277a02a..bd06d11 100644 --- a/spec/uh/wm/dispatcher_spec.rb +++ b/spec/uh/wm/dispatcher_spec.rb @@ -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