Yak/OrderedPlugins.pm

filter: Statementstmt 92.9% 66 / 71 Branchbran 66.6% 4 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.3% 14 / 15 Podpod n/a totaltotal 87.6% 85 / 97 CC 7 SCAR 9.9
CC
7
Cov
89%
_init_plugins
6.4
__ANON__
2.0
__ANON__
2.0
CRAP 2.7
« Yak.pm ↑ Summary Yak/NamedPlugins.pm »
1 package Config::Yak::OrderedPlugins;
2 {
3 3 $Config::Yak::OrderedPlugins::VERSION = '0.23';
4 }
5 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $Config::Yak::OrderedPlugins::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: a role to provide handling of ordered plugins
9  
10 3 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20 # use Carp;
21 3 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # extends ...
26 # has ...
27 3 has '_finder' => (
28 'is' => 'rw',
29 'isa' => 'Module::Pluggable::Object',
30 'lazy' => 1,
31 'builder' => '_init_finder',
32 'accessor' => 'finder',
33 );
34  
35 3 has '_plugins' => (
36 'is' => 'rw',
37 'isa' => 'ArrayRef',
38 'lazy' => 1,
39 'builder' => '_init_plugins',
40 'accessor' => 'plugins',
41 );
42  
43 3 has '_finder_search_path' => (
44 'is' => 'rw',
45 'isa' => 'ArrayRef[Str]',
46 'lazy' => 1,
47 'builder' => '_init_finder_search_path',
48 );
49  
50 # with ...
51 3 with qw(Config::Yak::RequiredConfig Log::Tree::RequiredLogger);
52 # initializers ...
53 1 ▶sub _init_finder_search_path {
Subroutine _init_finder_search_path: called CC=1 SCAR=0.0
54 1 my $self = shift;
55  
56 1 return [$self->_plugin_base_class()];
57 }
58  
59 1 ▶sub _init_finder {
Subroutine _init_finder: called CC=1 SCAR=0.0
60 1 my $self = shift;
61  
62 # The finder is the class that finds our available plugins
63 1 my $Finder = Module::Pluggable::Object::->new( 'search_path' => $self->_finder_search_path() );
64  
65 1 return $Finder;
66 } ## end sub _init_finder
67  
68 1 ▶sub _init_plugins {
Subroutine _init_plugins: called CC=6 SCAR=18.6
69 1 my $self = shift;
70  
71 # Allow the plugins to be sorted by prio in the config with the Priority
72 # key in each plugin section. Why priorities? It may make sense or even
73 # be a strict requirement to run some plugins before or after
74 # another one.
75 1 my $order_ref = {};
76  
77 1 PLUGIN: foreach my $plugin_name ( $self->finder()->plugins() ) {
78 ## no critic (ProhibitStringyEval)
79 4 my $eval_status = eval "require $plugin_name;";
80 ## use critic
81 4 ▶ if ( !$eval_status ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_status) 0 4 4
82 0 $self->logger()->log( message => 'Failed to require ' . $plugin_name . ': ' . $EVAL_ERROR, level => 'warning', );
83 0 next;
84 }
85 4 my $arg_ref = $self->config()->get($plugin_name);
86 4 $arg_ref->{'logger'} = $self->logger();
87 4 $arg_ref->{'config'} = $self->config();
88 4 $arg_ref->{'parent'} = $self;
89 4 ▶ $arg_ref->{'name'} ||= $plugin_name;
Condition33%1/3
$arg_ref->{"name"} ||= $plugin_name
l!l&&r!l&&!r
040
MC/DC0%0/2
$arg_ref->{"name"} ||= $plugin_name
$arg_ref->{"name"} $plugin_name
Truth table33%1/3
A ||= B
A= $arg_ref->{"name"}B= $plugin_name
ABresult
1X1
011
000
90 4 ▶ if ( $arg_ref->{'disabled'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"disabled"}) 0 4 4
91 0 $self->logger()->log( message => 'Skipping disabled plugin: ' . $plugin_name, level => 'debug', );
92 0 next PLUGIN;
93 }
94 4 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
95 4 my $Plugin = $plugin_name->new($arg_ref);
96 4 my $prio = $Plugin->priority();
97  
98 # disabled/abstract plugins will set a prio of 0
99 4 ▶ if ( $prio > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prio > 0) { } 3 1 4
100 3 push( @{ $order_ref->{$prio} }, $Plugin );
101 3 $self->logger()->log( message => 'Loaded plugin '.ref($Plugin).' w/ priority '.$prio, level => 'debug', );
102 } else {
103 1 $self->logger()->log( message => 'Skipped loaded plugin '.ref($Plugin).' w/ priority '.$prio, level => 'debug', );
104 }
105 } ## end try
106 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 $self->logger()->log( message => 'Failed to initialize plugin ' . $plugin_name . ' w/ error: ' . $_, level => 'warning', );
108 4 };
109 } ## end foreach my $plugin_name ( $self...)
110  
111 1 my @plugins = ();
112  
113 # this basically merges the pre-sorted sub-arrays (which, if you follow
114 # my advice, should each contain only one element ... but we must
115 # plan for the unexptected, of course)
116 1 foreach my $prio ( sort { $a <=> $b } keys %{$order_ref} ) {
117 #$self->logger()->log( message => 'Adding Plugins '.join(',',map { ref($_) } @{$order_ref->{$prio}}).' w/ prio '.$prio, level => 'debug', );
118 3 push( @plugins, @{ $order_ref->{$prio} } );
119 }
120  
121 1 return \@plugins;
122 } ## end sub _init_plugins
123  
124 # requires ...
125 3 requires qw(_plugin_base_class);
126  
127 # your code here ...
128  
129 3 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 3 1;
132  
133 __END__
« Yak.pm ↑ Summary Yak/NamedPlugins.pm »