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.3514_04'; # TRIAL
5             $Dancer::Hook::VERSION = '1.351404';
6 166     166   1441 use strict;
  166         326  
  166         4415  
7 166     166   743 use warnings;
  166         297  
  166         3598  
8 166     166   724 use Carp;
  166         302  
  166         9812  
9              
10 166     166   1004 use base 'Dancer::Object';
  166         359  
  166         20352  
11              
12             __PACKAGE__->attributes(qw/name code properties/);
13              
14 166     166   1051 use Dancer::Factory::Hook;
  166         362  
  166         3988  
15 166     166   59564 use Dancer::Hook::Properties;
  166         396  
  166         3922  
16 166     166   945 use Dancer::Exception qw(:all);
  166         297  
  166         60005  
17              
18             sub new {
19 133     133 1 352 my ($class, @args) = @_;
20              
21 133         283 my $self = bless {}, $class;
22              
23 133 50       352 if (!scalar @args) {
24 0         0 raise core_hook => "one name and a coderef are required";
25             }
26              
27 133         233 my $hook_name = shift @args;
28              
29             # XXX at the moment, we have a filer position named "before_template".
30             # this one is renamed "before_template_render", so we need to alias it.
31             # maybe we need to deprecate 'before_template' to enforce the use
32             # of 'hook before_template_render => sub {}' ?
33 133 100       317 $hook_name = 'before_template_render' if $hook_name eq 'before_template';
34              
35 133         438 $self->name($hook_name);
36              
37 133         219 my ( $properties, $code );
38 133 50       310 if ( scalar @args == 1 ) {
    0          
39 133         585 $properties = Dancer::Hook::Properties->new();
40 133         243 $code = shift @args;
41             }
42             elsif ( scalar @args == 2 ) {
43 0         0 my $prop = shift @args;
44 0         0 $properties = Dancer::Hook::Properties->new(%$prop);
45 0         0 $code = shift @args;
46             }
47             else {
48 0         0 raise core_hook => "something's wrong with parameters passed to Hook constructor";
49             }
50 133 100       407 ref $code eq 'CODE'
51             or raise core_hook => "the code argument passed to hook construction was not a CodeRef. Value was : '$code'";
52              
53              
54             my $compiled_filter = sub {
55 299     299   539 my @arguments = @_;
56 299 100       700 return if Dancer::SharedData->response->halted;
57              
58 298         704 my $app = Dancer::App->current();
59 298 50       670 return unless $properties->should_run_this_app($app->name);
60              
61 298         1070 Dancer::Logger::core( "entering " . $hook_name . " hook" );
62              
63 298         833 $code->(@arguments);
64              
65 132         607 };
66              
67 132         645 $self->properties($properties);
68 132         343 $self->code($compiled_filter);
69              
70 132         557 Dancer::Factory::Hook->instance->register_hook($self);
71 132         589 return $self;
72             }
73              
74             1;
75              
76             __END__