blib/lib/Dancer/Plugin.pm

filter: Statementstmt 95.2% 81 / 85 Branchbran 87.5% 7 / 8 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 90.4% 19 / 21 Podpod 66.6% 6 / 9 totaltotal 89.5% 120 / 134 SCAR 22.0
CC
9
Cov
93%
register
6.1
register_plugin
2.0
plugin_args
2.0
CRAP 9.0
« blib/lib/Dancer/Factory/Hook.pm ↑ Summary blib/lib/Dancer/Handler/Standalone.pm »
1 package Dancer::Plugin;
2 28 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: helper for writing Dancer plugins
4 28 $Dancer::Plugin::VERSION = '1.3520';
5 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 28 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Dancer::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Dancer::Factory::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 28 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 28 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 28 @EXPORT = qw(
19 add_hook
20 register
21 register_plugin
22 plugin_setting
23 register_hook
24 execute_hooks
25 execute_hook
26 plugin_args
27 );
28  
29 sub register($&);
30  
31 28 my $_keywords = {};
32  
33 18 ▶sub add_hook { Dancer::Hook->new(@_) }
Subroutine add_hook: called CC=1 SCAR=0.0
Pod undocumented
34  
35 0 ▶sub plugin_args { (undef, @_) }
Subroutine plugin_args: not called CC=1 SCAR=6.9
Pod documented
36  
37 sub plugin_setting {
38 4 ▶ my $plugin_orig_name = caller();
Subroutine plugin_setting: called CC=2 SCAR=6.9
Pod documented
39 4 (my $plugin_name = $plugin_orig_name) =~ s/Dancer::Plugin:://;
40  
41 4 ▶ return setting('plugins')->{$plugin_name} ||= {};
Condition100%2/2
setting("plugins")->{$plugin_name} ||= {}
l!l
31
MC/DC100%1/1
setting("plugins")->{$plugin_name} ||= {}
setting("plugins")->{$plugin_name}
Truth table100%2/2
A
A= setting("plugins")->{$plugin_name}
Aresult
11
00
42 }
43  
44 sub register_hook {
45 2 ▶ Dancer::Factory::Hook->instance->install_hooks(@_);
Subroutine register_hook: called CC=1 SCAR=0.0
Pod documented
46 }
47  
48 sub execute_hooks {
49 0 ▶ Dancer::Deprecation->deprecated(reason => "use 'execute_hook'",
Subroutine execute_hooks: not called CC=1 SCAR=6.9
Pod documented
50 version => '1.3098',
51 fatal => 0);
52 0 Dancer::Factory::Hook->instance->execute_hooks(@_);
53 }
54  
55 sub execute_hook {
56 1 ▶ Dancer::Factory::Hook->instance->execute_hooks(@_);
Subroutine execute_hook: called CC=1 SCAR=0.0
Pod undocumented
57 }
58  
59 sub register($&) {
60 31 ▶ my ($keyword, $code) = @_;
Subroutine register: called CC=6 SCAR=18.0
Pod documented
61 31 my $plugin_name = caller();
62  
63 31 ▶ $keyword =~ /^[a-zA-Z_]+[a-zA-Z0-9_]*$/
Branch: 1 branch
BranchTrueFalseTotal
$keyword =~ /^[a-zA-Z_]+[a-zA-Z0-9_]*$/ or &raise("core_plugin", "You can't use '${keyword}', it is an invalid name" . " (it should match ^[a-zA-Z_]+[a-zA-Z0-9_]*$)") 1 30 31
64 or raise core_plugin => "You can't use '$keyword', it is an invalid name"
65 . " (it should match ^[a-zA-Z_]+[a-zA-Z0-9_]*$ )";
66  
67 30 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $keyword;} map({s/^(?:\$|%|&|@|\*)//; $_;} @Dancer::EXPORT, @Dancer::EXPORT_OK)) 2 28 30
68 2011 grep { $_ eq $keyword }
69 2011 map { s/^(?:\$|%|&|@|\*)//; $_ }
70 (@Dancer::EXPORT, @Dancer::EXPORT_OK)
71 ) {
72 2 raise core_plugin => "You can't use '$keyword', this is a reserved keyword";
73 }
74 28 ▶ while (my ($plugin, $keywords) = each %$_keywords) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_keywords) 3 28 31
75 3 ▶ if (grep { $_->[0] eq $keyword } @$keywords) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->[0] eq $keyword;} @$keywords) 0 3 3
76 0 raise core_plugin => "You can't use $keyword, "
77 . "this is a keyword reserved by $plugin";
78 }
79 }
80  
81 28 ▶ $_keywords->{$plugin_name} ||= [];
Condition50%1/2
$_keywords->{$plugin_name} ||= []
l!l
028
MC/DC0%0/1
$_keywords->{$plugin_name} ||= []
$_keywords->{$plugin_name}
Truth table50%1/2
A
A= $_keywords->{$plugin_name}
Aresult
11
00
82 28 push @{$_keywords->{$plugin_name}}, [$keyword => $code];
83 }
84  
85 sub register_plugin {
86 28 ▶ my ($application) = shift || caller(1);
Subroutine register_plugin: called CC=2 SCAR=6.9
Pod documented
Condition66%2/3
shift() || caller 1
l!l&&r!l&&!r
1027
MC/DC50%1/2
shift() || caller 1
shift() caller 1
Truth table66%2/3
A || B
A= shift()B= caller 1
ABresult
1X1
011
000
87 28 my ($plugin) = caller();
88  
89 28 my @symbols = set_plugin_symbols($plugin);
90 {
91 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
92 # tried to use unshift, but it yields an undef warning on $plugin (perl v5.12.1)
93 28 @{"${plugin}::ISA"} = ('Dancer::Plugin', @{"${plugin}::ISA"});
94 # this works because Dancer::Plugin already ISA Exporter
95 28 push @{"${plugin}::EXPORT"}, @symbols;
96 }
97 28 return 1;
98 }
99  
100 sub set_plugin_symbols {
101 28 ▶ my ($plugin) = @_;
Subroutine set_plugin_symbols: called CC=2 SCAR=6.9
Pod undocumented
102  
103 28 for my $keyword (@{$_keywords->{$plugin}}) {
104 28 my ($name, $code) = @$keyword;
105 {
106 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 28 *{"${plugin}::${name}"} = $code;
108 }
109 }
110 28 return map { $_->[0] } @{$_keywords->{$plugin}};
111 }
112  
113 28 1;
114  
115 __END__
« blib/lib/Dancer/Factory/Hook.pm ↑ Summary blib/lib/Dancer/Handler/Standalone.pm »