| 1 | package Dancer::Factory::Hook; | ||||||||||
| 2 | 247 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||
| 3 | # ABSTRACT: Singleton class to create Dancer hooks | ||||||||||
| 4 | 247 | $Dancer::Factory::Hook::VERSION = '1.3522'; | |||||||||
| 5 | 247 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 247 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 247 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 247 | ▶ | use base 'Dancer::Object::Singleton'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 247 | ▶ | use Dancer::Exception qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 247 | __PACKAGE__->attributes(qw/ hooks registered_hooks/); | |||||||||
| 13 | |||||||||||
| 14 | 245 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 15 | 245 | my ( $class, $self ) = @_; | |||||||||
| 16 | 245 | $self->hooks( {} ); | |||||||||
| 17 | 245 | $self->registered_hooks( [] ); | |||||||||
| 18 | 245 | return $self; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 1420 | ▶ | sub install_hooks { | ||||||||
|
Subroutine
install_hooks: called CC=4 SCAR=15.6
Pod
undocumented
| |||||||||||
| 22 | 1420 | my ( $self, @hooks_name ) = @_; | |||||||||
| 23 | |||||||||||
| 24 | 1420 | ▶ | if ( !scalar @hooks_name ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | raise core_factory_hook => "at least one name is required"; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 1420 | foreach my $hook_name (@hooks_name) { | |||||||||
| 29 | 3668 | ▶ | if ( $self->hook_is_registered($hook_name) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | raise core_factory_hook => "$hook_name is already regsitered, please use another name"; | |||||||||
| 31 | } | ||||||||||
| 32 | 3668 | $self->_add_hook( $hook_name ); | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 133 | ▶ | sub register_hook { | ||||||||
|
Subroutine
register_hook: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | 133 | my ( $self, $hook ) = @_; | |||||||||
| 38 | 133 | $self->_add_registered_hook( $hook->name, $hook->code ); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 133 | ▶ | sub _add_registered_hook { | ||||||||
|
Subroutine
_add_registered_hook: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 133 | my ($class, $hook_name, $compiled_filter) = @_; | |||||||||
| 43 | 133 | push @{$class->hooks->{$hook_name}}, $compiled_filter; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 3668 | ▶ | sub _add_hook { | ||||||||
|
Subroutine
_add_hook: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 3668 | my ($self, $hook_name ) = @_; | |||||||||
| 48 | 3668 | push @{$self->registered_hooks}, $hook_name; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 5655 | ▶ | sub hook_is_registered { | ||||||||
|
Subroutine
hook_is_registered: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 52 | 5655 | my ( $self, $hook_name ) = @_; | |||||||||
| 53 | 5655 | return grep { $_ eq $hook_name } @{$self->registered_hooks}; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 1987 | ▶ | sub execute_hooks { | ||||||||
|
Subroutine
execute_hooks: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||
| 57 | 1987 | my ($self, $hook_name, @args) = @_; | |||||||||
| 58 | |||||||||||
| 59 | 1987 | ▶ | raise core_factory_hook => "Can't ask for hooks without a position" unless $hook_name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | |||||||||||
| 61 | 1987 | ▶ | if (!$self->hook_is_registered($hook_name)){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 1989 | ▶ | sub get_hooks_for { | ||||||||
|
Subroutine
get_hooks_for: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 71 | 1989 | my ( $self, $hook_name ) = @_; | |||||||||
| 72 | |||||||||||
| 73 | 1989 | ▶ | raise core_factory_hook => "Can't ask for hooks without a position" unless $hook_name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | |||||||||||
| 75 | 1989 | ▶ | $self->hooks->{$hook_name} || []; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | 247 | 1; | |||||||||
| 80 | |||||||||||
| 81 | __END__ | ||||||||||