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.1.0';
4 168     168   1459135 use Moo;
  168         53210  
  168         2972  
5 168     168   86027 use Dancer2::Core::Types;
  168         442  
  168         2892  
6 168     168   2614559 use Carp;
  168         415  
  168         16346  
7 168     168   2172 use Sub::Util qw/ set_subname subname /;
  168         395  
  168         47385  
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__