blib/lib/Config/Model/Backend/IniFile.pm

filter: Statementstmt 96.9% 221 / 228 Branchbran 89.7% 70 / 78 Conditioncond 68.4% 52 / 76 MC/DCmcdc 44.7% 17 / 38 Subroutinesub 100% 21 / 21 Podpod 75.0% 3 / 4 totaltotal 86.2% 384 / 445 CC 77 SCAR 24.8
CC
77
Cov
90%
read
25.0
_write_leaf
12.4
_write
12.0
CRAP 12.0
« blib/lib/Config/Model.pm ↑ Summary blib/lib/Config/Model/AnyId.pm »
1 #
2 # This file is part of Config-Model
3 #
4 # This software is Copyright (c) 2005-2022 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::Backend::IniFile 2.169;
11  
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 ▶use base qw/Config::Model::Backend::Any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # change inherited attribute. See Moose::Manual::Attributes
25 5 has '+node' => (
26 handles => ['load_data'],
27 );
28  
29 5 my $logger = get_logger("Backend::IniFile");
30  
31 33 ▶sub annotation { return 1; }
Subroutine annotation: called CC=1 SCAR=0.0
Pod documented
32  
33 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
34 33 ▶sub read ($self, %args) {
Subroutine read: called CC=25 SCAR=32.2
Pod documented
35 # args is:
36 # object => $obj, # Config::Model::Node object
37 # root => './my_test', # fake root directory, userd for tests
38 # config_dir => /etc/foo', # absolute path
39 # file => 'foo.conf', # file name
40 # file_path => './my_test/etc/foo/foo.conf'
41 # check => yes|no|skip
42  
43 33 ▶ return 0 unless $args{file_path}->exists; # no file to read
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"file_path"}->exists 2 31 33
44  
45 31 my $section = '<top>'; # dumb value used for logging
46  
47 31 ▶ my $delimiter = $args{comment_delimiter} || '#';
Condition100%2/2
$args{'comment_delimiter'} || '#'
l!l
427
MC/DC100%1/1
$args{'comment_delimiter'} || '#'
$args{'comment_delimiter'}
Truth table100%2/2
A
A= $args{'comment_delimiter'}
Aresult
11
00
48 31 ▶ my $hash_class = $args{store_class_in_hash} || '';
Condition100%2/2
$args{'store_class_in_hash'} || ''
l!l
625
MC/DC100%1/1
$args{'store_class_in_hash'} || ''
$args{'store_class_in_hash'}
Truth table100%2/2
A
A= $args{'store_class_in_hash'}
Aresult
11
00
49 31 ▶ my $section_map = $args{section_map} || {};
Condition100%2/2
$args{'section_map'} || {}
l!l
427
MC/DC100%1/1
$args{'section_map'} || {}
$args{'section_map'}
Truth table100%2/2
A
A= $args{'section_map'}
Aresult
11
00
50 31 my $split_reg = $args{split_list_value};
51 31 ▶ my $check = $args{check} || 'yes';
Condition50%1/2
$args{'check'} || 'yes'
l!l
310
MC/DC0%0/1
$args{'check'} || 'yes'
$args{'check'}
Truth table50%1/2
A
A= $args{'check'}
Aresult
11
00
52 31 ▶ my $assign_char = $args{assign_char} || '=';
Condition100%2/2
$args{'assign_char'} || '='
l!l
229
MC/DC100%1/1
$args{'assign_char'} || '='
$args{'assign_char'}
Truth table100%2/2
A
A= $args{'assign_char'}
Aresult
11
00
53 31 ▶ my $quote_value = $args{quote_value} || '';
Condition100%2/2
$args{'quote_value'} || ''
l!l
229
MC/DC100%1/1
$args{'quote_value'} || ''
$args{'quote_value'}
Truth table100%2/2
A
A= $args{'quote_value'}
Aresult
11
00
54 31 my $obj = $self->node;
55  
56 31 my %force_lc;
57 31 ▶ map { $force_lc{$_} = $args{"force_lc_$_"} ? 1 : 0; } qw/section key value/;
Branch: 1 branch
BranchTrueFalseTotal
$args{"force_lc_$_"} ? : 7 86 93
58  
59 #FIXME: Is it possible to store the comments with their location
60 #in the file? It would be nice if comments that are after values
61 #in input file, would be written in the same way in the output
62 #file. Also, comments at the end of file are being ignored now.
63  
64 31 my @lines = $args{file_path}->lines_utf8;
65  
66 # try to get global comments (comments before a blank line)
67 31 $self->read_global_comments( \@lines, $delimiter );
68  
69 31 my @assoc = $self->associates_comments_with_data( \@lines, $delimiter );
70  
71 # store INI data in a structure:
72 # {
73 # name => value leaf
74 # name => [ value ] list
75 # name => { key => value , ... } hash
76 # name => { ... } node
77 # name => [ { ... }, ... ] list of nodes
78 # name => { key => { ... } , ... } hash of nodes
79 # }
80  
81 31 my $ini_data = {};
82 31 my %ini_comment;
83 31 my $section_ref = $ini_data;
84 31 my $section_path = '';
85  
86 31 foreach my $item (@assoc) {
87 160 my ( $vdata, $comment ) = @$item;
88 160 $logger->debug("ini read: reading '$vdata'");
89 160 my $comment_path;
90  
91 # Update section name
92 160 ▶ if ( $vdata =~ /^\s*\[(.*)\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vdata =~ /^\s*\[(.*)\]/) { } 31 129 160
93 31 ▶ $section = $force_lc{section} ? lc($1) : $1;
Branch: 1 branch
BranchTrueFalseTotal
$force_lc{'section'} ? : 15 16 31
94 31 ▶ my $remap = $section_map->{$section} || '';
Condition100%2/2
$section_map->{$section} || ''
l!l
427
MC/DC100%1/1
$section_map->{$section} || ''
$section_map->{$section}
Truth table100%2/2
A
A= $section_map->{$section}
Aresult
11
00
95 31 ▶ if ( $remap eq '!' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($remap eq '!') { } 2 29 31
elsif ($remap) { } 2 27 29
elsif ($hash_class) { } 12 15 27
96 # section_map maps section to root node
97 2 $section_ref = $ini_data;
98 2 $comment_path = $section_path = '';
99 2 $logger->debug("step 1: found node <top> [$section]");
100 }
101 elsif ($remap) {
102 # section_map maps section to some node
103 2 $section_ref = {};
104 2 $logger->debug("step 1: found node $remap [$section]");
105 2 $section_path = $comment_path =
106 $self->set_or_push( $ini_data, $remap, $section_ref );
107 }
108 elsif ($hash_class) {
109 12 $ini_data->{$hash_class}{$section} = $section_ref = {};
110 12 $comment_path = $section_path = "$hash_class:$section";
111 12 $logger->debug("step 1: found node $hash_class and path $comment_path [$section]");
112 }
113 else {
114 15 $section_ref = {};
115 15 $logger->debug("step 1: found node $section [$section]");
116 15 $section_path = $comment_path =
117 $self->set_or_push( $ini_data, $section, $section_ref );
118 }
119  
120 # for write later, need to store the obj if section map was used
121 31 ▶ if ( defined $section_map->{$section} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $section_map->{$section}) 4 27 31
122 4 $logger->debug("store section_map loc '$section_path' section '$section'");
123 4 $self->{reverse_section_map}{$section_path} = $section;
124 }
125 }
126 else {
127 129 my ( $name, $val ) = split( /\s*$assign_char\s*/, $vdata, 2 );
128 129 ▶ $name = lc($name) if $force_lc{key};
Branch: 1 branch
BranchTrueFalseTotal
if $force_lc{"key"} 2 127 129
129 129 ▶ $val = lc($val) if $force_lc{value};
Branch: 1 branch
BranchTrueFalseTotal
if $force_lc{"value"} 2 127 129
130 129 ▶ $val =~ s/"([^"]*)"/$1/g if $quote_value eq "shell_style";
Branch: 1 branch
BranchTrueFalseTotal
if $quote_value eq "shell_style" 5 124 129
131 129 $comment_path = $section_path . ' ' . $self->set_or_push( $section_ref, $name, $val );
132 129 $logger->debug("step 1: found node $comment_path name $name in [$section]");
133 }
134  
135 160 ▶ $ini_comment{$comment_path} = $comment if $comment;
Branch: 1 branch
BranchTrueFalseTotal
if $comment 79 81 160
136 }
137  
138 31 my @load_args = ( data => $ini_data, check => $check );
139 31 ▶ push @load_args, split_reg => qr/$split_reg/ if $split_reg;
Branch: 1 branch
BranchTrueFalseTotal
if $split_reg 4 27 31
140 31 $self->load_data(@load_args);
141  
142 31 ▶ while ( my ( $k, $v ) = each %ini_comment ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ini_comment) 79 31 110
143 79 ▶ my $item = $obj->grab( step => $k, mode => 'loose' ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $item = $obj->grab("step", $k, "mode", "loose") or next 72 7 79
144 72 ▶ $item = $item->fetch_with_id(0) if $item->get_type eq 'list';
Branch: 1 branch
BranchTrueFalseTotal
if $item->get_type eq "list" 24 48 72
145 72 $logger->debug("annotate '$v' on ", $item->location);
146 72 $item->annotation($v);
147 }
148  
149 31 return 1;
150 }
151  
152 146 ▶sub set_or_push {
Subroutine set_or_push: called CC=4 SCAR=13.9
Pod undocumented
153 146 my ( $self, $ref, $name, $val ) = @_;
154 146 my $cell = $ref->{$name};
155 146 my $path;
156 146 ▶ if ( defined $cell and ref($cell) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $cell and ref $cell eq 'ARRAY') { } 13 133 146
elsif (defined $cell) { } 27 106 133
Condition100%3/3
defined $cell and ref $cell eq "ARRAY"
!ll&&!rl&&r
1062713
MC/DC100%2/2
defined $cell and ref $cell eq "ARRAY"
defined $cell ref $cell eq "ARRAY"
Truth table100%3/3
A and B
A= defined $cellB= ref $cell eq "ARRAY"
ABresult
0X0
100
111
157 13 push @$cell, $val;
158 13 $path = $name . ':' . $#$cell;
159 }
160 elsif ( defined $cell ) {
161 27 $ref->{$name} = [ $cell, $val ];
162 27 $path = $name . ':1';
163 }
164 else {
165 106 $ref->{$name} = $val;
166 106 $path = $name; # no way to distinguish between leaf and first value of list
167 }
168 146 return $path;
169 }
170  
171 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
172 19 ▶sub write ($self, %args) {
Subroutine write: called CC=6 SCAR=17.9
Pod documented
173 # args is:
174 # object => $obj, # Config::Model::Node object
175 # root => './my_test', # fake root directory, userd for tests
176 # config_dir => /etc/foo', # absolute path
177 # file => 'foo.conf', # file name
178 # file_path => './my_test/etc/foo/foo.conf'
179 # check => yes|no|skip
180  
181 19 my $node = $args{object};
182 19 ▶ my $delimiter = $args{comment_delimiter} || '#';
Condition100%2/2
$args{'comment_delimiter'} || '#'
l!l
217
MC/DC100%1/1
$args{'comment_delimiter'} || '#'
$args{'comment_delimiter'}
Truth table100%2/2
A
A= $args{'comment_delimiter'}
Aresult
11
00
183  
184 19 ▶ croak "Undefined file handle to write" unless defined $args{file_path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"file_path"} 0 19 19
185  
186 # use the first char of the list as a comment delimeter
187 19 my $cc = substr($delimiter,0,1);
188 19 $args{comment_delimiter} = $cc;
189  
190 19 my $res = '';
191  
192 # some INI file have a 'General' section mapped in root node
193 19 my $top_class_name = $self->{reverse_section_map}{''};
194 19 ▶ if ( defined $top_class_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $top_class_name) 1 18 19
195 1 $logger->debug("writing class $top_class_name from reverse_section_map");
196 1 $res .= $self->write_data_and_comments( $cc, "[$top_class_name]" );
197 }
198  
199 19 $res .= $self->_write(%args);
200 19 ▶ if ($res) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res) { } 17 2 19
elsif ($self->auto_delete) { } 2 0 2
201 17 $args{file_path}->spew_utf8($self->write_global_comment( $cc ) . $res);
202 }
203 elsif ($self->auto_delete) {
204 2 $args{file_path}->remove;
205 }
206 19 return;
207 }
208  
209 52 ▶sub _write_list{
Subroutine _write_list: called CC=8 SCAR=21.2
210 52 my ($self, $args, $node, $elt) = @_ ;
211 52 my $res = '';
212  
213 52 my $join_list = $args->{join_list_value};
214 52 ▶ my $delimiter = $args->{comment_delimiter} || '#';
Condition50%1/2
$args->{'comment_delimiter'} || '#'
l!l
520
MC/DC0%0/1
$args->{'comment_delimiter'} || '#'
$args->{'comment_delimiter'}
Truth table50%1/2
A
A= $args->{'comment_delimiter'}
Aresult
11
00
215 52 ▶ my $assign_with = $args->{assign_with} // $args->{assign_char} // ' = ';
Condition40%2/5
($args->{"assign_with"} // $args->{"assign_char"}) // " = "
l!l
052
$args->{"assign_with"} // $args->{"assign_char"}
l!l&&r!l&&!r
0052
MC/DC0%0/2
($args->{"assign_with"} // $args->{"assign_char"}) // " = "
$args->{"assign_with"} $args->{"assign_char"}
Truth table33%1/3
A // B
A= $args->{"assign_with"}B= $args->{"assign_char"}
ABresult
1X1
011
000
216 52 my $list_obj = $node->fetch_element($elt);
217  
218 52 my $list_obj_note = $list_obj->annotation;
219  
220 52 ▶ if ( $join_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($join_list) { } 6 46 52
221 6 my @v = grep { length } $list_obj->fetch_all_values();
222 6 my $v = join( $join_list, @v );
223 6 ▶ if ( length($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v) 2 4 6
224 2 $logger->debug("writing joined list elt $elt -> $v");
225 2 $res .= $self->write_data_and_comments( $delimiter, "$elt$assign_with$v", $list_obj_note );
226 }
227 }
228 else {
229 46 foreach my $obj ( $list_obj->fetch_all('custom') ) {
230 37 my $note = $obj->annotation;
231 37 my $v = $self->_fetch_obj_value($args, $obj);
232 37 ▶ if ( length $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v) { } 37 0 37
233 37 $logger->debug("writing list elt $elt -> $v");
234 37 $res .=
235 $self->write_data_and_comments( $delimiter, "$elt$assign_with$v",
236 $list_obj_note . $note );
237 }
238 else {
239 0 $logger->trace("NOT writing undef or empty list elt");
240 }
241 }
242 }
243 52 return $res;
244 }
245  
246 4 ▶sub _write_check_list{
Subroutine _write_check_list: called CC=7 SCAR=23.3
247 4 my ($self, $args, $node, $elt) = @_ ;
248 4 my $res = '';
249  
250 4 my $join_check_list = $args->{join_check_list_value};
251 4 ▶ my $delimiter = $args->{comment_delimiter} || '#';
Condition50%1/2
$args->{'comment_delimiter'} || '#'
l!l
40
MC/DC0%0/1
$args->{'comment_delimiter'} || '#'
$args->{'comment_delimiter'}
Truth table50%1/2
A
A= $args->{'comment_delimiter'}
Aresult
11
00
252 4 ▶ my $assign_with = $args->{assign_with} // $args->{assign_char} // ' = ';
Condition40%2/5
($args->{"assign_with"} // $args->{"assign_char"}) // " = "
l!l
04
$args->{"assign_with"} // $args->{"assign_char"}
l!l&&r!l&&!r
004
MC/DC0%0/2
($args->{"assign_with"} // $args->{"assign_char"}) // " = "
$args->{"assign_with"} $args->{"assign_char"}
Truth table33%1/3
A // B
A= $args->{"assign_with"}B= $args->{"assign_char"}
ABresult
1X1
011
000
253 4 my $obj = $node->fetch_element($elt);
254  
255 4 my $obj_note = $obj->annotation;
256  
257 4 ▶ if ($join_check_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($join_check_list) { } 0 4 4
258 0 my $v = join( $join_check_list, $obj->get_checked_list() );
259 0 ▶ if ( length($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v) 0 0 0
260 0 $logger->debug("writing check_list elt $elt -> $v");
261 0 $res .= $self->write_data_and_comments( $delimiter, "$elt$assign_with$v", $obj_note );
262 }
263 }
264 else {
265 4 foreach my $v ( $obj->get_checked_list() ) {
266 7 $logger->debug("writing joined check_list elt $elt -> $v");
267 7 $res .= $self->write_data_and_comments( $delimiter, "$elt$assign_with$v", $obj_note );
268 }
269 }
270 4 return $res;
271 }
272  
273 63 ▶sub _fetch_obj_value {
Subroutine _fetch_obj_value: called CC=5 SCAR=16.2
274 63 my ($self, $args, $obj) = @_ ;
275 63 my $v = $obj->fetch;
276 63 ▶ if ( defined $args->{quote_value}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"quote_value"} and $args->{"quote_value"} eq "shell_style" and defined $v and $v =~ /\s/) 4 59 63
Condition77%7/9
defined $args->{"quote_value"} and $args->{"quote_value"} eq "shell_style" and defined $v and $v =~ /\s/
!ll&&!rl&&r
5814
defined $args->{"quote_value"} and $args->{"quote_value"} eq "shell_style" and defined $v
!ll&&!rl&&r
5805
defined $args->{"quote_value"} and $args->{"quote_value"} eq "shell_style"
!ll&&!rl&&r
5805
MC/DC50%2/4
defined $args->{"quote_value"} and $args->{"quote_value"} eq "shell_style" and defined $v and $v =~ /\s/
defined $args->{"quote_value"} $args->{"quote_value"} eq "shell_style" defined $v $v =~ /\s/
Truth table60%3/5
A and B and C and D
A= defined $args->{"quote_value"}B= $args->{"quote_value"} eq "shell_style"C= defined $vD= $v =~ /\s/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
277 and $args->{quote_value} eq 'shell_style'
278 and defined $v
279 and $v =~ /\s/
280 ) {
281 4 $v = qq!"$v"!;
282 }
283 63 return $v;
284 }
285  
286 26 ▶sub _write_leaf {
Subroutine _write_leaf: called CC=10 SCAR=25.2
287 26 my ($self, $args, $node, $elt) = @_ ;
288 26 my $res = '';
289  
290 26 my $write_bool_as = $args->{write_boolean_as};
291 26 ▶ my $delimiter = $args->{comment_delimiter} || '#';
Condition50%1/2
$args->{'comment_delimiter'} || '#'
l!l
260
MC/DC0%0/1
$args->{'comment_delimiter'} || '#'
$args->{'comment_delimiter'}
Truth table50%1/2
A
A= $args->{'comment_delimiter'}
Aresult
11
00
292 26 ▶ my $assign_with = $args->{assign_with} // $args->{assign_char} // ' = ';
Condition80%4/5
($args->{"assign_with"} // $args->{"assign_char"}) // " = "
l!l
224
$args->{"assign_with"} // $args->{"assign_char"}
l!l&&r!l&&!r
2024
MC/DC50%1/2
($args->{"assign_with"} // $args->{"assign_char"}) // " = "
$args->{"assign_with"} $args->{"assign_char"}
Truth table66%2/3
A // B
A= $args->{"assign_with"}B= $args->{"assign_char"}
ABresult
1X1
011
000
293 26 my $obj = $node->fetch_element($elt);
294  
295 26 my $obj_note = $obj->annotation;
296  
297 26 my $v = $self->_fetch_obj_value($args, $obj);
298 26 ▶ if ( $write_bool_as and defined($v) and length($v) and $obj->value_type eq 'boolean' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_bool_as and defined $v and length $v and $obj->value_type eq "boolean") 0 26 26
Condition33%3/9
$write_bool_as and defined $v and length $v and $obj->value_type eq "boolean"
!ll&&!rl&&r
2600
$write_bool_as and defined $v and length $v
!ll&&!rl&&r
2600
$write_bool_as and defined $v
!ll&&!rl&&r
2600
MC/DC0%0/4
$write_bool_as and defined $v and length $v and $obj->value_type eq "boolean"
$write_bool_as defined $v length $v $obj->value_type eq "boolean"
Truth table20%1/5
A and B and C and D
A= $write_bool_asB= defined $vC= length $vD= $obj->value_type eq "boolean"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
299 0 $v = $write_bool_as->[$v];
300 }
301 26 ▶ if ( defined $v and length $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and length $v) { } 21 5 26
Condition66%2/3
defined $v and length $v
!ll&&!rl&&r
5021
MC/DC50%1/2
defined $v and length $v
defined $v length $v
Truth table66%2/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
302 21 $logger->debug("writing leaf elt $elt -> $v");
303 21 $res .= $self->write_data_and_comments( $delimiter, "$elt$assign_with$v", $obj_note );
304 }
305 else {
306 5 $logger->trace("NOT writing undef or empty leaf elt");
307 }
308 26 return $res;
309 }
310  
311 3 ▶sub _write_hash {
Subroutine _write_hash: called CC=4 SCAR=13.9
312 3 my ($self, $args, $node, $elt) = @_ ;
313 3 my $res = '';
314  
315 3 ▶ my $delimiter = $args->{comment_delimiter} || '#';
Condition50%1/2
$args->{'comment_delimiter'} || '#'
l!l
30
MC/DC0%0/1
$args->{'comment_delimiter'} || '#'
$args->{'comment_delimiter'}
Truth table50%1/2
A
A= $args->{'comment_delimiter'}
Aresult
11
00
316 3 my $obj = $node->fetch_element($elt);
317 3 my $obj_note = $obj->annotation;
318  
319 3 foreach my $key ( $obj->fetch_all_indexes ) {
320 7 my $hash_obj = $obj->fetch_with_id($key);
321 7 my $note = $hash_obj->annotation;
322 7 $logger->debug("writing hash elt $elt key $key");
323 7 my $subres = $self->_write( %$args, object => $hash_obj );
324 7 ▶ if ($subres) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subres) 5 2 7
325 5 $res .= "\n"
326 . $self->write_data_and_comments( $delimiter, "[$key]",
327 $obj_note . $note )
328 . $subres;
329 }
330 }
331 3 return $res;
332 }
333  
334 17 ▶sub _write_node {
Subroutine _write_node: called CC=5 SCAR=16.1
335 17 my ($self, $args, $node, $elt) = @_ ;
336 17 my $res = '';
337  
338 17 ▶ my $delimiter = $args->{comment_delimiter} || '#';
Condition50%1/2
$args->{'comment_delimiter'} || '#'
l!l
170
MC/DC0%0/1
$args->{'comment_delimiter'} || '#'
$args->{'comment_delimiter'}
Truth table50%1/2
A
A= $args->{'comment_delimiter'}
Aresult
11
00
339 17 my $obj = $node->fetch_element($elt);
340 17 my $obj_note = $obj->annotation;
341  
342 17 $logger->debug("writing class $elt");
343 17 my $subres = $self->_write( %$args, object => $obj );
344 17 ▶ if ($subres) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subres) 8 9 17
345  
346 # some INI file may have a section mapped to a node as exception to mapped in a hash
347 8 my $exception_name = $self->{reverse_section_map}{ $obj->location };
348 8 ▶ if ( defined $exception_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exception_name) 1 7 8
349 1 $logger->debug("writing class $exception_name from reverse_section_map");
350 }
351 8 ▶ my $c_name = $exception_name || $elt;
Condition66%2/3
$exception_name || $elt
l!l&&r!l&&!r
170
MC/DC0%0/2
$exception_name || $elt
$exception_name $elt
Truth table66%2/3
A || B
A= $exception_nameB= $elt
ABresult
1X1
011
000
352 8 $res .= "\n"
353 . $self->write_data_and_comments( $delimiter, "[$c_name]", $obj_note )
354 . $subres;
355 }
356 17 return $res;
357 }
358  
359 43 ▶sub _write ($self, %args) {
Subroutine _write: called CC=12 SCAR=24.9
360 43 my $node = $args{object};
361 43 ▶ my $delimiter = $args{comment_delimiter} || '#';
Condition50%1/2
$args{'comment_delimiter'} || '#'
l!l
430
MC/DC0%0/1
$args{'comment_delimiter'} || '#'
$args{'comment_delimiter'}
Truth table50%1/2
A
A= $args{'comment_delimiter'}
Aresult
11
00
362  
363 43 $logger->trace( "called on ", $node->name );
364 43 my $res = '';
365  
366 # Using Config::Model::ObjTreeScanner would be overkill
367 # first write list and element, then classes
368 43 foreach my $elt ( $node->get_element_name ) {
369 102 my $type = $node->element_type($elt);
370 102 $logger->trace("first loop on elt $elt type $type");
371 102 ▶ next if $type =~ /node/ or $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /node/ or $type eq "hash" 20 82 102
Condition100%3/3
$type =~ /node/ or $type eq "hash"
l!l&&r!l&&!r
17382
MC/DC100%2/2
$type =~ /node/ or $type eq "hash"
$type =~ /node/ $type eq "hash"
Truth table100%3/3
A or B
A= $type =~ /node/B= $type eq "hash"
ABresult
1X1
011
000
372  
373 82 ▶ if ( $type eq 'list' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'list') { } 52 30 82
elsif ($type eq 'check_list') { } 4 26 30
elsif ($type eq 'leaf') { } 26 0 26
374 52 $res .= $self->_write_list (\%args, $node, $elt) ;
375 }
376 elsif ( $type eq 'check_list') {
377 4 $res .= $self->_write_check_list (\%args, $node, $elt) ;
378 }
379 elsif ( $type eq 'leaf' ) {
380 26 $res .= $self->_write_leaf (\%args, $node, $elt) ;
381 }
382 else {
383 0 Config::Model::Exception::Model->throw(
384 error => "unexpected type $type for leaf elt $elt",
385 object => $node
386 );
387 }
388 }
389  
390 43 foreach my $elt ( $node->get_element_name ) {
391 102 my $type = $node->element_type($elt);
392 102 $logger->trace("second loop on elt $elt type $type");
393 102 ▶ next unless $type =~ /node/ or $type eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /node/ or $type eq "hash" 82 20 102
Condition100%3/3
$type =~ /node/ or $type eq "hash"
l!l&&r!l&&!r
17382
MC/DC100%2/2
$type =~ /node/ or $type eq "hash"
$type =~ /node/ $type eq "hash"
Truth table100%3/3
A or B
A= $type =~ /node/B= $type eq "hash"
ABresult
1X1
011
000
394 20 my $obj = $node->fetch_element($elt);
395  
396 20 my $obj_note = $obj->annotation;
397  
398 20 ▶ if ( $type eq 'hash' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'hash') { } 3 17 20
399 3 $res .= $self->_write_hash (\%args, $node, $elt) ;
400 }
401 else {
402 17 $res .= $self->_write_node (\%args, $node, $elt) ;
403 }
404 }
405  
406 43 $logger->trace( "done on ", $node->name );
407  
408 43 return $res;
409 }
410  
411 5 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
412 5 __PACKAGE__->meta->make_immutable;
413  
414 5 1;
415  
416 # ABSTRACT: Read and write config as a INI file
417  
418 __END__
« blib/lib/Config/Model.pm ↑ Summary blib/lib/Config/Model/AnyId.pm »