handle strings as well as prototypes
Matt S Trout [Wed, 15 Dec 2010 06:54:29 +0000 (06:54 +0000)]
Makefile.PL
lib/Web/Dispatch.pm
lib/Web/Dispatch/Parser.pm
t/sub-dispatch-args.t

index 0a801e5..bd9fc89 100644 (file)
@@ -7,5 +7,6 @@ requires 'Syntax::Keyword::Gather';
 requires 'Plack';
 requires 'Moo';
 requires 'warnings::illegalproto';
+requires 'Data::Dumper::Concise';
 
 WriteAll;
index 01ebb3e..581cc04 100644 (file)
@@ -37,7 +37,7 @@ sub _dispatch {
     } elsif (ref($try) eq 'ARRAY') {
       return $try;
     }
-    my @result = $self->_to_try($try)->($env, @match);
+    my @result = $self->_to_try($try, \@match)->($env, @match);
     next unless @result and defined($result[0]);
     if (ref($result[0]) eq 'ARRAY') {
       return $result[0];
@@ -62,7 +62,7 @@ sub _dispatch {
 }
 
 sub _to_try {
-  my ($self, $try) = @_;
+  my ($self, $try, $more) = @_;
   if (ref($try) eq 'CODE') {
     if (defined(my $proto = prototype($try))) {
       $self->_construct_node(
@@ -71,6 +71,10 @@ sub _to_try {
     } else {
       $try
     }
+  } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
+    $self->_construct_node(
+      match => $self->_parser->parse($try), run => shift(@$more)
+    )->to_app;
   } elsif (blessed($try) && $try->can('to_app')) {
     $try->to_app;
   } else {
@@ -80,8 +84,7 @@ sub _to_try {
 
 sub _construct_node {
   my ($self, %args) = @_;
-  @args{keys %$_} = values %$_ for $self->node_args;
-  $self->node_class->new(\%args);
+  $self->node_class->new({ %{$self->node_args}, %args });
 }
 
 1;
index b7d604a..9d02af1 100644 (file)
@@ -29,6 +29,7 @@ ${hat} here\n";
 
 sub parse {
   my ($self, $spec) = @_;
+  $spec =~ s/\s+//g; # whitespace is not valid
   return $self->_cache->{$spec} ||= $self->_parse_spec($spec);
 }
 
@@ -36,7 +37,6 @@ sub _parse_spec {
   my ($self, $spec, $nested) = @_;
   for ($_[1]) {
     my @match;
-    /^\G\s*/; # eat leading whitespace
     PARSE: { do {
       push @match, $self->_parse_spec_section($_)
         or $self->_blam("Unable to work out what the next section is");
index 8762459..c4b6530 100644 (file)
@@ -1,7 +1,7 @@
 use strict;
 use warnings FATAL => 'all';
 
-use Data::Dump qw(dump);
+use Data::Dumper::Concise;
 use Test::More (
   eval { require HTTP::Request::AsCGI }
     ? 'no_plan'
@@ -20,7 +20,7 @@ use Test::More (
             $self->show_landing(@_);
         },
         sub(/...) {
-            sub (GET + /user) {
+            q(GET + /user) => sub {
                 $self->show_users(@_);
             },
             sub (/user/*) {
@@ -36,30 +36,34 @@ use Test::More (
 
     sub show_landing {
         my ($self, @args) = @_;
+        local $self->{_dispatcher};
         return [
             200, ['Content-Type' => 'application/perl' ],
-            [Data::Dump::dump @args],
+            [::Dumper \@args],
         ];
     }
     sub show_users {
         my ($self, @args) = @_;
+        local $self->{_dispatcher};
         return [
             200, ['Content-Type' => 'application/perl' ],
-            [Data::Dump::dump @args],
+            [::Dumper \@args],
         ];
     }
     sub show_user {
         my ($self, @args) = @_;
+        local $self->{_dispatcher};
         return [
             200, ['Content-Type' => 'application/perl' ],
-            [Data::Dump::dump @args],
+            [::Dumper \@args],
         ];
     }
     sub process_post {
         my ($self, @args) = @_;
+        local $self->{_dispatcher};
         return [
             200, ['Content-Type' => 'application/perl' ],
-            [Data::Dump::dump @args],
+            [::Dumper \@args],
         ];
     }
 }
@@ -84,7 +88,8 @@ cmp_ok $get_landing->code, '==', 200,
   '200 on GET';
 
 {
-    my ($self, $env, @noextra) = eval $get_landing->content;
+    my ($self, $env, @noextra) = @{eval $get_landing->content};
+    die $@ if $@;
     is scalar(@noextra), 0, 'No extra stuff';
     is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
     is ref($env), 'HASH', 'Got hashref';
@@ -98,7 +103,7 @@ cmp_ok $get_users->code, '==', 200,
   '200 on GET';
 
 {
-    my ($self, $env, @noextra) = eval $get_users->content;
+    my ($self, $env, @noextra) = @{eval $get_users->content};
     is scalar(@noextra), 0, 'No extra stuff';
     is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
     is ref($env), 'HASH', 'Got hashref';
@@ -112,7 +117,7 @@ cmp_ok $get_user->code, '==', 200,
   '200 on GET';
 
 {
-    my ($self, $env, @noextra) = eval $get_user->content;
+    my ($self, $env, @noextra) = @{eval $get_user->content};
     is scalar(@noextra), 0, 'No extra stuff';
     is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
     is ref($env), 'HASH', 'Got hashref';
@@ -126,7 +131,7 @@ cmp_ok $post_user->code, '==', 200,
   '200 on POST';
 
 {
-    my ($self, $params, $env, @noextra) = eval $post_user->content;
+    my ($self, $params, $env, @noextra) = @{eval $post_user->content};
     is scalar(@noextra), 0, 'No extra stuff';
     is ref($self), 't::Web::Simple::SubDispatchArgs', 'got object';
     is ref($params), 'HASH', 'Got POST hashref';