From 7b9854b49962ac4bfa201147c451d9ef7bfd69fd Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Thu, 9 Apr 2015 23:15:10 +0000 Subject: [PATCH] Support event hook arguments in Dispatcher --- lib/uh/wm/dispatcher.rb | 4 ++-- spec/uh/wm/dispatcher_spec.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/uh/wm/dispatcher.rb b/lib/uh/wm/dispatcher.rb index d8ada3c..e49eac2 100644 --- a/lib/uh/wm/dispatcher.rb +++ b/lib/uh/wm/dispatcher.rb @@ -15,8 +15,8 @@ module Uh @hooks[translate_key key] << block end - def emit *key - @hooks[translate_key key].each { |e| e.call } + def emit *key, args: [] + @hooks[translate_key key].each { |e| e.call *args } end diff --git a/spec/uh/wm/dispatcher_spec.rb b/spec/uh/wm/dispatcher_spec.rb index 3399351..15cfc6c 100644 --- a/spec/uh/wm/dispatcher_spec.rb +++ b/spec/uh/wm/dispatcher_spec.rb @@ -57,6 +57,14 @@ module Uh expect { dispatcher.emit :hook_key }.not_to raise_error end end + + context 'when args keyword argument is given' do + it 'calls hooks with given args' do + dispatcher.on(:hook_key) { |arg1, arg2| throw arg2 } + expect { dispatcher.emit :hook_key, args: %i[arg1 arg2] } + .to throw_symbol :arg2 + end + end end end end