fix for response_filter regression and matching test
[catagits/Web-Simple.git] / lib / Web / Dispatch / Wrapper.pm
index f60a435..c838261 100644 (file)
@@ -1,25 +1,35 @@
 package Web::Dispatch::Wrapper;
 
 use strictures 1;
+use Moo;
 use Exporter 'import';
 
-our @EXPORT_OK = qw(dispatch_wrapper redispatch_to response_filter);
+our @EXPORT = qw(dispatch_wrapper redispatch_to response_filter);
+
+extends 'Plack::Middleware';
+
+has 'wrapper' => (is => 'ro', required => 1);
 
 sub dispatch_wrapper (&) {
+  my ($code) = @_;
+  __PACKAGE__->from_code($code);
+}
+
+sub from_code {
   my ($class, $code) = @_;
-  bless(\$code, $class);
+  $class->new(wrapper => $code);
 }
 
 sub redispatch_to {
-  my ($class, $new_path) = @_;
-  $class->from_code(sub {
+  my ($new_path) = @_;
+  __PACKAGE__->from_code(sub {
     $_[1]->({ %{$_[0]}, PATH_INFO => $new_path });
   });
 }
 
 sub response_filter (&) {
-  my ($class, $code) = @_;
-  $class->from_code(sub {
+  my ($code) = @_;
+  __PACKAGE__->from_code(sub {
     my @result = $_[1]->($_[0]);
     if (@result) {
       $code->(@result);
@@ -29,9 +39,9 @@ sub response_filter (&) {
   });
 }
 
-sub wrap {
-  my $code = ${$_[0]};
-  my $app = $_[1];
+sub to_app {
+  my $code = $_[0]->wrapper;
+  my $app = $_[0]->app;
   sub { $code->($_[0], $app) }
 }