blib/lib/Dancer2/Core/Role/Hookable.pm

filter: Statementstmt 100% 66 / 66 Branchbran 91.6% 22 / 24 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 5 totaltotal 92.1% 105 / 114 CC 16 SCAR 14.3
CC
16
Cov
97%
execute_hook
8.0
_add_postponed_hooks
6.0
__ANON__
2.0
CRAP 4.2
« blib/lib/Dancer2/Serializer/JSON.pm ↑ Summary blib/lib/Dancer2/Core/Time.pm »
1 package Dancer2::Core::Role::Hookable;
2 # ABSTRACT: Role for hookable objects
3 219 $Dancer2::Core::Role::Hookable::VERSION = '2.2.1';
4 219 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 219 ▶use Dancer2::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 219 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 219 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 219 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 219 ▶use Sub::Util qw/ subname /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 219 requires 'supported_hooks', 'hook_aliases';
12  
13 # The hooks registry
14 219 has hooks => (
15 is => 'ro',
16 isa => HashRef,
17 builder => '_build_hooks',
18 lazy => 1,
19 );
20  
21 ▶sub BUILD { }
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
22  
23 # after a hookable object is built, we go over its postponed hooks and register
24 # them if any.
25 after BUILD => sub {
26 1366 ▶ my ( $self, $args ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
27 $self->_add_postponed_hooks($args)
28 1366 ▶ if defined $args->{postponed_hooks};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"postponed_hooks"} 919 447 1366
29 219 };
30  
31 sub _add_postponed_hooks {
32 919 ▶ my ( $self, $args ) = @_;
Subroutine _add_postponed_hooks: called CC=6 SCAR=17.9
33 919 my $postponed_hooks = $args->{postponed_hooks};
34  
35 # find the internal name of the hooks, from the caller name
36 919 my $caller = ref($self);
37 919 my ( $dancer, $h_type, $h_name, @rest ) = map lc, split /::/, $caller;
38 919 ▶ $h_name = $rest[0] if $h_name eq 'role';
Branch: 1 branch
BranchTrueFalseTotal
if $h_name eq "role" 0 919 919
39 919 ▶ if ( $h_type =~ /(template|logger|serializer|session)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_type =~ /(template|logger|serializer|session)/) 672 247 919
40 672 $h_name = $h_type;
41 672 $h_type = 'engine';
42 }
43  
44 # keep only the hooks we want
45 919 $postponed_hooks = $postponed_hooks->{$h_type}{$h_name};
46 919 ▶ return unless defined $postponed_hooks;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $postponed_hooks 909 10 919
47  
48 10 foreach my $name ( keys %{$postponed_hooks} ) {
49 11 my $hook = $postponed_hooks->{$name}{hook};
50 11 my $caller = $postponed_hooks->{$name}{caller};
51  
52 $self->has_hook($name)
53 or croak "$h_name $h_type does not support the hook `$name'. ("
54 11 ▶ . join( ", ", @{$caller} ) . ")";
Branch: 1 branch
BranchTrueFalseTotal
$self->has_hook($name) or croak("$h_name $h_type does not support the hook `${name}'. (" . join(", ", @{$caller;}) . ')') 1 10 11
55  
56 10 $self->add_hook($hook);
57 }
58 }
59  
60 # mst++ for the hint
61 sub _build_hooks {
62 484 ▶ my ($self) = @_;
Subroutine _build_hooks: called CC=1 SCAR=0.0
63 484 my %hooks = map +( $_ => [] ), $self->supported_hooks;
64 484 return \%hooks;
65 }
66  
67 # This binds a coderef to an installed hook if not already
68 # existing
69 sub add_hook {
70 420 ▶ my ( $self, $hook ) = @_;
Subroutine add_hook: called CC=2 SCAR=6.9
Pod undocumented
71 420 my $name = $hook->name;
72 420 my $code = $hook->code;
73  
74 420 ▶ croak "Unsupported hook '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_hook($name) 1 419 420
75 unless $self->has_hook($name);
76  
77 419 push @{ $self->hooks->{$name} }, $code;
78 }
79  
80 # allows the caller to replace the current list of hooks at the given position
81 # this is useful if the object where this role is composed wants to compile the
82 # hooks.
83 sub replace_hook {
84 2568 ▶ my ( $self, $position, $hooks ) = @_;
Subroutine replace_hook: called CC=2 SCAR=6.9
Pod undocumented
85  
86 2568 ▶ croak "Hook '$position' must be installed first"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_hook($position) 1 2567 2568
87 unless $self->has_hook($position);
88  
89 2567 $self->hooks->{$position} = $hooks;
90 }
91  
92 # Boolean flag to tells if the hook is registered or not
93 sub has_hook {
94 28607 ▶ my ( $self, $hook_name ) = @_;
Subroutine has_hook: called CC=1 SCAR=0.0
Pod undocumented
95 28607 return exists $self->hooks->{$hook_name};
96 }
97  
98 # Execute the hook at the given position
99 sub execute_hook {
100 23037 ▶ my $self = shift;
Subroutine execute_hook: called CC=8 SCAR=20.8
Pod undocumented
101 23037 my $name = shift;
102  
103 23037 ▶ $name and !ref $name
Branch: 1 branch
BranchTrueFalseTotal
$name and not ref $name or croak("execute_hook needs a hook name") 1 23036 23037
Condition66%2/3
$name and not ref $name
!ll&&!rl&&r
1023036
MC/DC50%1/2
$name and not ref $name
$name not ref $name
Truth table66%2/3
A and not(B)
A= $nameB= not ref $name
ABresult
0X0
100
111
104 or croak "execute_hook needs a hook name";
105  
106 $name = $self->hook_aliases->{$name}
107 23036 ▶ if exists $self->hook_aliases->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->hook_aliases->{$name} 3 23033 23036
108  
109 23036 ▶ croak "Hook '$name' does not exist"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_hook($name) 0 23036 23036
110 if !$self->has_hook($name);
111  
112 23036 ▶ $self->$_isa('Dancer2::Core::App') &&
Branch: 1 branch
BranchTrueFalseTotal
$self->$Dancer2::Core::Role::Hookable::_isa("Dancer2::Core::App") and $self->log("core", "Entering hook $name") 1986 21050 23036
113 $self->log( core => "Entering hook $name" );
114  
115 23036 for my $hook ( @{ $self->hooks->{$name} } ) {
116  
117 825 ▶ $self->log( core => "running hook entry " . subname $hook)
Branch: 1 branch
BranchTrueFalseTotal
if $self->$Dancer2::Core::Role::Hookable::_isa("Dancer2::Core::App") 720 105 825
118 if $self->$_isa('Dancer2::Core::App');
119  
120 825 $hook->(@_);
121 }
122 }
123  
124 219 1;
125  
126 __END__
« blib/lib/Dancer2/Serializer/JSON.pm ↑ Summary blib/lib/Dancer2/Core/Time.pm »