Nile/Hook.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 78 CC 3 SCAR 16.7
CC
3
Cov
0%
AUTOLOAD
12.0
BUILD
2.0
hook_before
2.0
CRAP 5.3
1 # Copyright Infomation
2 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D.
4 # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com
5 # Email : mewsoft@cpan.org, support@mewsoft.com
6 # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved.
7 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 package Nile::Hook;
9  
10 our $VERSION = '0.55';
11 our $AUTHORITY = 'cpan:MEWSOFT';
12  
13 =pod
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Nile::Hook - Hook class for the Nile framework.
20  
21 =head1 SYNOPSIS
22
23 # run this hook before the "start"
24 $me->hook->before_start( sub {
25 my ($me, @args) = @_;
26  
27 });
28
29 # run this hook after the "start"
30 $me->hook->after_start( sub {
31 my ($me, @args) = @_;
32  
33 });
34
35 # inside plugins and modules
36
37 # call the before hook for "start"
38 #$self->me->hook->on_start;
39  
40 # call the after hook for "start"
41 #$self->me->hook->off_start;
42  
43 =head1 DESCRIPTION
44  
45 Nile::Hook - Hook class for the Nile framework.
46  
47 =cut
48  
49 use Nile::Base;
50 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51 sub AUTOLOAD {
52
53 my ($self) = shift;
54  
55 my ($class, $method) = our $AUTOLOAD =~ /^(.*)::(\w+)$/;
56  
57 if ($self->can($method)) {
58 return $self->$method(@_);
59 }
60
61 # $me->hook->before_start(sub {});
62 my ($action, $name) = $method =~ /^(before|after|on|off)_(.*)/;
63  
64 if ($action && $name) {
65 $action = "hook_$action";
66 $self->$action($name, @_);
67 }
68  
69 }
70 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71 sub BUILD {
72 my ($self, $arg) = @_;
73 $self->{hooks}->{before} = +{};
74 $self->{hooks}->{after} = +{};
75 }
76 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77 sub hook_before {
78 my ($self, $name, @args) = @_;
79 #say "hook_before $name, @args";
80 push @{$self->{hooks}->{before}->{$name}}, [@args];
81 }
82 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83 sub hook_after {
84 my ($self, $name, @args) = @_;
85 push @{$self->{hooks}->{after}->{$name}}, [@args];
86 }
87 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88 sub hook_on {
89
90 my ($self, $name, @args) = @_;
91  
92 exists $self->{hooks}->{before}->{$name} || return sub {};
93  
94 my @hooks = @{$self->{hooks}->{before}->{$name}};
95  
96 @hooks || return sub {};
97
98 my ($code, @hook_args);
99  
100 foreach my $hook (@hooks) {
101 ($code, @hook_args) = @{$hook};
102 $code->($self->me, @args, @hook_args);
103 }
104 }
105 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106 sub hook_off {
107
108 my ($self, $name, @args) = @_;
109
110 exists $self->{hooks}->{after}->{$name} || return sub {};
111  
112 my @hooks = @{$self->{hooks}->{after}->{$name}};
113  
114 @hooks || return sub {};
115
116 my ($code, @hook_args);
117  
118 foreach my $hook (@hooks) {
119 ($code, @hook_args) = @{$hook};
120 $code->($self->me, @args, @hook_args);
121 }
122 }
123 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124  
125 =pod
126  
127 =head1 Bugs
128  
129 This project is available on github at L<https://github.com/mewsoft/Nile>.
130  
131 =head1 HOMEPAGE
132  
133 Please visit the project's homepage at L<https://metacpan.org/release/Nile>.
134  
135 =head1 SOURCE
136  
137 Source repository is at L<https://github.com/mewsoft/Nile>.
138  
139 =head1 SEE ALSO
140  
141 See L<Nile> for details about the complete framework.
142  
143 =head1 AUTHOR
144  
145 Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org>
146 Website: http://www.mewsoft.com
147  
148 =head1 COPYRIGHT AND LICENSE
149  
150 Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com,
151 L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com>
152  
153 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
154  
155 =cut
156  
157 1;