lib/Ubic/ServiceLoader/Ext/json.pm

filter: Statementstmt 97.8% 45 / 46 Branchbran 62.5% 10 / 16 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 90.6% 68 / 75 SCAR 22.1
CC
9
Cov
89%
load
9.3
BEGIN
1.0
BEGIN
1.0
CRAP 9.1
« blib/lib/Ubic/Settings/ConfigFile.pm ↑ Summary blib/lib/Ubic/Run.pm »
1 package Ubic::ServiceLoader::Ext::json;
2 2 $Ubic::ServiceLoader::Ext::json::VERSION = '1.59';
3 # ABSTRACT: loader for json-style configs
4  
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use parent qw( Ubic::ServiceLoader::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 {
14 # support the compatibility with JSON.pm v1 just because we can
15 # see also: Ubic::Persistent
16 2 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ sub jsonToObj; *jsonToObj = (*{JSON::from_json}{CODE}) ? \&JSON::from_json : \&JSON::jsonToObj;
Branch: 1 branch
BranchTrueFalseTotal
*JSON::from_json{'CODE'} ? : 2 0 2
19 }
20  
21 sub new {
22 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 1 return bless {} => $class;
24 }
25  
26 sub load {
27 4 ▶ my $self = shift;
Subroutine load: called CC=9 SCAR=22.3
Pod documented
28 4 my ($file) = @_;
29  
30 4 ▶ open my $fh, '<', $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't open ${file}: $!" 0 4 4
31 4 my $content = do { local $/; <$fh> };
32 4 ▶ close $fh or die "Can't close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close ${file}: $!" 0 4 4
33  
34 4 my $config = eval { jsonToObj $content };
35 4 ▶ unless ($config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 1 3 4
36 1 die "Failed to parse $file: $@";
37 }
38  
39 3 ▶ my $module = delete $config->{module} || 'Ubic::Service::SimpleDaemon';
Condition100%2/2
delete $config->{'module'} || 'Ubic::Service::SimpleDaemon'
l!l
21
MC/DC100%1/1
delete $config->{'module'} || 'Ubic::Service::SimpleDaemon'
delete $config->{'module'}
Truth table100%2/2
A
A= delete $config->{'module'}
Aresult
11
00
40  
41 3 my $options = delete $config->{options};
42 3 ▶ if (keys %$config) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$config) 1 2 3
43 1 die "Unknown option '".join(', ', keys %$config)."' in file $file";
44 }
45  
46 2 ▶ $module =~ /^[\w:]+$/ or die "Invalid module name '$module'";
Branch: 1 branch
BranchTrueFalseTotal
$module =~ /^[\w:]+$/ or die "Invalid module name '${module}'" 0 2 2
47 2 eval "require $module"; # TODO - Class::Load?
48 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
49 0 die $@;
50 }
51  
52 2 my @options = ();
53 2 ▶ @options = ($options) if $options; # some modules can have zero options, I guess
Branch: 1 branch
BranchTrueFalseTotal
if $options 2 0 2
54 2 return $module->new(@options);
55 }
56  
57 2 1;
58  
59 __END__
« blib/lib/Ubic/Settings/ConfigFile.pm ↑ Summary blib/lib/Ubic/Run.pm »