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