From: Christian Walde Date: Fri, 22 Nov 2013 15:09:55 +0000 (+0100) Subject: allow dispatching to a method name as a string instead of a sub X-Git-Tag: v0.021~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Simple.git;a=commitdiff_plain;h=b2e37c62aa27ca0459e4863f1dcf502769db0786 allow dispatching to a method name as a string instead of a sub --- diff --git a/lib/Web/Dispatch.pm b/lib/Web/Dispatch.pm index 268781f..2a5a835 100644 --- a/lib/Web/Dispatch.pm +++ b/lib/Web/Dispatch.pm @@ -126,7 +126,11 @@ sub _to_try { } else { $try } - } elsif (!ref($try) and ref($more->[0]) eq 'CODE') { + } elsif (!ref($try) + and (ref($more->[0]) eq 'CODE' + or (!ref($more->[0]) and $self->dispatch_object + and $self->dispatch_object->can($more->[0]))) + ) { $self->_construct_node(match => $try, run => shift(@$more)); } elsif ( (blessed($try) && $try->isa('Web::Dispatch::Matcher')) diff --git a/t/dispatch_misc.t b/t/dispatch_misc.t index 517a3e0..05bdd67 100644 --- a/t/dispatch_misc.t +++ b/t/dispatch_misc.t @@ -16,11 +16,13 @@ my @dispatch; package MiscTest; sub dispatch_request { @dispatch } + sub string_method { [ 999, [], [""] ]; } } my $app = MiscTest->new; sub run_request { $app->run_test_request( @_ ); } +string_method_name(); app_is_non_plack(); app_is_object(); app_is_just_sub(); @@ -34,6 +36,14 @@ matcher_nonsub_pair(); done_testing(); +sub string_method_name { + @dispatch = ( '/' => "string_method" ); + + my $get = run_request( GET => 'http://localhost/' ); + + cmp_ok $get->code, '==', 999, "a dispatcher that's a string matching a method on the dispatch object gets executed"; +} + sub app_is_non_plack { my $r = HTTP::Response->new( 999 );