as_psgi_app becomes to_psgi_app, factor dispatcher sugar out into Web::Dispatch:...
[catagits/Web-Simple.git] / lib / Web / Dispatch / Wrapper.pm
CommitLineData
4ed4fb42 1package Web::Dispatch::Wrapper;
2
3use strictures 1;
445b3ea0 4use Exporter 'import';
4ed4fb42 5
445b3ea0 6our @EXPORT_OK = qw(dispatch_wrapper redispatch_to response_filter);
7
8sub dispatch_wrapper (&) {
4ed4fb42 9 my ($class, $code) = @_;
10 bless(\$code, $class);
11}
12
445b3ea0 13sub redispatch_to {
14 my ($class, $new_path) = @_;
15 $class->from_code(sub {
16 $_[1]->({ %{$_[0]}, PATH_INFO => $new_path });
17 });
18}
19
20sub 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
4ed4fb42 32sub wrap {
33 my $code = ${$_[0]};
34 my $app = $_[1];
35 sub { $code->($_[0], $app) }
36}
37
381;