Delegates `layout_*' messages as Layout#handle_*
This commit is contained in:
parent
d47e7de6d0
commit
c51d1525ee
31
features/actions/layout_delegation.feature
Normal file
31
features/actions/layout_delegation.feature
Normal file
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user