File Coverage

blib/lib/Plack/ResponseHelper.pm
Criterion Covered Total %
statement 37 39 94.8
branch 8 12 66.6
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 53 59 89.8


line stmt bran cond sub pod time code
1             package Plack::ResponseHelper;
2 6     6   262172 use strict;
  6         16  
  6         210  
3 6     6   30 use warnings;
  6         12  
  6         191  
4 6     6   31 use Carp;
  6         16  
  6         1847  
5              
6             our $HELPERS = {};
7              
8             sub import {
9 6     6   49 shift;
10 6         36 my %options = @_;
11 6         26 foreach my $type (keys %options) {
12 2         6 my ($helper, $init) = ref $options{$type}
13 5 100       25 ? @{$options{$type}}
14             : ($options{$type}, undef);
15              
16 5         13 $helper =~ s/^\+//;
17 5 50       55 croak "bad helper name $helper" unless $helper =~ /^\w+(?:::\w+)*$/;
18              
19 5         13 my $package = "Plack::ResponseHelper::$helper";
20 5 50       305 eval "require $package" or croak $@;
21              
22 6     6   37 no strict 'refs';
  6         11  
  6         541  
23 5         20 my $code = *{$package.'::helper'}->($init);
  5         32  
24 5         29 $HELPERS->{$type} = $code;
25             }
26              
27 6         26 my $pkg = caller;
28 6     6   28 no strict 'refs';
  6         18  
  6         1345  
29 6         49 *{$pkg.'::respond'} = \&respond;
  6         10104  
30             }
31              
32             sub respond {
33 9     9 1 36393 my $type = shift;
34 9         22 my $r = shift;
35              
36 9 100       105 croak "unknown type '$type'" unless exists $HELPERS->{$type};
37 8         50 $r = $HELPERS->{$type}->($r);
38              
39 7 50       51 if (ref $r =~ /^(?:ARRAY|CODE)$/) {
40 0         0 return $r;
41             }
42 7 50       53 if (UNIVERSAL::isa($r, "Plack::Response")) {
43 7         39 return $r->finalize();
44             }
45              
46 0           croak "bad response";
47             }
48              
49             1;
50             __END__