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 {
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;
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;
# 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 {
+++ /dev/null
-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;
app_is_non_plack();
app_is_object();
+app_is_just_sub();
plack_app_return();
broken_route_def();
invalid_psgi_responses();
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 {
{