From: Christian Walde Date: Fri, 22 Nov 2013 16:11:59 +0000 (+0100) Subject: disassemble DispatchNode since Dispatch.pm can now differentiate between dispatch... X-Git-Tag: v0.021~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1f8cad5e5a1875de94d63ac91d8ded4d2282c62e;p=catagits%2FWeb-Simple.git disassemble DispatchNode since Dispatch.pm can now differentiate between dispatch app and object --- diff --git a/lib/Web/Dispatch.pm b/lib/Web/Dispatch.pm index e32aced..268781f 100644 --- a/lib/Web/Dispatch.pm +++ b/lib/Web/Dispatch.pm @@ -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; diff --git a/lib/Web/Dispatch/Node.pm b/lib/Web/Dispatch/Node.pm index bd83a7c..2e7bf2e 100644 --- a/lib/Web/Dispatch/Node.pm +++ b/lib/Web/Dispatch/Node.pm @@ -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; diff --git a/lib/Web/Simple/Application.pm b/lib/Web/Simple/Application.pm index fc72cd0..ea2bda2 100644 --- a/lib/Web/Simple/Application.pm +++ b/lib/Web/Simple/Application.pm @@ -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 index f3b0e56..0000000 --- a/lib/Web/Simple/DispatchNode.pm +++ /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; diff --git a/t/dispatch_misc.t b/t/dispatch_misc.t index 2924a9d..517a3e0 100644 --- a/t/dispatch_misc.t +++ b/t/dispatch_misc.t @@ -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 { {