allow dispatching to a method name as a string instead of a sub
Christian Walde [Fri, 22 Nov 2013 15:09:55 +0000 (16:09 +0100)]
lib/Web/Dispatch.pm
t/dispatch_misc.t

index 268781f..2a5a835 100644 (file)
@@ -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'))
index 517a3e0..05bdd67 100644 (file)
@@ -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 );