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.3521';
5 165     165   1272 use strict;
  165         396  
  165         5073  
6 165     165   936 use warnings;
  165         462  
  165         4180  
7 165     165   876 use Carp;
  165         458  
  165         9782  
8              
9 165     165   1133 use base 'Dancer::Object';
  165         438  
  165         21011  
10              
11             __PACKAGE__->attributes(qw/name code properties/);
12              
13 165     165   1350 use Dancer::Factory::Hook;
  165         484  
  165         4902  
14 165     165   73446 use Dancer::Hook::Properties;
  165         538  
  165         6365  
15 165     165   1192 use Dancer::Exception qw(:all);
  165         446  
  165         70460  
16              
17             sub new {
18 133     133 1 450 my ($class, @args) = @_;
19              
20 133         352 my $self = bless {}, $class;
21              
22 133 50       422 if (!scalar @args) {
23 0         0 raise core_hook => "one name and a coderef are required";
24             }
25              
26 133         288 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       391 $hook_name = 'before_template_render' if $hook_name eq 'before_template';
33              
34 133         565 $self->name($hook_name);
35              
36 133         470 my ( $properties, $code );
37 133 50       365 if ( scalar @args == 1 ) {
    0          
38 133         746 $properties = Dancer::Hook::Properties->new();
39 133         324 $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       511 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   628 my @arguments = @_;
55 299 100       863 return if Dancer::SharedData->response->halted;
56              
57 298         833 my $app = Dancer::App->current();
58 298 50       795 return unless $properties->should_run_this_app($app->name);
59              
60 298         1272 Dancer::Logger::core( "entering " . $hook_name . " hook" );
61              
62 298         957 $code->(@arguments);
63              
64 132         683 };
65              
66 132         730 $self->properties($properties);
67 132         434 $self->code($compiled_filter);
68              
69 132         711 Dancer::Factory::Hook->instance->register_hook($self);
70 132         747 return $self;
71             }
72              
73             1;
74              
75             __END__