diff --git a/lib/uh/wm/dispatcher.rb b/lib/uh/wm/dispatcher.rb index 697e5af..d8ada3c 100644 --- a/lib/uh/wm/dispatcher.rb +++ b/lib/uh/wm/dispatcher.rb @@ -3,21 +3,20 @@ module Uh class Dispatcher attr_reader :hooks - def initialize hooks = Hash.new - @hooks = hooks + def initialize + @hooks = Hash.new { |h, k| h[k] = [] } end def [] *key - @hooks[translate_key key] or [] + @hooks[translate_key key] 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 } + @hooks[translate_key key].each { |e| e.call } end diff --git a/spec/uh/wm/dispatcher_spec.rb b/spec/uh/wm/dispatcher_spec.rb index bd06d11..3399351 100644 --- a/spec/uh/wm/dispatcher_spec.rb +++ b/spec/uh/wm/dispatcher_spec.rb @@ -1,12 +1,11 @@ module Uh module WM RSpec.describe Dispatcher do - let(:hooks) { {} } - subject(:dispatcher) { described_class.new hooks } + subject(:dispatcher) { described_class.new } describe '#[]' do context 'when given key for existing hook' do - let(:hooks) { { hook_key: [:hook] } } + before { dispatcher.hooks[:hook_key] = [:hook] } it 'returns registered hooks for this key' do expect(dispatcher[:hook_key]).to eq [:hook] @@ -14,7 +13,7 @@ module Uh end context 'when given multiple keys for existing hook' do - let(:hooks) { { %i[hook key] => [:hook] } } + before { dispatcher.hooks[%i[hook key]] = [:hook] } it 'returns registered hooks for this key' do expect(dispatcher[:hook, :key]).to eq [:hook]