lib/Ubic/ServiceLoader.pm

filter: Statementstmt 100% 39 / 39 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 96.4% 54 / 56 SCAR 13.9
CC
4
Cov
96%
load
3.0
ext2loader
2.0
BEGIN
1.0
CRAP 4.0
« lib/Ubic/Multiservice/Simple.pm ↑ Summary lib/Ubic/AccessGuard.pm »
1 package Ubic::ServiceLoader;
2 154 $Ubic::ServiceLoader::VERSION = '1.59';
3 # ABSTRACT: load service from file
4  
5 154 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 154 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 154 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 154 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 154 ▶use Ubic::ServiceLoader::Default;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 154 my %ext2loader;
14  
15 sub ext2loader {
16 1 ▶ my $class = shift;
Subroutine ext2loader: called CC=2 SCAR=7.0
Pod documented
17 1 my ($ext) = validate_pos(@_, { type => SCALAR, regex => qr/^\w+$/ });
18  
19 1 ▶ return $ext2loader{$ext} if $ext2loader{$ext};
Branch: 1 branch
BranchTrueFalseTotal
if $ext2loader{$ext} 0 1 1
20 1 require "Ubic/ServiceLoader/Ext/$ext.pm"; # TODO - improve error message if ext.pm doesn't exist
21 1 my $loader_class = "Ubic::ServiceLoader::Ext::$ext";
22 1 return $loader_class->new;
23 # FIXME - cache loader_class!
24 }
25  
26 sub split_service_filename {
27 2020 ▶ my $class = shift;
Subroutine split_service_filename: called CC=1 SCAR=0.0
Pod documented
28 2020 my ($filename) = validate_pos(@_, 1);
29  
30 2020 my ($service_name, $ext) = $filename =~ /^
31 ([\w-]+)
32 (?: \.(\w+) )?
33 $/x;
34 2020 return ($service_name, $ext);
35 }
36  
37 sub load {
38 974 ▶ my $class = shift;
Subroutine load: called CC=3 SCAR=11.0
Pod documented
39 974 my ($file) = validate_pos(@_, 1);
40  
41 974 my $filename = basename($file);
42 974 my ($service_name, $ext) = $class->split_service_filename($filename);
43 974 ▶ die "Invalid filename '$file'" unless defined $service_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $service_name 0 974 974
44  
45 974 my $loader;
46 974 ▶ if ($ext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext) { } 1 973 974
47 1 $loader = $class->ext2loader($ext);
48 }
49 else {
50 973 $loader = Ubic::ServiceLoader::Default->new;
51 }
52  
53 974 my $service = $loader->load($file);
54 971 return $service;
55 }
56  
57  
58 154 1;
59  
60 __END__
« lib/Ubic/Multiservice/Simple.pm ↑ Summary lib/Ubic/AccessGuard.pm »