File Coverage

blib/lib/Dancer2/Core/Hook.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Core::Hook;
2             # ABSTRACT: Manipulate hooks with Dancer2
3             $Dancer2::Core::Hook::VERSION = '2.0.1';
4 164     164   1403211 use Moo;
  164         38734  
  164         1303  
5 164     164   82699 use Dancer2::Core::Types;
  164         481  
  164         2037  
6 164     164   2571541 use Carp;
  164         394  
  164         16006  
7 164     164   1199 use Sub::Util qw/ set_subname subname /;
  164         370  
  164         46118  
8              
9             has name => (
10             is => 'rw',
11             isa => Str,
12             required => 1,
13             coerce => sub {
14             my ($hook_name) = @_;
15              
16             # XXX at the moment, we have a filer position named "before_template".
17             # this one is renamed "before_template_render", so we need to alias it.
18             # maybe we need to deprecate 'before_template' to enforce the use
19             # of 'hook before_template_render => sub {}' ?
20             $hook_name = 'before_template_render'
21             if $hook_name eq 'before_template';
22             return $hook_name;
23             },
24             );
25              
26             has code => (
27             is => 'ro',
28             isa => CodeRef,
29             required => 1,
30             coerce => sub {
31             my ($hook) = @_;
32             set_subname subname($hook) => sub {
33             my $res = eval { $hook->(@_) };
34             croak "Hook error: $@" if $@;
35             return $res;
36             };
37             },
38             );
39              
40             1;
41              
42             __END__