Gears/Config.pm

filter: Statementstmt 95.0% 58 / 61 Branchbran 76.4% 26 / 34 Conditioncond 90.9% 10 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 89.0% 114 / 128 CC 26 SCAR 22.2
CC
26
Cov
89%
_merge
16.7
parse
6.4
get
5.0
CRAP 9.2
↑ Summary Gears/Router/Pattern/SigilMatch.pm »
1 package Gears::Config;
2 2 $Gears::Config::VERSION = '0.105';
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 16 ▶ 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 17 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 2 );
19  
20 26 ▶my sub _merge ($this_conf, $this_diff)
Subroutine _merge: called CC=16 SCAR=28.1
21 {
22 26 foreach my $key (sort keys $this_diff->%*) {
23 36 my $value = $this_diff->{$key};
24 36 my $ref = ref $value;
25 36 my $mode;
26  
27 36 ▶ if ($key =~ /^([+=-])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^([+=-])/u) 10 26 36
28 10 $mode = $1;
29 10 $key = substr $key, 1;
30 }
31  
32 # equal sign allow replacing mismatching types of refs
33 36 ▶ if (!exists $this_conf->{$key} || ($mode // '') eq '=') {
Branch: 4 branches
BranchTrueFalseTotal
if (not exists $this_conf->{$key} or ($mode // '') eq '=') { } 27 9 36
elsif (ref $this_conf->{$key} ne $ref) { } 0 9 9
elsif ($ref eq 'HASH') { } 2 7 9
elsif ($ref eq 'ARRAY') { } 6 1 7
Condition100%5/5
not exists $this_conf->{$key} or ($mode // "") eq "="
l!l&&r!l&&!r
2349
$mode // ""
l!l
103
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 27 $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 6 ▶ if (!defined $mode || $mode eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $mode or $mode eq '+') { } 3 3 6
elsif ($mode eq '-') { } 3 0 3
Condition100%3/3
not defined $mode or $mode eq "+"
l!l&&r!l&&!r
123
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 '-') {
54 3 my $rest;
55 $rest = []
56 3 ▶ unless diff($this_conf->{$key}, $value, \$rest);
Branch: 1 branch
BranchTrueFalseTotal
unless diff($this_conf->{$key}, $value, \$rest) 1 2 3
57  
58 3 $this_conf->{$key}->@* = $rest->@*;
59 }
60 }
61  
62 else {
63 1 ▶ Gears::X::Config->raise("$mode$key is not supported for this ref type")
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 0 1 1
64 if defined $mode;
65  
66 1 $this_conf->{$key} = $value;
67 }
68 }
69  
70 }
71  
72 24 ▶sub merge ($self, $hash)
Subroutine merge: called CC=2 SCAR=7.1
Pod documented
73 {
74 24 my $conf = $self->config;
75 24 ▶ if (diff($hash, $conf, \my $diff)) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff($hash, $conf, \my $diff)) 24 0 24
76 24 _merge($conf, $diff);
77 }
78 }
79  
80 26 ▶sub parse ($self, $source_type, $source)
Subroutine parse: called CC=6 SCAR=18.6
Pod documented
81 {
82 26 ▶ if ($source_type eq 'file') {
Branch: 2 branches
BranchTrueFalseTotal
if ($source_type eq 'file') { } 4 22 26
elsif ($source_type eq 'var') { } 22 0 22
83 4 my $config;
84 4 foreach my $reader ($self->readers->@*) {
85 4 ▶ next unless $reader->handles($source);
Branch: 1 branch
BranchTrueFalseTotal
unless $reader->handles($source) 0 4 4
86 4 $config = $reader->parse($self, $source);
87 3 last;
88 }
89  
90 3 ▶ Gears::X::Config->raise("no reader to handle file: $source")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 3 3
91 unless defined $config;
92  
93 3 return $config;
94 }
95 elsif ($source_type eq 'var') {
96 22 return $source;
97 }
98 else {
99 0 Gears::X::Config->raise("unknown type of config to add: $source_type");
100 }
101 }
102  
103 25 ▶sub add ($self, $source_type, $source)
Subroutine add: called CC=1 SCAR=0.0
Pod documented
104 {
105 25 $self->merge($self->parse($source_type, $source));
106 24 return $self;
107 }
108  
109 9 ▶sub get ($self, $path, $default = undef)
Subroutine get: called CC=5 SCAR=16.1
Pod documented
110 9 {
111 9 my $current = $self->config;
112  
113 9 foreach my $part (split /\./, $path) {
114 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
115 unless ref $current eq 'HASH';
116  
117 13 ▶ return $default unless exists $current->{$part};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $current->{$part} 3 10 13
118 10 $current = $current->{$part};
119 }
120  
121 4 return $current;
122 }
123  
124 __END__
↑ Summary Gears/Router/Pattern/SigilMatch.pm »