File Coverage

blib/lib/Dancer/Factory/Hook.pm
Criterion Covered Total %
statement 45 48 93.7
branch 7 12 58.3
condition n/a
subroutine 13 13 100.0
pod 1 6 16.6
total 66 79 83.5


line stmt bran cond sub pod time code
1             package Dancer::Factory::Hook;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             # ABSTRACT: Singleton class to create Dancer hooks
4             $Dancer::Factory::Hook::VERSION = '1.3520';
5 194     194   1612 use strict;
  194         908  
  194         7820  
6 194     194   1207 use warnings;
  194         978  
  194         5154  
7 194     194   1129 use Carp;
  194         11269  
  194         12192  
8              
9 194     194   1342 use base 'Dancer::Object::Singleton';
  194         421  
  194         88065  
10 194     194   1392 use Dancer::Exception qw(:all);
  194         467  
  194         121572  
11              
12             __PACKAGE__->attributes(qw/ hooks registered_hooks/);
13              
14             sub init {
15 194     194 1 525 my ( $class, $self ) = @_;
16 194         817 $self->hooks( {} );
17 194         761 $self->registered_hooks( [] );
18 194         420 return $self;
19             }
20              
21             sub install_hooks {
22 1226     1226 0 4024 my ( $self, @hooks_name ) = @_;
23              
24 1226 50       3986 if ( !scalar @hooks_name ) {
25 0         0 raise core_factory_hook => "at least one name is required";
26             }
27              
28 1226         3400 foreach my $hook_name (@hooks_name) {
29 3153 50       7014 if ( $self->hook_is_registered($hook_name) ) {
30 0         0 raise core_factory_hook => "$hook_name is already regsitered, please use another name";
31             }
32 3153         7389 $self->_add_hook( $hook_name );
33             }
34             }
35              
36             sub register_hook {
37 132     132 0 327 my ( $self, $hook ) = @_;
38 132         381 $self->_add_registered_hook( $hook->name, $hook->code );
39             }
40              
41             sub _add_registered_hook {
42 132     132   373 my ($class, $hook_name, $compiled_filter) = @_;
43 132         221 push @{$class->hooks->{$hook_name}}, $compiled_filter;
  132         399  
44             }
45              
46             sub _add_hook {
47 3153     3153   5931 my ($self, $hook_name ) = @_;
48 3153         4369 push @{$self->registered_hooks}, $hook_name;
  3153         6310  
49             }
50              
51             sub hook_is_registered {
52 5134     5134 0 9354 my ( $self, $hook_name ) = @_;
53 5134         7126 return grep { $_ eq $hook_name } @{$self->registered_hooks};
  61681         103953  
  5134         12231  
54             }
55              
56             sub execute_hooks {
57 1981     1981 0 4750 my ($self, $hook_name, @args) = @_;
58              
59 1981 50       4083 raise core_factory_hook => "Can't ask for hooks without a position" unless $hook_name;
60              
61 1981 50       4449 if (!$self->hook_is_registered($hook_name)){
62 0         0 raise core_factory_hook => "The hook '$hook_name' doesn't exists";
63             }
64              
65 1981         3126 foreach my $h (@{$self->get_hooks_for($hook_name)}) {
  1981         4336  
66 299         1768 $h->(@args);
67             }
68             }
69              
70             sub get_hooks_for {
71 1983     1983 0 3486 my ( $self, $hook_name ) = @_;
72              
73 1983 50       3882 raise core_factory_hook => "Can't ask for hooks without a position" unless $hook_name;
74              
75 1983 100       4454 $self->hooks->{$hook_name} || [];
76             }
77              
78              
79             1;
80              
81             __END__