refactor _construct_node to reduce code duplication
[catagits/Web-Simple.git] / lib / Web / Dispatch.pm
index 9c5559f..e32aced 100644 (file)
@@ -11,7 +11,10 @@ use Web::Dispatch::Node;
 
 with 'Web::Dispatch::ToApp';
 
-has app => (is => 'ro', required => 1);
+has dispatch_app => (
+  is => 'lazy', builder => sub { shift->dispatch_object->to_app }
+);
+has dispatch_object => (is => 'ro', required => 0);
 has parser_class => (
   is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
 );
@@ -21,6 +24,12 @@ has node_class => (
 has node_args => (is => 'ro', default => quote_sub q{ {} });
 has _parser => (is => 'lazy');
 
+after BUILDARGS => sub {
+  my ( $self, %args ) = @_;
+  die "Either dispatch_app or dispatch_object need to be supplied."
+    if !$args{dispatch_app} and !$args{dispatch_object}
+};
+
 sub _build__parser {
   my ($self) = @_;
   $self->parser_class->new;
@@ -28,16 +37,18 @@ sub _build__parser {
 
 sub call {
   my ($self, $env) = @_;
-  $self->_dispatch($env, $self->app);
+  my $res = $self->_dispatch($env, $self->dispatch_app);
+  return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE';
+  return $res;
 }
 
 sub _dispatch {
   my ($self, $env, @match) = @_;
-  while (my $try = shift @match) {
+  while (defined(my $try = shift @match)) {
 
     return $try if ref($try) eq 'ARRAY';
     if (ref($try) eq 'HASH') {
-      $env = { %$env, %$try };
+      $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try };
       next;
     }
 
@@ -46,7 +57,7 @@ sub _dispatch {
 
     my $first = $result[0];
 
-    if (my $res = $self->_have_result( $first, \@result, \@match, $env )) {
+    if (my $res = $self->_have_result($first, \@result, \@match, $env)) {
 
       return $res;
     }
@@ -60,32 +71,29 @@ sub _dispatch {
 }
 
 sub _have_result {
-  my ( $self, $first, $result, $match, $env ) = @_;
+  my ($self, $first, $result, $match, $env) = @_;
 
-  if ( ref($first) eq 'ARRAY' ) {
-    return $self->_unpack_array_match( $first );
+  if (ref($first) eq 'ARRAY') {
+    return $first;
   }
-  elsif ( blessed($first) && $first->isa('Plack::Middleware') ) {
-    return $self->_uplevel_middleware( $first, $result );
+  elsif (blessed($first) && $first->isa('Plack::Middleware')) {
+    return $self->_uplevel_middleware($first, $result);
   }
-  elsif ( ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY} ) {
-    return $self->_redispatch_with_middleware( $first, $match, $env );
+  elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) {
+    return $self->_redispatch_with_middleware($first, $match, $env);
   }
-  elsif ( blessed($first) && !$first->can('to_app') ) {
+  elsif (
+    blessed($first) &&
+    not($first->can('to_app')) &&
+    not($first->isa('Web::Dispatch::Matcher'))
+  ) {
     return $first;
   }
-
   return;
 }
 
-sub _unpack_array_match {
-  my ( $self, $match ) = @_;
-  return $match->[0] if @{$match} == 1 and ref($match->[0]) eq 'CODE';
-  return $match;
-}
-
 sub _uplevel_middleware {
-  my ( $self, $match, $results ) = @_;
+  my ($self, $match, $results) = @_;
   die "Multiple results but first one is a middleware ($match)"
     if @{$results} > 1;
   # middleware needs to uplevel exactly once to wrap the rest of the
@@ -94,7 +102,7 @@ sub _uplevel_middleware {
 }
 
 sub _redispatch_with_middleware {
-  my ( $self, $first, $match, $env ) = @_;
+  my ($self, $first, $match, $env) = @_;
 
   my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
 
@@ -105,14 +113,27 @@ sub _redispatch_with_middleware {
 
 sub _to_try {
   my ($self, $try, $more) = @_;
+
+  # sub (<spec>) {}      becomes a dispatcher
+  # sub {}               is a PSGI app and can be returned as is
+  # '<spec>' => sub {}   becomes a dispatcher
+  # $obj isa WD:Predicates::Matcher => sub { ... } -  become a dispatcher
+  # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app
+  #
+
   if (ref($try) eq 'CODE') {
     if (defined(my $proto = prototype($try))) {
-      $self->_construct_node( match=> $proto, run => $try )->to_app;
+      $self->_construct_node(match => $proto, run => $try);
     } else {
       $try
     }
   } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
-    $self->_construct_node( match => $try, run => shift(@$more) )->to_app;
+    $self->_construct_node(match => $try, run => shift(@$more));
+  } elsif (
+    (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
+    and (ref($more->[0]) eq 'CODE')
+  ) {
+    $self->_construct_node(match => $try, run => shift(@$more));
   } elsif (blessed($try) && $try->can('to_app')) {
     $try->to_app;
   } else {
@@ -122,8 +143,8 @@ sub _to_try {
 
 sub _construct_node {
   my ($self, %args) = @_;
-  $args{match} = $self->_parser->parse( $args{match} );
-  $self->node_class->new({ %{$self->node_args}, %args });
+  $args{match} = $self->_parser->parse($args{match}) if !ref $args{match};
+  $self->node_class->new({ %{$self->node_args}, %args })->to_app;
 }
 
 1;