loader.pm

filter: Statementstmt 85.0% 74 / 87 Branchbran 61.1% 11 / 18 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 81.2% 13 / 16 Podpod 0.0% 0 / 8 totaltotal 66.8% 103 / 154 CC 20 SCAR 15.3
CC
20
Cov
75%
_read
12.0
load_from_hashref
7.3
load_from_filename
6.0
CRAP 4.6
« loader/JSON.pm ↑ Summary alias.pm »
1 3 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package portable::loader;
6  
7 3 our $AUTHORITY = 'cpan:TOBYINK';
8 3 our $VERSION = '0.003';
9  
10 3 ▶use portable::lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Module::Pluggable (
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 search_path => ['portable::loader'],
14 sub_name => '_plugins',
15 require => 1,
16 3 );
17  
18 {
19 3 my @loaders;
20 0 ▶ sub loaders {
Subroutine loaders: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my $me = shift;
22 0 @loaders;
23 }
24 9 ▶ sub add_loader {
Subroutine add_loader: called CC=3 SCAR=11.1
Pod undocumented
25 9 my $me = shift;
26 9 for my $loader (@_) {
27 9 ▶ $loader->init($me) if $loader->can('init');
Branch: 1 branch
BranchTrueFalseTotal
if $loader->can("init") 9 0 9
28 9 push @loaders, $loader;
29 }
30 }
31 }
32  
33 {
34 3 my %extensions;
35 7 ▶ sub extensions {
Subroutine extensions: called CC=1 SCAR=0.0
Pod undocumented
36 7 %extensions;
37 }
38 12 ▶ sub register_extension {
Subroutine register_extension: called CC=2 SCAR=9.2
Pod undocumented
39 12 ▶ $extensions{$_[1]} = $_[2] || caller;
Condition33%1/3
$_[2] || caller
l!l&&r!l&&!r
0120
MC/DC0%0/2
$_[2] || caller
$_[2] caller
Truth table33%1/3
A || B
A= $_[2]B= caller
ABresult
1X1
011
000
40 }
41 }
42  
43 3 ▶sub _croak {
Subroutine _croak: not called CC=1 SCAR=4.1
44 0 my $me = shift;
45 0 my ($msg, @args) = @_;
46 0 require Carp;
47 0 Carp::croak(sprintf($msg, @args));
48 }
49  
50 0 ▶sub _read {
Subroutine _read: not called CC=3 SCAR=24.8
51 0 my $me = shift;
52 0 my ($collection) = @_;
53 0 for my $loader ($me->loaders) {
54 0 my ($fn, $loaded) = $loader->load($collection);
55 0 ▶ return ($fn, $loaded) if $loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $loaded 0 0 0
56 }
57 0 $me->_croak('Could not load portable collection %s', $collection);
58 }
59  
60 {
61 3 my $i = 0;
62 3 ▶ sub _mint_prefix {
Subroutine _mint_prefix: called CC=1 SCAR=0.0
63 3 ++$i;
64 3 "portable::collection::Collection$i";
65 }
66 }
67  
68 3 ▶sub load {
Subroutine load: called CC=2 SCAR=7.0
Pod undocumented
69 4 my $me = shift;
70 4 my ($collection) = @_;
71  
72 4 my $file = $me->find_collection($collection);
73 4 ▶ $me->_croak("Could not load collection $collection")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 4 4
74 unless defined $file;
75
76 # method call preserving caller
77 4 my $next = $me->can('load_from_filename');
78 4 @_ = ($me, $file);
79 4 goto $next;
80 }
81  
82 4 ▶sub find_collection {
Subroutine find_collection: called CC=4 SCAR=13.9
Pod undocumented
83 4 my $me = shift;
84 4 my ($collection) = @_;
85 4 my %exts = $me->extensions;
86 4 DIR: for my $dir (@portable::INC) {
87 8 EXT: for my $ext (sort keys %exts) {
88 26 my $qualified = "$dir/$collection.$ext";
89 26 ▶ return $qualified if -f $qualified;
Branch: 1 branch
BranchTrueFalseTotal
if -f $qualified 4 22 26
90 }
91 }
92 0 return;
93 }
94  
95 4 ▶sub load_from_filename {
Subroutine load_from_filename: called CC=6 SCAR=18.0
Pod undocumented
96 4 my $me = shift;
97 4 my ($filename, $handler) = @_;
98
99 return $portable::INC{$filename}
100 4 ▶ if $portable::INC{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if $portable::INC{$filename} 1 3 4
101
102 3 ▶ unless (defined $handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $handler) 3 0 3
103 3 my %exts = $me->extensions;
104 3 for my $ext (sort keys %exts) {
105 12 my $qext = quotemeta $ext;
106 12 ▶ if ($filename =~ /$qext\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /$qext\z/) 3 9 12
107 3 $handler = $exts{$ext};
108 }
109 }
110 3 ▶ $me->_croak("Could not find plugin to load file $filename")
Branch: 1 branch
BranchTrueFalseTotal
unless $handler 0 3 3
111 unless $handler;
112 }
113
114 3 my $hashref = $handler->parse($filename);
115 3 $hashref->{____source____} = $filename;
116
117 # method call preserving caller
118 3 my $next = $me->can('load_from_hashref');
119 3 @_ = ($me, $hashref);
120 3 goto $next;
121 }
122  
123 3 ▶sub load_from_hashref {
Subroutine load_from_hashref: called CC=6 SCAR=19.9
Pod undocumented
124 3 require MooX::Press;
125 3 'MooX::Press'->VERSION('0.011');
126 3 my $me = shift;
127 3 my %opts = %{ $_[0] };
128 3 ▶ $opts{prefix} ||= $me->_mint_prefix;
Condition33%1/3
$opts{"prefix"} ||= $me->_mint_prefix
l!l&&r!l&&!r
030
MC/DC0%0/2
$opts{"prefix"} ||= $me->_mint_prefix
$opts{"prefix"} $me->_mint_prefix
Truth table33%1/3
A ||= B
A= $opts{"prefix"}B= $me->_mint_prefix
ABresult
1X1
011
000
129 3 ▶ $opts{factory_package} ||= $opts{prefix};
Condition33%1/3
$opts{"factory_package"} ||= $opts{"prefix"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$opts{"factory_package"} ||= $opts{"prefix"}
$opts{"factory_package"} $opts{"prefix"}
Truth table33%1/3
A ||= B
A= $opts{"factory_package"}B= $opts{"prefix"}
ABresult
1X1
011
000
130 3 ▶ $opts{caller} ||= caller;
Condition33%1/3
$opts{"caller"} ||= caller
l!l&&r!l&&!r
030
MC/DC0%0/2
$opts{"caller"} ||= caller
$opts{"caller"} caller
Truth table33%1/3
A ||= B
A= $opts{"caller"}B= caller
ABresult
1X1
011
000
131 3 'MooX::Press'->import(%opts);
132 3 ▶ my $return = $opts{factory_package} || $opts{caller};
Condition33%1/3
$opts{'factory_package'} || $opts{'caller'}
l!l&&r!l&&!r
300
MC/DC0%0/2
$opts{'factory_package'} || $opts{'caller'}
$opts{'factory_package'} $opts{'caller'}
Truth table33%1/3
A || B
A= $opts{'factory_package'}B= $opts{'caller'}
ABresult
1X1
011
000
133 3 ▶ if ($opts{____source____}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"____source____"}) 3 0 3
134 3 $portable::INC{$opts{____source____}} = $return;
135 }
136 3 $return;
137 }
138  
139 # init
140 3 __PACKAGE__->add_loader($_) for __PACKAGE__->_plugins;
141  
142 3 1;
143  
144 __END__
« loader/JSON.pm ↑ Summary alias.pm »