disassemble DispatchNode since Dispatch.pm can now differentiate between dispatch...
Christian Walde [Fri, 22 Nov 2013 16:11:59 +0000 (17:11 +0100)]
lib/Web/Dispatch.pm
lib/Web/Dispatch/Node.pm
lib/Web/Simple/Application.pm
lib/Web/Simple/DispatchNode.pm [deleted file]
t/dispatch_misc.t

index e32aced..268781f 100644 (file)
@@ -21,7 +21,6 @@ has parser_class => (
 has node_class => (
   is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
 );
-has node_args => (is => 'ro', default => quote_sub q{ {} });
 has _parser => (is => 'lazy');
 
 after BUILDARGS => sub {
@@ -144,7 +143,12 @@ sub _to_try {
 sub _construct_node {
   my ($self, %args) = @_;
   $args{match} = $self->_parser->parse($args{match}) if !ref $args{match};
-  $self->node_class->new({ %{$self->node_args}, %args })->to_app;
+  if ( my $obj = $self->dispatch_object) {
+    # if possible, call dispatchers as methods of the app object
+    my $dispatch_sub = $args{run};
+    $args{run} = sub { $obj->$dispatch_sub(@_) };
+  }
+  $self->node_class->new(\%args)->to_app;
 }
 
 1;
index bd83a7c..2e7bf2e 100644 (file)
@@ -20,7 +20,12 @@ sub call {
 sub _curry {
   my ($self, @args) = @_;
   my $run = $self->_run;
-  sub { $run->(@args, $_[0]) };
+  my $code = sub { $run->(@args, $_[0]) };
+  # if the first argument is a hashref, localize %_ to it to permit
+  # use of $_{name} inside the dispatch sub
+  ref($args[0]) eq 'HASH'
+    ? do { my $v = $args[0]; sub { local *_ = $v; &$code } }
+    : $code
 }
 
 1;
index fc72cd0..ea2bda2 100644 (file)
@@ -37,13 +37,12 @@ sub _build__dispatcher {
   # closes back over $self
 
   weaken($self);
-  my $node_args = { app_object => $self };
-  weaken($node_args->{app_object});
-  Web::Dispatch->new(
+  my %dispatch_args = (
     dispatch_app => sub { $self->dispatch_request(@_), $final },
-    node_class => 'Web::Simple::DispatchNode',
-    node_args => $node_args
+    dispatch_object => $self
   );
+  weaken($dispatch_args{dispatch_object});
+  Web::Dispatch->new(%dispatch_args);
 }
 
 sub _build_final_dispatcher {
diff --git a/lib/Web/Simple/DispatchNode.pm b/lib/Web/Simple/DispatchNode.pm
deleted file mode 100644 (file)
index f3b0e56..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-package Web::Simple::DispatchNode;
-
-use Moo;
-
-extends 'Web::Dispatch::Node';
-
-has _app_object => (is => 'ro', init_arg => 'app_object', required => 1);
-
-around _curry => sub {
-  my ($orig, $self) = (shift, shift);
-  # this ensures that the dispatchers get called as methods of the app itself
-  my $code = $self->$orig($self->_app_object, @_);
-  # if the first argument is a hashref, localize %_ to it to permit
-  # use of $_{name} inside the dispatch sub
-  ref($_[0]) eq 'HASH'
-    ? do { my $v = $_[0]; sub { local *_ = $v; &$code } }
-    : $code
-};
-
-1;
index 2924a9d..517a3e0 100644 (file)
@@ -23,6 +23,7 @@ sub run_request { $app->run_test_request( @_ ); }
 
 app_is_non_plack();
 app_is_object();
+app_is_just_sub();
 plack_app_return();
 broken_route_def();
 invalid_psgi_responses();
@@ -59,6 +60,14 @@ sub app_is_object {
     cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method";
 }
 
+sub app_is_just_sub {
+    my $d = Web::Dispatch->new( dispatch_app => sub () { [ 999, [], ["ok"] ] } );
+    my $res = $d->call( {} );
+
+    cmp_ok $res->[0], '==', 999,
+      "Web::Dispatch can dispatch properly, given only an app that's just a sub, with no object involved";
+}
+
 sub plack_app_return {
     {