fix config handling, finish porting bloggery, safer exporting
[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 = qw(dispatch_wrapper redispatch_to response_filter);
7
8 sub dispatch_wrapper (&) {
9   my ($code) = @_;
10   __PACKAGE__->from_code($code);
11 }
12
13 sub from_code {
14   my ($class, $code) = @_;
15   bless(\$code, $class);
16 }
17
18 sub redispatch_to {
19   my ($new_path) = @_;
20   __PACKAGE__->from_code(sub {
21     $_[1]->({ %{$_[0]}, PATH_INFO => $new_path });
22   });
23 }
24
25 sub response_filter (&) {
26   my ($code) = @_;
27   __PACKAGE__->from_code(sub {
28     my @result = $_[1]->($_[0]);
29     if (@result) {
30       $code->(@result);
31     } else {
32       ()
33     }
34   });
35 }
36
37 sub wrap {
38   my $code = ${$_[0]};
39   my $app = $_[1];
40   sub { $code->($_[0], $app) }
41 }
42
43 1;