From: Christian Walde Date: Fri, 22 Nov 2013 15:59:50 +0000 (+0100) Subject: allow passing either psgi app, or application object to Web::Dispatch X-Git-Tag: v0.021~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Simple.git;a=commitdiff_plain;h=e5250d96fc8320272ab2ddc39b445649f980815f allow passing either psgi app, or application object to Web::Dispatch --- diff --git a/lib/Web/Dispatch.pm b/lib/Web/Dispatch.pm index 5da5854..07537a4 100644 --- a/lib/Web/Dispatch.pm +++ b/lib/Web/Dispatch.pm @@ -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,7 +37,7 @@ sub _build__parser { sub call { my ($self, $env) = @_; - my $res = $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; } diff --git a/lib/Web/Simple/Application.pm b/lib/Web/Simple/Application.pm index 1ec59aa..fc72cd0 100644 --- a/lib/Web/Simple/Application.pm +++ b/lib/Web/Simple/Application.pm @@ -40,7 +40,7 @@ sub _build__dispatcher { my $node_args = { app_object => $self }; weaken($node_args->{app_object}); Web::Dispatch->new( - app => sub { $self->dispatch_request(@_), $final }, + dispatch_app => sub { $self->dispatch_request(@_), $final }, node_class => 'Web::Simple::DispatchNode', node_args => $node_args ); diff --git a/t/dispatch_misc.t b/t/dispatch_misc.t index 7ad4cd8..2924a9d 100644 --- a/t/dispatch_misc.t +++ b/t/dispatch_misc.t @@ -22,6 +22,7 @@ my $app = MiscTest->new; sub run_request { $app->run_test_request( @_ ); } app_is_non_plack(); +app_is_object(); plack_app_return(); broken_route_def(); invalid_psgi_responses(); @@ -36,7 +37,7 @@ sub app_is_non_plack { my $r = HTTP::Response->new( 999 ); - my $d = Web::Dispatch->new( app => $r ); + my $d = Web::Dispatch->new( dispatch_app => $r ); eval { $d->call }; like $@, qr/No idea how we got here with HTTP::Response/, @@ -44,6 +45,20 @@ sub app_is_non_plack { undef $@; } +sub app_is_object { + { + + package ObjectApp; + use Moo; + sub to_app { [ 999, [], ["ok"] ] } + } + + my $d = Web::Dispatch->new( dispatch_object => ObjectApp->new ); + my $res = $d->call; + + cmp_ok $res->[0], '==', 999, "Web::Dispatch can dispatch properly, given only an object with to_app method"; +} + sub plack_app_return { {