From c51d1525ee569307d1a79ab70d5de01c382fc1f8 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sun, 19 Apr 2015 02:13:26 +0000 Subject: [PATCH] Delegates `layout_*' messages as Layout#handle_* --- features/actions/layout_delegation.feature | 31 ++++++++++++++++++++++ lib/uh/wm/actions_handler.rb | 28 +++++++++++++++++++ spec/uh/wm/actions_handler_spec.rb | 7 +++++ 3 files changed, 66 insertions(+) create mode 100644 features/actions/layout_delegation.feature diff --git a/features/actions/layout_delegation.feature b/features/actions/layout_delegation.feature new file mode 100644 index 0000000..4cc3091 --- /dev/null +++ b/features/actions/layout_delegation.feature @@ -0,0 +1,31 @@ +Feature: `layout_*' action keywords + + Background: + Given a file named layout.rb with: + """ + class Layout + def register _; end + + def handle_some_action arg + puts arg + end + end + """ + + Scenario: delegates messages matching `layout_*' to `layout_handle_*' + Given a run control file with: + """ + key(:f) { layout_some_action :testing_some_action } + """ + And uhwm is running + When I press the alt+f keys + Then the output must contain ":testing_some_action" + + Scenario: logs an error about unimplemented messages + Given a run control file with: + """ + key(:f) { layout_unknown_action } + """ + And uhwm is running + When I press the alt+f keys + Then the output must match /layout.+no.+implem.+handle_unknown_action/i diff --git a/lib/uh/wm/actions_handler.rb b/lib/uh/wm/actions_handler.rb index e90eeb2..5309f67 100644 --- a/lib/uh/wm/actions_handler.rb +++ b/lib/uh/wm/actions_handler.rb @@ -3,6 +3,9 @@ module Uh class ActionsHandler include EnvLogging + extend Forwardable + def_delegator :@env, :layout + def initialize env, events @env, @events = env, events end @@ -30,6 +33,31 @@ module Uh end Process.waitpid pid end + + def method_missing(m, *args, &block) + if respond_to? m + meth = layout_method m + log "#{layout.class.name}##{meth} #{args.inspect}" + begin + layout.send(meth, *args) + rescue NoMethodError + log_error "Layout does not implement `#{meth}'" + end + else + super + end + end + + def respond_to_missing?(m, *) + m.to_s =~ /\Alayout_/ || super + end + + + private + + def layout_method(m) + m.to_s.gsub(/\Alayout_/, 'handle_').to_sym + end end end end diff --git a/spec/uh/wm/actions_handler_spec.rb b/spec/uh/wm/actions_handler_spec.rb index a593c7e..7d74fcd 100644 --- a/spec/uh/wm/actions_handler_spec.rb +++ b/spec/uh/wm/actions_handler_spec.rb @@ -18,6 +18,13 @@ module Uh actions.quit end end + + describe '#layout_*' do + it 'delegates messages to the layout with handle_ prefix' do + expect(env.layout).to receive :handle_screen_sel + actions.layout_screen_sel :succ + end + end end end end