as_psgi_app becomes to_psgi_app, factor dispatcher sugar out into Web::Dispatch:...
[catagits/Web-Simple.git] / lib / Web / Dispatch / Wrapper.pm
1 package Web::Dispatch::Wrapper;
2
3 use strictures 1;
4 use Exporter 'import';
5
6 our @EXPORT_OK = qw(dispatch_wrapper redispatch_to response_filter);
7
8 sub dispatch_wrapper (&) {
9   my ($class, $code) = @_;
10   bless(\$code, $class);
11 }
12
13 sub redispatch_to {
14   my ($class, $new_path) = @_;
15   $class->from_code(sub {
16     $_[1]->({ %{$_[0]}, PATH_INFO => $new_path });
17   });
18 }
19
20 sub response_filter (&) {
21   my ($class, $code) = @_;
22   $class->from_code(sub {
23     my @result = $_[1]->($_[0]);
24     if (@result) {
25       $code->(@result);
26     } else {
27       ()
28     }
29   });
30 }
31
32 sub wrap {
33   my $code = ${$_[0]};
34   my $app = $_[1];
35   sub { $code->($_[0], $app) }
36 }
37
38 1;