File Coverage

blib/lib/Dancer/Hook.pm
Criterion Covered Total %
statement 43 48 89.5
branch 9 14 64.2
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 62 72 86.1


line stmt bran cond sub pod time code
1             package Dancer::Hook;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: Class to manipulate hooks with Dancer
4             $Dancer::Hook::VERSION = '1.3520';
5 165     165   1356 use strict;
  165         438  
  165         5165  
6 165     165   958 use warnings;
  165         431  
  165         4173  
7 165     165   1011 use Carp;
  165         425  
  165         9411  
8              
9 165     165   1119 use base 'Dancer::Object';
  165         443  
  165         21978  
10              
11             __PACKAGE__->attributes(qw/name code properties/);
12              
13 165     165   1358 use Dancer::Factory::Hook;
  165         451  
  165         4914  
14 165     165   74005 use Dancer::Hook::Properties;
  165         531  
  165         4725  
15 165     165   1212 use Dancer::Exception qw(:all);
  165         427  
  165         71201  
16              
17             sub new {
18 133     133 1 445 my ($class, @args) = @_;
19              
20 133         388 my $self = bless {}, $class;
21              
22 133 50       418 if (!scalar @args) {
23 0         0 raise core_hook => "one name and a coderef are required";
24             }
25              
26 133         290 my $hook_name = shift @args;
27              
28             # XXX at the moment, we have a filer position named "before_template".
29             # this one is renamed "before_template_render", so we need to alias it.
30             # maybe we need to deprecate 'before_template' to enforce the use
31             # of 'hook before_template_render => sub {}' ?
32 133 100       407 $hook_name = 'before_template_render' if $hook_name eq 'before_template';
33              
34 133         533 $self->name($hook_name);
35              
36 133         463 my ( $properties, $code );
37 133 50       380 if ( scalar @args == 1 ) {
    0          
38 133         767 $properties = Dancer::Hook::Properties->new();
39 133         299 $code = shift @args;
40             }
41             elsif ( scalar @args == 2 ) {
42 0         0 my $prop = shift @args;
43 0         0 $properties = Dancer::Hook::Properties->new(%$prop);
44 0         0 $code = shift @args;
45             }
46             else {
47 0         0 raise core_hook => "something's wrong with parameters passed to Hook constructor";
48             }
49 133 100       504 ref $code eq 'CODE'
50             or raise core_hook => "the code argument passed to hook construction was not a CodeRef. Value was : '$code'";
51              
52              
53             my $compiled_filter = sub {
54 299     299   621 my @arguments = @_;
55 299 100       878 return if Dancer::SharedData->response->halted;
56              
57 298         884 my $app = Dancer::App->current();
58 298 50       722 return unless $properties->should_run_this_app($app->name);
59              
60 298         1257 Dancer::Logger::core( "entering " . $hook_name . " hook" );
61              
62 298         1040 $code->(@arguments);
63              
64 132         682 };
65              
66 132         744 $self->properties($properties);
67 132         435 $self->code($compiled_filter);
68              
69 132         694 Dancer::Factory::Hook->instance->register_hook($self);
70 132         758 return $self;
71             }
72              
73             1;
74              
75             __END__