Yak/NamedPlugins.pm

filter: Statementstmt 92.5% 62 / 67 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod n/a totaltotal 91.0% 81 / 89 CC 5 SCAR 5.8
CC
5
Cov
90%
_init_plugins
4.2
__ANON__
2.0
__ANON__
2.0
CRAP 1.8
« Yak/OrderedPlugins.pm ↑ Summary Yak/LazyConfig.pm »
1 package Config::Yak::NamedPlugins;
2 {
3 3 $Config::Yak::NamedPlugins::VERSION = '0.23';
4 }
5 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $Config::Yak::NamedPlugins::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: a role to provide handling of named 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 Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # extends ...
27 # has ...
28 3 has '_finder' => (
29 'is' => 'rw',
30 'isa' => 'Module::Pluggable::Object',
31 'lazy' => 1,
32 'builder' => '_init_finder',
33 'accessor' => 'finder',
34 );
35  
36 3 has '_plugins' => (
37 'is' => 'rw',
38 'isa' => 'HashRef',
39 'lazy' => 1,
40 'builder' => '_init_plugins',
41 'accessor' => 'plugins',
42 );
43  
44 3 has '_finder_search_path' => (
45 'is' => 'rw',
46 'isa' => 'ArrayRef[Str]',
47 'lazy' => 1,
48 'builder' => '_init_finder_search_path',
49 );
50  
51 # with ...
52 3 with qw(Config::Yak::RequiredConfig Log::Tree::RequiredLogger);
53 # initializers ...
54 1 ▶sub _init_finder_search_path {
Subroutine _init_finder_search_path: called CC=1 SCAR=0.0
55 1 my $self = shift;
56  
57 1 return [$self->_plugin_base_class()];
58 }
59  
60 1 ▶sub _init_finder {
Subroutine _init_finder: called CC=1 SCAR=0.0
61 1 my $self = shift;
62  
63 # The finder is the class that finds our available plugins
64 1 my $Finder = Module::Pluggable::Object::->new( 'search_path' => $self->_finder_search_path() );
65  
66 1 return $Finder;
67 } ## end sub _init_finder
68  
69 1 ▶sub _init_plugins {
Subroutine _init_plugins: called CC=4 SCAR=14.4
70 1 my $self = shift;
71  
72 1 my $plugin_ref = {};
73  
74 1 PLUGIN: foreach my $plugin_name ( $self->finder()->plugins() ) {
75 ## no critic (ProhibitStringyEval)
76 4 my $eval_status = eval "require $plugin_name;";
77 ## use critic
78 4 ▶ if ( !$eval_status ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eval_status) 0 4 4
79 0 $self->logger()->log( message => 'Failed to require ' . $plugin_name . ': ' . $EVAL_ERROR, level => 'warning', );
80 0 next;
81 }
82 4 my $arg_ref = $self->config()->get($plugin_name);
83 4 $arg_ref->{'logger'} = $self->logger();
84 4 $arg_ref->{'config'} = $self->config();
85 4 $arg_ref->{'parent'} = $self;
86 4 Scalar::Util::weaken( $arg_ref->{'parent'} );
87 4 ▶ if ( $arg_ref->{'disabled'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"disabled"}) 0 4 4
88 0 $self->logger()->log( message => 'Skipping disabled plugin: ' . $plugin_name, level => 'debug', );
89 0 next PLUGIN;
90 }
91 4 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
92 4 my $Plugin = $plugin_name->new($arg_ref);
93 4 my $prio = $Plugin->priority();
94  
95 # disabled/abstract plugins will set a prio of 0
96 4 ▶ if ( $prio > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prio > 0) { } 3 1 4
97 3 $plugin_ref->{$plugin_name} = $Plugin;
98 3 $self->logger()->log( message => 'Loaded plugin '.ref($Plugin).' w/ name '.$plugin_name, level => 'debug', );
99 } else {
100 1 $self->logger()->log( message => 'Skipped loaded plugin '.ref($Plugin).' w/ name '.$plugin_name, level => 'debug', );
101 }
102 } ## end try
103 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 $self->logger()->log( message => 'Failed to initialize plugin ' . $plugin_name . ' w/ error: ' . $_, level => 'warning', );
105 4 };
106 } ## end foreach my $plugin_name ( $self...)
107  
108 1 return $plugin_ref;
109 } ## end sub _init_plugins
110  
111 # requires ...
112 3 requires qw(_plugin_base_class);
113  
114 # your code here ...
115  
116 3 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 3 1;
119  
120 __END__
« Yak/OrderedPlugins.pm ↑ Summary Yak/LazyConfig.pm »