File Coverage

blib/lib/Dancer2/Handler/AutoPage.pm
Criterion Covered Total %
statement 31 33 93.9
branch 8 10 80.0
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 51 55 92.7


line stmt bran cond sub pod time code
1             package Dancer2::Handler::AutoPage;
2             # ABSTRACT: Class for handling the AutoPage feature
3             $Dancer2::Handler::AutoPage::VERSION = '2.1.0';
4 145     145   315062 use Moo;
  145         10342  
  145         1223  
5 145     145   70415 use Carp 'croak';
  145         401  
  145         11070  
6 145     145   1586 use Dancer2::Core::Types;
  145         449  
  145         1924  
7              
8             with qw<
9             Dancer2::Core::Role::Handler
10             Dancer2::Core::Role::StandardResponses
11             >;
12              
13             sub register {
14 220     220 1 686 my ( $self, $app ) = @_;
15              
16 220 100       4458 return unless $app->config->{auto_page};
17              
18             $app->add_route(
19             method => $_,
20             regexp => $self->regexp,
21             code => $self->code,
22 4         55 ) for $self->methods;
23             }
24              
25             sub code {
26             sub {
27 10     10   25 my $app = shift;
28 10         21 my $prefix = shift;
29              
30 10         224 my $template = $app->template_engine;
31 10 50       111 if ( !defined $template ) {
32 0         0 $app->response->has_passed(1);
33 0         0 return;
34             }
35              
36 10         59 my $page = $app->request->path;
37 10         323 my $layout_dir = $template->layout_dir;
38 10 100       166 if ( $page =~ m{^/\Q$layout_dir\E/} ) {
39 3         78 $app->response->has_passed(1);
40 3         207 return;
41             }
42              
43             # remove leading '/', ensuring paths relative to the view
44 7         41 $page =~ s{^/}{};
45 7         46 my $view_path = $template->view_pathname($page);
46              
47 7 100       308 if ( ! $template->pathname_exists( $view_path ) ) {
48 3         240 $app->response->has_passed(1);
49 3         234 return;
50             }
51              
52 4         202 my $ct = $template->process( $page );
53 4 50       38 return ( $app->request->method eq 'GET' ) ? $ct : '';
54 8     8 1 81 };
55             }
56              
57 8     8 1 51 sub regexp {'/**'}
58              
59 4     4 1 28 sub methods {qw(head get)}
60              
61             1;
62              
63             __END__