Gears/Config.pm

filter: Statementstmt 94.9% 56 / 59 Branchbran 75.0% 24 / 32 Conditioncond 78.5% 11 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 86.0% 111 / 129 CC 26 SCAR 22.6
CC
26
Cov
87%
_merge
17.5
parse
6.4
get
5.0
CRAP 9.6
↑ Summary Gears/Router/Pattern/SigilMatch.pm »
1 package Gears::Config;
2 2 $Gears::Config::VERSION = '0.103';
3 2 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Gears::Config::Reader::PerlScript;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Gears::X::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Value::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 has param 'readers' => (
11 isa => ArrayRef [InstanceOf ['Gears::Config::Reader']],
12 14 ▶ default => sub { [Gears::Config::Reader::PerlScript->new] },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 2 );
14  
15 has field 'config' => (
16 isa => HashRef,
17 15 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 2 );
19  
20 my sub _merge ($this_conf, $this_diff)
21 ▶{
Subroutine _merge: called CC=16 SCAR=28.6
22 23 foreach my $key (sort keys $this_diff->%*) {
23 33 my $value = $this_diff->{$key};
24 33 my $ref = ref $value;
25 33 my $mode;
26  
27 33 ▶ if ($key =~ /^([+-=])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^([+-=])/u) 9 24 33
28 9 $mode = $1;
29 9 $key = substr $key, 1;
30 }
31  
32 # equal sign allow replacing mismatching types of refs
33 33 ▶ if (!exists $this_conf->{$key} || ($mode // '') eq '=') {
Branch: 4 branches
BranchTrueFalseTotal
if (not exists $this_conf->{$key} or ($mode // '') eq '=') { } 25 8 33
elsif (ref $this_conf->{$key} ne $ref) { } 0 8 8
elsif ($ref eq 'HASH') { } 2 6 8
elsif ($ref eq 'ARRAY') { } 5 1 6
Condition100%5/5
not exists $this_conf->{$key} or ($mode // "") eq "="
l!l&&r!l&&!r
2148
$mode // ""
l!l
93
MC/DC100%2/2
not exists $this_conf->{$key} or ($mode // "") eq "="
not exists $this_conf->{$key} $mode
Truth table100%3/3
not(A) or B
A= not exists $this_conf->{$key}B= $mode
ABresult
1X1
011
000
34 25 $this_conf->{$key} = $value;
35 }
36 elsif (ref $this_conf->{$key} ne $ref) {
37 0 Gears::X::Config->raise("configuration key type mismatch for $key");
38 }
39  
40 elsif ($ref eq 'HASH') {
41 2 ▶ if (!defined $mode || $mode eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $mode or $mode eq '+') { } 2 0 2
Condition66%2/3
not defined $mode or $mode eq "+"
l!l&&r!l&&!r
110
MC/DC0%0/2
not defined $mode or $mode eq "+"
not defined $mode $mode eq "+"
Truth table66%2/3
not(A) or B
A= not defined $modeB= $mode eq "+"
ABresult
1X1
011
000
42 2 __SUB__->($this_conf->{$key}, $value);
43 }
44 else {
45 0 Gears::X::Config->raise("$mode$key is not supported for this ref type");
46 }
47 }
48  
49 elsif ($ref eq 'ARRAY') {
50 5 ▶ if (!defined $mode || $mode eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $mode or $mode eq '+') { } 3 2 5
elsif ($mode eq '-' and diff($this_conf->{$key}, $value, \my $rest)) { } 2 0 2
Condition100%3/3
not defined $mode or $mode eq "+"
l!l&&r!l&&!r
122
MC/DC100%2/2
not defined $mode or $mode eq "+"
not defined $mode $mode eq "+"
Truth table100%3/3
not(A) or B
A= not defined $modeB= $mode eq "+"
ABresult
1X1
011
000
51 3 push $this_conf->{$key}->@*, $value->@*;
52 }
53 ▶ elsif ($mode eq '-' && diff($this_conf->{$key}, $value, \my $rest)) {
Condition33%1/3
$mode eq "-" and diff($this_conf->{$key}, $value, \my $rest)
!ll&&!rl&&r
002
MC/DC0%0/2
$mode eq "-" and diff($this_conf->{$key}, $value, \my $rest)
$mode eq "-" diff($this_conf->{$key}, $value, \my $rest)
Truth table33%1/3
A and B
A= $mode eq "-"B= diff($this_conf->{$key}, $value, \my $rest)
ABresult
0X0
100
111
54 2 $this_conf->{$key}->@* = $rest->@*;
55 }
56 }
57  
58 else {
59 1 ▶ Gears::X::Config->raise("$mode$key is not supported for this ref type")
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 0 1 1
60 if defined $mode;
61  
62 1 $this_conf->{$key} = $value;
63 }
64 }
65  
66 }
67  
68 sub merge ($self, $hash)
69 ▶{
Subroutine merge: called CC=2 SCAR=7.1
Pod documented
70 21 my $conf = $self->config;
71 21 ▶ if (diff($hash, $conf, \my $diff)) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff($hash, $conf, \my $diff)) 21 0 21
72 21 _merge($conf, $diff);
73 }
74 }
75  
76 sub parse ($self, $source_type, $source)
77 ▶{
Subroutine parse: called CC=6 SCAR=18.6
Pod documented
78 23 ▶ if ($source_type eq 'file') {
Branch: 2 branches
BranchTrueFalseTotal
if ($source_type eq 'file') { } 4 19 23
elsif ($source_type eq 'var') { } 19 0 19
79 4 my $config;
80 4 foreach my $reader ($self->readers->@*) {
81 4 ▶ next unless $reader->handles($source);
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->handles($source) 0 4 4
82 4 $config = $reader->parse($self, $source);
83 3 last;
84 }
85  
86 3 ▶ Gears::X::Config->raise("no reader to handle file: $source")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 3 3
87 unless defined $config;
88  
89 3 return $config;
90 }
91 elsif ($source_type eq 'var') {
92 19 return $source;
93 }
94 else {
95 0 Gears::X::Config->raise("unknown type of config to add: $source_type");
96 }
97 }
98  
99 sub add ($self, $source_type, $source)
100 ▶{
Subroutine add: called CC=1 SCAR=0.0
Pod documented
101 22 $self->merge($self->parse($source_type, $source));
102 21 return $self;
103 }
104  
105 sub get ($self, $path, $default = undef)
106 9 ▶{
Subroutine get: called CC=5 SCAR=16.1
Pod documented
107 9 my $current = $self->config;
108  
109 9 foreach my $part (split /\./, $path) {
110 15 ▶ Gears::X::Config->raise("invalid config path $path at part $part - not a hash")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $current eq "HASH" 2 13 15
111 unless ref $current eq 'HASH';
112  
113 13 ▶ return $default unless exists $current->{$part};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $current->{$part} 3 10 13
114 10 $current = $current->{$part};
115 }
116  
117 4 return $current;
118 }
119  
120 __END__
↑ Summary Gears/Router/Pattern/SigilMatch.pm »