blib/lib/Dancer/Factory/Hook.pm

filter: Statementstmt 94.2% 49 / 52 Branchbran 58.3% 7 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 16.6% 1 / 6 totaltotal 84.3% 70 / 83 CC 9 SCAR 9.7
CC
9
Cov
88%
install_hooks
4.8
execute_hooks
4.3
get_hooks_for
3.0
CRAP 2.6
« blib/lib/Dancer/Exception/Base.pm ↑ Summary blib/lib/Dancer/Logger.pm »
1 package Dancer::Factory::Hook;
2 237 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: Singleton class to create Dancer hooks
4 237 $Dancer::Factory::Hook::VERSION = '1.3513';
5 237 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 237 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 237 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 237 ▶use base 'Dancer::Object::Singleton';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 237 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 237 __PACKAGE__->attributes(qw/ hooks registered_hooks/);
13  
14 sub init {
15 235 ▶ my ( $class, $self ) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
16 235 $self->hooks( {} );
17 235 $self->registered_hooks( [] );
18 235 return $self;
19 }
20  
21 sub install_hooks {
22 1346 ▶ my ( $self, @hooks_name ) = @_;
Subroutine install_hooks: called CC=4 SCAR=15.6
Pod undocumented
23  
24 1346 ▶ if ( !scalar @hooks_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @hooks_name) 0 1346 1346
25 0 raise core_factory_hook => "at least one name is required";
26 }
27  
28 1346 foreach my $hook_name (@hooks_name) {
29 3476 ▶ if ( $self->hook_is_registered($hook_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hook_is_registered($hook_name)) 0 3476 3476
30 0 raise core_factory_hook => "$hook_name is already regsitered, please use another name";
31 }
32 3476 $self->_add_hook( $hook_name );
33 }
34 }
35  
36 sub register_hook {
37 127 ▶ my ( $self, $hook ) = @_;
Subroutine register_hook: called CC=1 SCAR=0.0
Pod undocumented
38 127 $self->_add_registered_hook( $hook->name, $hook->code );
39 }
40  
41 sub _add_registered_hook {
42 127 ▶ my ($class, $hook_name, $compiled_filter) = @_;
Subroutine _add_registered_hook: called CC=1 SCAR=0.0
43 127 push @{$class->hooks->{$hook_name}}, $compiled_filter;
44 }
45  
46 sub _add_hook {
47 3476 ▶ my ($self, $hook_name ) = @_;
Subroutine _add_hook: called CC=1 SCAR=0.0
48 3476 push @{$self->registered_hooks}, $hook_name;
49 }
50  
51 sub hook_is_registered {
52 5450 ▶ my ( $self, $hook_name ) = @_;
Subroutine hook_is_registered: called CC=1 SCAR=0.0
Pod undocumented
53 5450 return grep { $_ eq $hook_name } @{$self->registered_hooks};
54 }
55  
56 sub execute_hooks {
57 1974 ▶ my ($self, $hook_name, @args) = @_;
Subroutine execute_hooks: called CC=4 SCAR=14.6
Pod undocumented
58  
59 1974 ▶ raise core_factory_hook => "Can't ask for hooks without a position" unless $hook_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $hook_name 0 1974 1974
60  
61 1974 ▶ if (!$self->hook_is_registered($hook_name)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hook_is_registered($hook_name)) 0 1974 1974
62 0 raise core_factory_hook => "The hook '$hook_name' doesn't exists";
63 }
64  
65 1974 foreach my $h (@{$self->get_hooks_for($hook_name)}) {
66 292 $h->(@args);
67 }
68 }
69  
70 sub get_hooks_for {
71 1976 ▶ my ( $self, $hook_name ) = @_;
Subroutine get_hooks_for: called CC=3 SCAR=11.1
Pod undocumented
72  
73 1976 ▶ raise core_factory_hook => "Can't ask for hooks without a position" unless $hook_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $hook_name 0 1976 1976
74  
75 1976 ▶ $self->hooks->{$hook_name} || [];
Branch: 1 branch
BranchTrueFalseTotal
$self->hooks->{$hook_name} or [] 1780 196 1976
76 }
77  
78  
79 237 1;
80  
81 __END__
« blib/lib/Dancer/Exception/Base.pm ↑ Summary blib/lib/Dancer/Logger.pm »