blib/lib/Config/Model/AnyId.pm

filter: Statementstmt 87.7% 407 / 464 Branchbran 74.7% 136 / 182 Conditioncond 70.4% 69 / 98 MC/DCmcdc 46.5% 27 / 58 Subroutinesub 85.7% 60 / 70 Podpod 58.3% 28 / 48 totaltotal 79.0% 727 / 920 CC 147 SCAR 24.5
CC
147
Cov
82%
handle_args
30.0
error_msg
30.0
check_idx
24.6
CRAP 11.6
« blib/lib/Config/Model/Backend/IniFile.pm ↑ Summary blib/lib/Config/Model/Node.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::AnyId 2.169;
11  
12 39 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 39 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 39 with "Config::Model::Role::NodeLoader";
16 39 with "Config::Model::Role::Utils";
17  
18 39 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 39 ▶use Config::Model::Warper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 39 ▶use Carp qw/cluck croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 39 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 39 ▶use Storable qw/dclone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 39 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 39 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 39 extends qw/Config::Model::AnyThing/;
27  
28 39 ▶use feature qw/signatures postderef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 39 ▶no warnings qw/experimental::signatures experimental::postderef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 39 subtype 'KeyArray' => as 'ArrayRef' ;
32 0 ▶coerce 'KeyArray' => from 'Str' => via { [$_] } ;
Subroutine __ANON__: not called CC=1 SCAR=1.2
33  
34 39 my $logger = get_logger("Tree::Element::Id");
35 39 my $user_logger = get_logger("User");
36 39 my $deep_check_logger = get_logger('DeepCheck');
37 39 my $fix_logger = get_logger("Anything::Fix");
38 39 my $change_logger = get_logger("ChangeTracker");
39  
40 39 enum 'DataMode' => [qw/preset layered normal/];
41  
42 has data_mode => (
43 is => 'rw',
44 isa => 'HashRef[DataMode]',
45 traits => ['Hash'],
46 handles => {
47 get_data_mode => 'get',
48 set_data_mode => 'set',
49 delete_data_mode => 'delete',
50 clear_data_mode => 'clear',
51 },
52 537 ▶ default => sub { {}; }
Subroutine __ANON__: called CC=1 SCAR=0.0
53 39 );
54  
55 # this is cleared and set by set_properties
56 has _warpable_check_content_actions => (
57 is => 'bare', # no direct accessor
58 isa => 'ArrayRef[CodeRef]',
59 traits => ['Array'],
60 handles => {
61 add_warpable_check_content => 'push',
62 clear_warpable_check_content => 'clear',
63 get_all_warpable_content_checks => 'elements',
64 },
65 537 ▶ default => sub { []; }
Subroutine __ANON__: called CC=1 SCAR=0.0
66 39 );
67  
68 has _check_content_actions => (
69 is => 'bare', # no direct accessor
70 isa => 'ArrayRef[CodeRef]',
71 traits => ['Array'],
72 handles => {
73 add_check_content => 'push',
74 get_all_content_checks => 'elements',
75 },
76 537 ▶ default => sub { []; }
Subroutine __ANON__: called CC=1 SCAR=0.0
77 39 );
78  
79 # needs_content_check defaults to 1 to trap bad data right after loading
80 39 has needs_content_check => ( is => 'rw', isa => 'Bool', default => 1 );
81  
82 39 has has_fixes => (
83 is => 'ro',
84 isa => 'Num',
85 default => 0,
86 traits => ['Number'],
87 handles => {
88 inc_fixes => [ add => 1 ],
89 dec_fixes => [ sub => 1 ],
90 add_fixes => 'add',
91 flush_fixes => [ mul => 0 ],
92 }
93 );
94  
95 # Some idea for improvement
96  
97 # suggest => 'foo' or '$bar foo'
98 # creates a method analog to next_id (or next_id but I need to change
99 # run_user_command) that suggest the next id as foo_<nb> where
100 # nb is incremented each time, or compute the passed formula
101 # and performs the same
102  
103 39 my @common_int_params = qw/min_index max_index max_nb auto_create_ids/;
104 39 has \@common_int_params => ( is => 'ro', isa => 'Maybe[Int]' );
105  
106 39 my @common_list_params = qw/allow_keys default_keys auto_create_keys/;
107 has \@common_list_params => (
108 is => 'ro',
109 isa => 'KeyArray',
110 coerce => 1,
111 1116 ▶ default => sub { []; }
Subroutine __ANON__: called CC=1 SCAR=0.0
112 39 );
113  
114 39 my @common_str_params = qw/allow_keys_from allow_keys_matching follow_keys_from
115 migrate_keys_from migrate_values_from
116 duplicates warn_if_key_match warn_unless_key_match/;
117 39 has \@common_str_params => ( is => 'ro', isa => 'Maybe[Str]' );
118  
119 39 my @common_params =
120 ( @common_int_params, @common_str_params, @common_list_params );
121 39 my @allowed_warp_params = ( @common_params, qw/level convert/ );
122  
123 537 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 537 my $orig = shift;
125 537 my $class = shift;
126 537 my %args = @_;
127 537 my %h = map { ( $_ => $args{$_} ); } grep { defined $args{$_} } @allowed_warp_params;
128 537 return $class->$orig( backup => dclone( \%h ), @_ );
129 39 };
130  
131 39 has [qw/cargo/] => ( is => 'ro', isa => 'HashRef', required => 1 );
132 39 has warp => ( is => 'ro', isa => 'Maybe[HashRef]' );
133 39 has [qw/morph/] => ( is => 'ro', isa => 'Bool', default => 0 );
134 537 ▶has content_warning_list => ( is => 'rw', isa => 'ArrayRef', default => sub { []; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
135 39 has [qw/cargo_class max_index index_class index_type/] =>
136 ( is => 'rw', isa => 'Maybe[Str]' );
137  
138 39 has config_model => (
139 is => 'ro',
140 isa => 'Config::Model',
141 weak_ref => 1,
142 lazy => 1,
143 builder => '_config_model'
144 );
145  
146 114 ▶sub _config_model {
Subroutine _config_model: called CC=1 SCAR=0.0
147 114 my $self = shift;
148 114 return $self->instance->config_model;
149 }
150  
151 310 ▶sub config_class_name {
Subroutine config_class_name: called CC=1 SCAR=0.0
Pod documented
152 310 my $self = shift;
153 310 return $self->cargo->{config_class_name};
154 }
155  
156 # used by roles
157 570 ▶sub allowed_warp_params {
Subroutine allowed_warp_params: called CC=1 SCAR=0.0
Pod undocumented
158 570 return @allowed_warp_params;
159 }
160  
161 537 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=26.9
Pod documented
162 537 my $self = shift;
163  
164 croak "Missing cargo->type parameter for element " . $self->{element_name} || 'unknown'
165 537 ▶ unless defined $self->cargo->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->cargo->{"type"} 0 537 537
Condition0%0/2
'Missing cargo->type parameter for element ' . $self->{'element_name'} || 'unknown'
l!l
00
MC/DC0%0/1
'Missing cargo->type parameter for element ' . $self->{'element_name'} || 'unknown'
'Missing cargo->type parameter for element ' . $self->{'element_name'}
Truth table0%0/2
A
A= 'Missing cargo->type parameter for element ' . $self->{'element_name'}
Aresult
11
00
166  
167 537 ▶ if ( $self->cargo->{type} eq 'node' and not $self->cargo->{config_class_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cargo->{"type"} eq "node" and not $self->cargo->{"config_class_name"}) 0 537 537
Condition66%2/3
$self->cargo->{"type"} eq "node" and not $self->cargo->{"config_class_name"}
!ll&&!rl&&r
3701670
MC/DC0%0/2
$self->cargo->{"type"} eq "node" and not $self->cargo->{"config_class_name"}
$self->cargo->{"type"} eq "node" not $self->cargo->{"config_class_name"}
Truth table66%2/3
A and not(B)
A= $self->cargo->{"type"} eq "node"B= not $self->cargo->{"config_class_name"}
ABresult
0X0
100
111
168 0 ▶ croak "Missing cargo->config_class_name parameter for element "
Condition0%0/2
'Missing cargo->config_class_name parameter for element ' . $self->element_name || 'unknown'
l!l
00
MC/DC0%0/1
'Missing cargo->config_class_name parameter for element ' . $self->element_name || 'unknown'
'Missing cargo->config_class_name parameter for element ' . $self->element_name
Truth table0%0/2
A
A= 'Missing cargo->config_class_name parameter for element ' . $self->element_name
Aresult
11
00
169 . $self->element_name || 'unknown';
170 }
171  
172 537 ▶ if ( $self->{cargo}{type} eq 'hash' or $self->{cargo}{type} eq 'list' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cargo"}{"type"} eq "hash" or $self->{"cargo"}{"type"} eq "list") 0 537 537
Condition33%1/3
$self->{"cargo"}{"type"} eq "hash" or $self->{"cargo"}{"type"} eq "list"
l!l&&r!l&&!r
00537
MC/DC0%0/2
$self->{"cargo"}{"type"} eq "hash" or $self->{"cargo"}{"type"} eq "list"
$self->{"cargo"}{"type"} eq "hash" $self->{"cargo"}{"type"} eq "list"
Truth table33%1/3
A or B
A= $self->{"cargo"}{"type"} eq "hash"B= $self->{"cargo"}{"type"} eq "list"
ABresult
1X1
011
000
173 0 die "$self->{element_name}: using $self->{cargo}{type} will probably not work";
174 }
175  
176 537 $self->set_properties();
177  
178 534 ▶ if ( defined $self->warp ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->warp) 6 528 534
179 $self->{warper} = Config::Model::Warper->new(
180 warped_object => $self,
181 6 %{ $self->warp },
182 allowed => \@allowed_warp_params
183 );
184 }
185  
186 534 return $self;
187 }
188  
189 # this method can be called by the warp mechanism to alter (warp) the
190 # feature of the Id object.
191 570 ▶sub set_properties ($self, %raw_args) {
Subroutine set_properties: called CC=21 SCAR=30.6
Pod undocumented
192 # mega cleanup
193 570 for ( @allowed_warp_params ) { delete $self->{$_}; }
194  
195 # merge data passed to set_properties with data passed to the constructor
196 570 my %args = $self->merge_properties(%raw_args);
197  
198 # these are handled by Node or Warper
199 570 for ( qw/level/ ) { delete $args{$_}; }
200  
201 570 $logger->trace( $self->name, " set_properties called with %raw_args" );
202  
203 570 for ( @common_params ) {
204 8550 ▶ $self->{$_} = delete $args{$_} if defined $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$_} 89 8461 8550
205 }
206  
207 570 ▶ $self->set_convert( \%args ) if defined $args{convert};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"convert"} 1 569 570
208  
209 570 $self-> clear_warpable_check_content;
210 570 for ( $self-> get_all_content_checks ) {
211 0 $self-> add_warpable_check_content($_);
212 }
213 570 for ( qw/duplicates/ ) {
214 570 my $method = "check_$_";
215 570 my $weak_self = $self;
216 570 weaken($weak_self); # weaken reference loop ($self - check_content - closure - self)
217 570 ▶ $self-> add_check_content( sub { $weak_self->$method(@_);} ) if $self->{$_};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} 7 563 570
218 }
219  
220 Config::Model::Exception::Model->throw(
221 object => $self,
222 error => "Undefined index_type"
223 570 ▶ ) unless defined $self->{index_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"index_type"} 0 570 570
224  
225 Config::Model::Exception::Model->throw(
226 object => $self,
227 error => "Unexpected index_type $self->{index_type}"
228 )
229 unless ( $self->{index_type} eq 'integer'
230 570 ▶ or $self->{index_type} eq 'string' );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"index_type"} eq "integer" or $self->{"index_type"} eq "string" 0 570 570
Condition66%2/3
$self->{"index_type"} eq "integer" or $self->{"index_type"} eq "string"
l!l&&r!l&&!r
3112590
MC/DC0%0/2
$self->{"index_type"} eq "integer" or $self->{"index_type"} eq "string"
$self->{"index_type"} eq "integer" $self->{"index_type"} eq "string"
Truth table66%2/3
A or B
A= $self->{"index_type"} eq "integer"B= $self->{"index_type"} eq "string"
ABresult
1X1
011
000
231  
232 570 my @current_idx = $self->_fetch_all_indexes();
233 570 ▶ if (@current_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (@current_idx) 15 555 570
234 15 my $first_idx = shift @current_idx;
235 15 my $last_idx = pop @current_idx;
236  
237 15 foreach my $idx ( ( $first_idx, $last_idx ) ) {
238 29 my $ok = $self->check_idx($first_idx);
239 29 ▶ next if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 28 1 29
240  
241 # here a user input may trigger an exception even if fetch
242 # or set value check is disabled. That's mostly because,
243 # we cannot enforce more strict settings without random
244 # deletion of data. For instance, if a hash contains 5
245 # items and the max_nb of items is reduced to 3. Which 2
246 # items should we remove ?
247  
248 # Since we cannot choose, we must raise an exception in
249 # all cases.
250 Config::Model::Exception::WrongValue->throw(
251 error => "Error while setting id property:"
252 1 . join( "\n\t", @{ $self->{idx_error_list} } ),
253 object => $self
254 );
255 }
256 }
257  
258 569 $self->auto_create_elements;
259  
260 568 ▶ if ( defined $self->{duplicates}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"duplicates"} and defined $self->{"cargo"} and $self->{"cargo"}{"type"} ne "leaf") 1 567 568
Condition83%5/6
defined $self->{"duplicates"} and defined $self->{"cargo"} and $self->{"cargo"}{"type"} ne "leaf"
!ll&&!rl&&r
56161
defined $self->{"duplicates"} and defined $self->{"cargo"}
!ll&&!rl&&r
56107
MC/DC66%2/3
defined $self->{"duplicates"} and defined $self->{"cargo"} and $self->{"cargo"}{"type"} ne "leaf"
defined $self->{"duplicates"} defined $self->{"cargo"} $self->{"cargo"}{"type"} ne "leaf"
Truth table75%3/4
A and B and C
A= defined $self->{"duplicates"}B= defined $self->{"cargo"}C= $self->{"cargo"}{"type"} ne "leaf"
ABCresult
0XX0
10X0
1100
1111
261 and defined $self->{cargo}
262 and $self->{cargo}{type} ne 'leaf' ) {
263 1 Config::Model::Exception::Model->throw(
264 object => $self,
265 error => "Cannot specify 'duplicates' with cargo type '$self->{cargo}{type}'",
266 );
267 }
268  
269 567 my $ok_dup = 'forbid|suppress|warn|allow';
270 567 ▶ if ( defined $self->{duplicates} and $self->{duplicates} !~ /^$ok_dup$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"duplicates"} and not $self->{"duplicates"} =~ /^$ok_dup$/u) 1 566 567
Condition100%3/3
defined $self->{"duplicates"} and not $self->{"duplicates"} =~ /^$ok_dup$/u
!ll&&!rl&&r
56151
MC/DC100%2/2
defined $self->{"duplicates"} and not $self->{"duplicates"} =~ /^$ok_dup$/u
defined $self->{"duplicates"} not $self->{"duplicates"} =~ /^$ok_dup$/u
Truth table100%3/3
A and not(B)
A= defined $self->{"duplicates"}B= not $self->{"duplicates"} =~ /^$ok_dup$/u
ABresult
0X0
100
111
271 1 Config::Model::Exception::Model->throw(
272 object => $self,
273 error => "Unexpected 'duplicates' $self->{duplicates} expected $ok_dup",
274 );
275 }
276  
277 Config::Model::Exception::Model->throw(
278 566 ▶ object => $self,
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args 0 566 566
279 error => "Unexpected parameters: " . join( ' ', keys %args )
280 ) if scalar keys %args;
281  
282 566 return;
283 }
284  
285 0 ▶sub max {
Subroutine max: not called CC=1 SCAR=6.9
Pod undocumented
286 0 my $self = shift;
287 0 carp $self->name, ": max param is deprecated, use max_index\n";
288 0 return $self->max_index;
289 }
290  
291 0 ▶sub min {
Subroutine min: not called CC=1 SCAR=6.9
Pod undocumented
292 0 my $self = shift;
293 0 carp $self->name, ": min param is deprecated, use min_index\n";
294 0 return $self->min_index;
295 }
296  
297 1635 ▶sub cargo_type { goto &get_cargo_type; }
Subroutine cargo_type: called CC=1 SCAR=0.0
Pod undocumented
298  
299 2845 ▶sub get_cargo_type {
Subroutine get_cargo_type: called CC=1 SCAR=0.0
Pod documented
300 2845 my $self = shift;
301  
302 #my @ids = $self->fetch_all_indexes ;
303 # the returned cargo type might be different from collected type
304 # when collected type is 'warped_node'.
305 #return @ids ? $self->fetch_with_id($ids[0])->get_cargo_type
306 # : $self->{cargo_type} ;
307 2845 return $self->{cargo}{type};
308 }
309  
310 2 ▶sub get_cargo_info {
Subroutine get_cargo_info: called CC=1 SCAR=0.0
Pod documented
311 2 my $self = shift;
312 2 my $what = shift;
313 2 return $self->{cargo}{$what};
314 }
315  
316 # internal, does a grab with improved error message
317 11 ▶sub safe_typed_grab ($self, %args) {
Subroutine safe_typed_grab: called CC=5 SCAR=16.2
Pod undocumented
318 11 ▶ my $param = $args{param} || croak "safe_typed_grab: missing param";
Condition50%1/2
$args{'param'} || croak('safe_typed_grab: missing param')
l!l
110
MC/DC0%0/1
$args{'param'} || croak('safe_typed_grab: missing param')
$args{'param'}
Truth table50%1/2
A
A= $args{'param'}
Aresult
11
00
319  
320 11 my $res = eval {
321 $self->grab(
322 step => $self->{$param},
323 type => $self->get_type,
324 11 ▶ check => $args{check} || 'yes',
Condition100%2/2
$args{'check'} || 'yes'
l!l
56
MC/DC100%1/1
$args{'check'} || 'yes'
$args{'check'}
Truth table100%2/2
A
A= $args{'check'}
Aresult
11
00
325 );
326 };
327  
328 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 10 11
329 1 my $e = $@;
330 1 ▶ my $msg = $e ? $e->full_message : '';
Branch: 1 branch
BranchTrueFalseTotal
$e ? : 1 0 1
331 1 Config::Model::Exception::Model->throw(
332 object => $self,
333 error => "'$param' parameter: " . $msg
334 );
335 }
336  
337 10 return $res;
338 }
339  
340 835 ▶sub get_default_keys {
Subroutine get_default_keys: called CC=5 SCAR=16.1
Pod documented
341 835 my $self = shift;
342  
343 835 ▶ if ( $self->{follow_keys_from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"follow_keys_from"}) 3 832 835
344 3 my $followed = $self->safe_typed_grab( param => 'follow_keys_from' );
345 2 my @res = $followed->fetch_all_indexes;
346 2 ▶ return wantarray ? @res : \@res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
347 }
348  
349 832 my @res;
350  
351 3 push @res, @{ $self->{default_keys} }
352 832 ▶ if defined $self->{default_keys};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"default_keys"} 3 829 832
353  
354 832 ▶ return wantarray ? @res : \@res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 832 832
355 }
356  
357 1647 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
358 1647 my $self = shift;
359 1647 return $self->{parent}->name . ' ' . $self->{element_name} . ' id';
360 }
361  
362 # internal. Handle model declaration arguments
363 0 ▶sub handle_args ($self, %args) {
Subroutine handle_args: not called CC=5 SCAR=34.0
Pod undocumented
364 0 my $warp_info = delete $args{warp};
365  
366 0 for (qw/index_class index_type morph ordered/) {
367 0 ▶ $self->{$_} = delete $args{$_} if defined $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$_} 0 0 0
368 }
369  
370 0 ▶ $self->set_properties(%args) if defined $self->{index_type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"index_type"} 0 0 0
371  
372 0 ▶ if ( defined $warp_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $warp_info) 0 0 0
373 0 $self->{warper} = Config::Model::Warper->new(
374 warped_object => $self,
375 %$warp_info,
376 allowed => \@allowed_warp_params
377 );
378 }
379  
380 0 return $self;
381 }
382  
383 15 ▶sub apply_fixes {
Subroutine apply_fixes: called CC=1 SCAR=0.0
Pod undocumented
384 15 my $self = shift;
385 15 $fix_logger->trace( $self->location . ": apply_fixes called" );
386  
387 15 $self->deep_check( fix => 1, logger => $fix_logger );
388 15 return;
389 }
390  
391 my %check_idx_dispatch =
392 39 map { ( $_ => 'check_' . $_ ); }
393 qw/follow_keys_from allow_keys allow_keys_from allow_keys_matching
394 warn_if_key_match warn_unless_key_match/;
395  
396 39 my %mode_move = (
397 layered => { preset => 1, normal => 1 },
398 preset => { normal => 1 },
399 normal => {},
400 );
401  
402 1023 ▶around notify_change => sub ($orig, $self, %args) {
Subroutine __ANON__: called CC=8 SCAR=20.8
403 1023 ▶ if ($change_logger->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($change_logger->is_trace) 22 1001 1023
404 22 ▶ my @a = map { ( $_ => $args{$_} // '<undef>' ); } sort keys %args;
Condition100%2/2
$args{$_} // "<undef>"
l!l
15015
MC/DC100%1/1
$args{$_} // "<undef>"
$args{$_}
Truth table100%2/2
A
A= $args{$_}
Aresult
11
00
405 22 $change_logger->trace( "called for ", $self->name, " from ", join( ' ', caller ),
406 " with ", join( ' ', @a ) );
407 }
408  
409 # $idx may be undef if $self has changed, not necessarily its content
410 1023 my $idx = $args{index};
411 1023 ▶ if ( defined $idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx) 712 311 1023
412  
413 # use $idx to trigger move from layered->preset->normal
414 712 my $imode = $self->instance->get_data_mode;
415 712 ▶ my $old_mode = $self->get_data_mode($idx) || 'normal';
Condition100%2/2
$self->get_data_mode($idx) || 'normal'
l!l
68230
MC/DC100%1/1
$self->get_data_mode($idx) || 'normal'
$self->get_data_mode($idx)
Truth table100%2/2
A
A= $self->get_data_mode($idx)
Aresult
11
00
416 712 ▶ $self->set_data_mode( $idx, $imode ) if $mode_move{$old_mode}{$imode};
Branch: 1 branch
BranchTrueFalseTotal
if $mode_move{$old_mode}{$imode} 4 708 712
417 }
418  
419 1023 ▶ return if $self->instance->initial_load and not $args{really};
Branch: 1 branch
BranchTrueFalseTotal
if $self->instance->initial_load and not $args{"really"} 12 1011 1023
Condition66%2/3
$self->instance->initial_load and not $args{"really"}
!ll&&!rl&&r
1011012
MC/DC50%1/2
$self->instance->initial_load and not $args{"really"}
$self->instance->initial_load not $args{"really"}
Truth table66%2/3
A and not(B)
A= $self->instance->initial_loadB= not $args{"really"}
ABresult
0X0
100
111
420  
421 1011 $self-> needs_content_check(1);
422 1011 $self->$orig(%args);
423 1011 return;
424 39 };
425  
426 # the number of checks is becoming confusing. We have
427 # - check_idx to check whether an index is fine. This is called when creating
428 # a new index
429 # - check_or_fix_content: a more expensive check that runs all content checker registered
430 # in this object. By default, none. A plain AnyId can contains a duplicated_content
431 # checker if configured
432 # - a deep_check (for lack of a better name): a also expensive check that involve index
433 # versus other part of the config tree. By default, no check is done. This is currently
434 # used only by DPkg model which check if the index value is used elsewhere
435  
436 # Using plain check in this class is avoided because it's too generic, but a polymorphic
437 # entry point is still needed, oh well...
438 1 ▶sub check {
Subroutine check: called CC=1 SCAR=0.0
Pod documented
439 # since check is not used when creating an index, but called explicitly
440 # so it can be forwarded to deep_check.
441 1 goto &deep_check; # backward compat
442 }
443  
444 21 ▶sub deep_check ($self, @args) {
Subroutine deep_check: called CC=2 SCAR=6.9
Pod undocumented
445 21 $deep_check_logger->trace("called on ".$self->name);
446  
447 21 for ( $self->fetch_all_indexes() ) {
448 66 $self->check_idx(@args, index => $_);
449 }
450  
451 21 $self->check_or_fix_content(@args, logger => $deep_check_logger);
452 21 return;
453 }
454  
455 # check globally the list or hash, called by apply_fix or deep_check
456 367 ▶sub check_or_fix_content ($self, %args) {
Subroutine check_or_fix_content: called CC=15 SCAR=27.5
Pod undocumented
457 367 ▶ my $silent = $args{silent} || 0;
Condition50%1/2
$args{'silent'} || 0
l!l
0367
MC/DC0%0/1
$args{'silent'} || 0
$args{'silent'}
Truth table50%1/2
A
A= $args{'silent'}
Aresult
11
00
458 367 ▶ my $apply_fix = $args{fix} || 0;
Condition100%2/2
$args{'fix'} || 0
l!l
15352
MC/DC100%1/1
$args{'fix'} || 0
$args{'fix'}
Truth table100%2/2
A
A= $args{'fix'}
Aresult
11
00
459 367 ▶ my $local_logger = $args{logger} || $logger;
Condition66%2/3
$args{'logger'} || $logger
l!l&&r!l&&!r
213460
MC/DC0%0/2
$args{'logger'} || $logger
$args{'logger'} $logger
Truth table66%2/3
A || B
A= $args{'logger'}B= $logger
ABresult
1X1
011
000
460  
461 367 ▶ if ( $apply_fix or $self-> needs_content_check ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($apply_fix or $self->needs_content_check) { } 248 119 367
Condition100%3/3
$apply_fix or $self->needs_content_check
l!l&&r!l&&!r
15233119
MC/DC100%2/2
$apply_fix or $self->needs_content_check
$apply_fix $self->needs_content_check
Truth table100%3/3
A or B
A= $apply_fixB= $self->needs_content_check
ABresult
1X1
011
000
462  
463 248 $local_logger->trace( "Running check_content on ",$self->location );
464 # need to keep track to update GUI
465 248 $self-> flush_fixes; # reset before check
466  
467 248 my @error;
468 my @warn;
469  
470 248 foreach my $sub ( $self-> get_all_content_checks ) {
471 11 $sub->( \@error, \@warn, $apply_fix );
472 }
473  
474 248 my $nb = $self->fetch_size;
475 push @error, "Too many items ($nb) limit $self->{max_nb}, "
476 248 ▶ if defined $self->{max_nb} and $nb > $self->{max_nb};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max_nb"} and $nb > $self->{"max_nb"} 0 248 248
Condition33%1/3
defined $self->{"max_nb"} and $nb > $self->{"max_nb"}
!ll&&!rl&&r
24800
MC/DC0%0/2
defined $self->{"max_nb"} and $nb > $self->{"max_nb"}
defined $self->{"max_nb"} $nb > $self->{"max_nb"}
Truth table33%1/3
A and B
A= defined $self->{"max_nb"}B= $nb > $self->{"max_nb"}
ABresult
0X0
100
111
477  
478 248 ▶ if (not $silent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($silent) 248 0 248
479 248 for ( @warn ) {
480 3 $user_logger->warn( "Warning in '" . $self->location_short . "': $_" )
481 }
482 }
483  
484  
485 248 $self->{content_warning_list} = \@warn;
486 248 $self->{content_error_list} = \@error;
487 248 $self-> needs_content_check(0);
488  
489 248 ▶ return scalar @error ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @error ? : 1 247 248
490 }
491 else {
492 119 ▶ $local_logger->debug( $self->location, " has not changed, actual check skipped" )
Branch: 1 branch
BranchTrueFalseTotal
if $local_logger->is_debug 1 118 119
493 if $local_logger->is_debug;
494 119 ▶ my $err = $self->{content_error_list} // [];
Condition50%1/2
$self->{"content_error_list"} // []
l!l
1190
MC/DC0%0/1
$self->{"content_error_list"} // []
$self->{"content_error_list"}
Truth table50%1/2
A
A= $self->{"content_error_list"}
Aresult
11
00
495 119 ▶ return scalar @$err ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @$err ? : 1 118 119
496 }
497 }
498  
499 # internal function to check the validity of the index. Called when creating a new
500 # index or when set_properties is called (init or during warp)
501 1099 ▶sub check_idx ($self, @args) {
Subroutine check_idx: called CC=24 SCAR=32.0
Pod undocumented
502 1099 my %args = _resolve_arg_shortcut(\@args, 'index');
503 1099 my $idx = $args{index};
504 1099 ▶ my $silent = $args{silent} || 0;
Condition50%1/2
$args{'silent'} || 0
l!l
01099
MC/DC0%0/1
$args{'silent'} || 0
$args{'silent'}
Truth table50%1/2
A
A= $args{'silent'}
Aresult
11
00
505 1099 ▶ my $check = $args{check} || 'yes';
Condition100%2/2
$args{'check'} || 'yes'
l!l
957142
MC/DC100%1/1
$args{'check'} || 'yes'
$args{'check'}
Truth table100%2/2
A
A= $args{'check'}
Aresult
11
00
506 1099 ▶ my $apply_fix = $args{fix} // ($check eq 'fix' ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$check eq 'fix' ? : 0 1048 1048
Condition100%2/2
$args{"fix"} // ($check eq "fix" ? 1 : 0)
l!l
511048
MC/DC100%1/1
$args{"fix"} // ($check eq "fix" ? 1 : 0)
$args{"fix"}
Truth table100%2/2
A
A= $args{"fix"}
Aresult
11
00
507  
508 1099 ▶ Config::Model::Exception::Internal->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 1099 1099
509 object => $self,
510 error => "check_idx method: key or index is not defined"
511 ) unless defined $idx;
512  
513 1099 my @error;
514 my @warn;
515  
516 1099 foreach my $key_check_name ( keys %check_idx_dispatch ) {
517 6594 ▶ next unless $self->{$key_check_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$key_check_name} 6584 10 6594
518 10 my $method = $check_idx_dispatch{$key_check_name};
519 10 $self->$method( $idx, \@error, \@warn, $apply_fix );
520 }
521  
522 1099 my $nb = $self->fetch_size;
523 1099 my $new_nb = $nb;
524 1099 ▶ $new_nb++ unless $self->_exists($idx);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_exists($idx) 965 134 1099
525  
526 1099 ▶ if ( $idx eq '' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($idx eq '') { } 1 1098 1099
elsif ($self->{'index_type'} eq 'integer' and $idx =~ /\D/u) { } 0 1098 1098
elsif (defined $self->{'max_index'} and $idx > $self->{'max_index'}) { } 2 1096 1098
elsif (defined $self->{'min_index'} and $idx < $self->{'min_index'}) { } 1 2174 2175
527 1 push @error, "Index is empty";
528 }
529 ▶ elsif ( $self->{index_type} eq 'integer' and $idx =~ /\D/ ) {
Condition66%2/3
$self->{"index_type"} eq "integer" and $idx =~ /\D/u
!ll&&!rl&&r
4306680
MC/DC0%0/2
$self->{"index_type"} eq "integer" and $idx =~ /\D/u
$self->{"index_type"} eq "integer" $idx =~ /\D/u
Truth table66%2/3
A and B
A= $self->{"index_type"} eq "integer"B= $idx =~ /\D/u
ABresult
0X0
100
111
530 0 push @error, "Index is not integer ($idx)";
531 }
532 ▶ elsif ( defined $self->{max_index} and $idx > $self->{max_index} ) {
Condition100%3/3
defined $self->{"max_index"} and $idx > $self->{"max_index"}
!ll&&!rl&&r
1051452
MC/DC100%2/2
defined $self->{"max_index"} and $idx > $self->{"max_index"}
defined $self->{"max_index"} $idx > $self->{"max_index"}
Truth table100%3/3
A and B
A= defined $self->{"max_index"}B= $idx > $self->{"max_index"}
ABresult
0X0
100
111
533 2 push @error, "Index $idx > max_index limit $self->{max_index}";
534 }
535 ▶ elsif ( defined $self->{min_index} and $idx < $self->{min_index} ) {
Condition100%3/3
defined $self->{"min_index"} and $idx < $self->{"min_index"}
!ll&&!rl&&r
1079161
MC/DC100%2/2
defined $self->{"min_index"} and $idx < $self->{"min_index"}
defined $self->{"min_index"} $idx < $self->{"min_index"}
Truth table100%3/3
A and B
A= defined $self->{"min_index"}B= $idx < $self->{"min_index"}
ABresult
0X0
100
111
536 1 push @error, "Index $idx < min_index limit $self->{min_index}";
537 }
538  
539 push @error, "Too many items ($new_nb) limit $self->{max_nb}, " . "rejected id '$idx'"
540 1099 ▶ if defined $self->{max_nb} and $new_nb > $self->{max_nb};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max_nb"} and $new_nb > $self->{"max_nb"} 8 1091 1099
Condition100%3/3
defined $self->{"max_nb"} and $new_nb > $self->{"max_nb"}
!ll&&!rl&&r
1077148
MC/DC100%2/2
defined $self->{"max_nb"} and $new_nb > $self->{"max_nb"}
defined $self->{"max_nb"} $new_nb > $self->{"max_nb"}
Truth table100%3/3
A and B
A= defined $self->{"max_nb"}B= $new_nb > $self->{"max_nb"}
ABresult
0X0
100
111
541  
542 1099 ▶ if ( scalar @error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @error) 13 1086 1099
543 13 my @a = $self->_fetch_all_indexes;
544 13 push @error, "Item ids are '" . join( ',', @a ) . "'", $self->warp_error;
545 }
546  
547 1099 $self->{idx_error_list} = \@error;
548 1099 $self->{warning_hash}{$idx} = \@warn;
549  
550 1099 ▶ if (@warn and not $silent and $check ne 'no') {
Branch: 1 branch
BranchTrueFalseTotal
if (@warn and not $silent and $check ne "no") 3 1096 1099
Condition66%4/6
@warn and not $silent and $check ne "no"
!ll&&!rl&&r
109603
@warn and not $silent
!ll&&!rl&&r
109603
MC/DC33%1/3
@warn and not $silent and $check ne "no"
@warn not $silent $check ne "no"
Truth table50%2/4
A and not(B) and C
A= @warnB= not $silentC= $check ne "no"
ABCresult
0XX0
10X0
1100
1111
551 3 for (@warn) {
552 3 $user_logger->warn( "Warning in '" . $self->location_short . "': $_" );
553 }
554 }
555  
556 1099 ▶ return scalar @error ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @error ? : 13 1086 1099
557 }
558  
559 #internal
560 1 ▶sub check_follow_keys_from {
Subroutine check_follow_keys_from: called CC=2 SCAR=7.0
Pod undocumented
561 1 my ( $self, $idx, $error ) = @_;
562  
563 1 my $followed = $self->safe_typed_grab( param => 'follow_keys_from' );
564 1 ▶ return if $followed->exists($idx);
Branch: 1 branch
BranchTrueFalseTotal
if $followed->exists($idx) 0 1 1
565  
566 1 push @$error,
567 "key '" . $self->shorten_idx($idx) . "' does not exists in followed object '"
568 . $followed->name
569 . "'. Expected '"
570 . join( "', '", $followed->fetch_all_indexes ) . "'";
571 1 return;
572 }
573  
574 #internal
575 2 ▶sub check_allow_keys {
Subroutine check_allow_keys: called CC=2 SCAR=6.9
Pod undocumented
576 2 my ( $self, $idx, $error ) = @_;
577  
578 2 my $ok = grep { $_ eq $idx } @{ $self->{allow_keys} };
579  
580 push @$error,
581 2 ▶ "Unexpected key '" . $self->shorten_idx($idx) . "'. Expected '" . join( "', '", @{ $self->{allow_keys} } ) . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 1 2
582 unless $ok;
583 2 return;
584 }
585  
586 #internal
587 2 ▶sub check_allow_keys_matching {
Subroutine check_allow_keys_matching: called CC=2 SCAR=6.9
Pod undocumented
588 2 my ( $self, $idx, $error ) = @_;
589 2 my $match = $self->{allow_keys_matching};
590  
591 2 ▶ push @$error, "Unexpected key '" . $self->shorten_idx($idx) . "'. Key must match $match"
Branch: 1 branch
BranchTrueFalseTotal
unless $idx =~ /$match/u 1 1 2
592 unless $idx =~ /$match/;
593 2 return;
594 }
595  
596 #internal
597 2 ▶sub check_allow_keys_from {
Subroutine check_allow_keys_from: called CC=2 SCAR=6.9
Pod undocumented
598 2 my ( $self, $idx, $error ) = @_;
599  
600 2 my $from = $self->safe_typed_grab( param => 'allow_keys_from' );
601 2 my $ok = grep { $_ eq $idx } $from->fetch_all_indexes;
602  
603 2 ▶ return if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 1 2
604  
605 1 push @$error,
606 "key '" . $self->shorten_idx($idx) . "' does not exists in '"
607 . $from->name
608 . "'. Expected '"
609 . join( "', '", $from->fetch_all_indexes ) . "'";
610 1 return;
611 }
612  
613 2 ▶sub check_warn_if_key_match {
Subroutine check_warn_if_key_match: called CC=2 SCAR=7.0
Pod undocumented
614 2 my ( $self, $idx, $error, $warn ) = @_;
615 2 my $re = $self->{warn_if_key_match};
616  
617 2 ▶ push @$warn, "key '" . $self->shorten_idx($idx) . "' should not match $re\n" if $idx =~ /$re/;
Branch: 1 branch
BranchTrueFalseTotal
if $idx =~ /$re/u 2 0 2
618 2 return;
619 }
620  
621 1 ▶sub check_warn_unless_key_match {
Subroutine check_warn_unless_key_match: called CC=2 SCAR=7.0
Pod undocumented
622 1 my ( $self, $idx, $error, $warn ) = @_;
623 1 my $re = $self->{warn_unless_key_match};
624  
625 1 ▶ push @$warn, "key '" . $self->shorten_idx($idx) . "' should match $re\n" unless $idx =~ /$re/;
Branch: 1 branch
BranchTrueFalseTotal
unless $idx =~ /$re/u 1 0 1
626 1 return;
627 }
628  
629 9 ▶sub check_duplicates {
Subroutine check_duplicates: called CC=13 SCAR=25.7
Pod undocumented
630 9 my ( $self, $error, $warn, $apply_fix, $silent ) = @_;
631  
632 9 my $dup = $self->{duplicates};
633 9 ▶ return if $dup eq 'allow';
Branch: 1 branch
BranchTrueFalseTotal
if $dup eq "allow" 0 9 9
634  
635 9 $logger->trace("check_duplicates called");
636 9 my %h;
637 my @issues;
638 9 my @to_delete;
639 9 foreach my $i ( $self->fetch_all_indexes ) {
640 29 my $v = $self->fetch_with_id( index => $i, check => 'no' )->fetch;
641 29 ▶ next unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 1 28 29
642 28 ▶ $h{$v} = 0 unless defined $h{$v};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h{$v} 18 10 28
643 28 $h{$v}++;
644 28 ▶ if ( $h{$v} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{$v} > 1) 10 18 28
645 10 $logger->info(qq!got $h{$v} duplicates key "$i" value "$v"!);
646 10 push @to_delete, $i;
647 10 push @issues, qq!$i:"$v"!;
648 }
649 }
650  
651 9 ▶ return unless @issues;
Branch: 1 branch
BranchTrueFalseTotal
unless @issues 3 6 9
652  
653 6 ▶ if ($apply_fix) {
Branch: 4 branches
BranchTrueFalseTotal
if ($apply_fix) { } 2 4 6
elsif ($dup eq 'forbid') { } 1 3 4
elsif ($dup eq 'warn') { } 2 1 3
elsif ($dup eq 'suppress') { } 1 0 1
654 2 $logger->info("Fixing duplicates @issues, removing @to_delete");
655 2 for (reverse @to_delete) { $self->remove($_) }
656 }
657 elsif ( $dup eq 'forbid' ) {
658 1 $logger->info("Found forbidden duplicates @issues");
659 1 push @$error, "Forbidden duplicates value @issues";
660 }
661 elsif ( $dup eq 'warn' ) {
662 2 $logger->info("warning condition: found duplicate @issues");
663 2 push @$warn, "Duplicated value: @issues";
664 2 $self->add_fixes( scalar @issues);
665 }
666 elsif ( $dup eq 'suppress' ) {
667 1 $logger->info("suppressing duplicates @issues");
668 1 for (reverse @to_delete) { $self->remove($_) }
669 }
670 else {
671 0 die "Internal error: duplicates is $dup";
672 }
673 6 return;
674 }
675  
676 3501 ▶sub fetch_with_id ($self, @args) {
Subroutine fetch_with_id: called CC=10 SCAR=23.1
Pod documented
677 3501 my %args = _resolve_arg_shortcut(\@args, 'index');
678 3501 my $check = $self->_check_check( $args{check} );
679 3501 my $idx = $args{index};
680  
681 3501 ▶ $logger->trace( $self->name, " called for idx $idx" ) if $logger->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 57 3444 3501
682  
683 $idx = $self->{convert_sub}($idx)
684 3501 ▶ if ( defined $self->{convert_sub} and defined $idx );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"convert_sub"} and defined $idx 2 3499 3501
Condition66%2/3
defined $self->{"convert_sub"} and defined $idx
!ll&&!rl&&r
349902
MC/DC50%1/2
defined $self->{"convert_sub"} and defined $idx
defined $self->{"convert_sub"} defined $idx
Truth table66%2/3
A and B
A= defined $self->{"convert_sub"}B= defined $idx
ABresult
0X0
100
111
685  
686 # try migration only once
687 3501 ▶ $self->_migrate unless $self->{migration_done};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"migration_done"} 562 2939 3501
688  
689 3501 my $ok = 1;
690  
691 # check index only if it's unknown
692 3501 ▶ $ok = $self->check_idx( index => $idx, check => $check )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_defined($idx) or $check eq "no" 957 2544 3501
Condition100%3/3
$self->_defined($idx) or $check eq "no"
l!l&&r!l&&!r
25359957
MC/DC100%2/2
$self->_defined($idx) or $check eq "no"
$self->_defined($idx) $check eq "no"
Truth table100%3/3
A or B
A= $self->_defined($idx)B= $check eq "no"
ABresult
1X1
011
000
693 unless $self->_defined($idx)
694 or $check eq 'no';
695  
696 3501 ▶ if ( $ok or $check eq 'no' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok or $check eq 'no') { } 3489 12 3501
Condition66%2/3
$ok or $check eq "no"
l!l&&r!l&&!r
3489012
MC/DC50%1/2
$ok or $check eq "no"
$ok $check eq "no"
Truth table66%2/3
A or B
A= $okB= $check eq "no"
ABresult
1X1
011
000
697 3489 ▶ $self->auto_vivify($idx) unless $self->_defined($idx);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_defined($idx) 954 2535 3489
698 3488 return $self->_fetch_with_id($idx);
699 }
700 else {
701 Config::Model::Exception::WrongValue->throw(
702 12 error => join( "\n\t", @{ $self->{idx_error_list} } ),
703 object => $self
704 );
705 }
706  
707 0 return;
708 }
709  
710 3 ▶sub get ($self, @args) {
Subroutine get: called CC=10 SCAR=27.5
Pod documented
711 3 my %args = _resolve_arg_shortcut(\@args, 'path');
712 3 my $path = delete $args{path};
713 3 my $autoadd = 1;
714 3 ▶ $autoadd = $args{autoadd} if defined $args{autoadd};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"autoadd"} 0 3 3
715 3 ▶ my $get_obj = delete $args{get_obj} || 0;
Condition100%2/2
delete $args{'get_obj'} || 0
l!l
12
MC/DC100%1/1
delete $args{'get_obj'} || 0
delete $args{'get_obj'}
Truth table100%2/2
A
A= delete $args{'get_obj'}
Aresult
11
00
716 3 $path =~ s!^/!!;
717 3 my ( $item, $new_path ) = split m!/!, $path, 2;
718  
719 3 my $dcm = $args{dir_char_mockup};
720  
721 # $item =~ s($dcm)(/)g if $dcm ;
722 3 ▶ if ($dcm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dcm) 0 3 3
723 0 while (1) {
724 0 my $i = index( $item, $dcm );
725 0 ▶ last if $i == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $i == -1 0 0 0
726 0 substr $item, $i, length($dcm), '/';
727 }
728 }
729  
730 3 ▶ return unless ( $self->exists($item) or $autoadd );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($item) or $autoadd 0 3 3
Condition33%1/3
$self->exists($item) or $autoadd
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->exists($item) or $autoadd
$self->exists($item) $autoadd
Truth table33%1/3
A or B
A= $self->exists($item)B= $autoadd
ABresult
1X1
011
000
731  
732 3 $logger->trace("get: path $path, item $item");
733  
734 3 my $obj = $self->fetch_with_id( index => $item, %args );
735 3 ▶ return $obj if ( ( $get_obj or $obj->get_type ne 'leaf' ) and not defined $new_path );
Branch: 1 branch
BranchTrueFalseTotal
if $get_obj || $obj->get_type ne "leaf" and not defined $new_path 0 3 3
Condition50%3/6
$get_obj || $obj->get_type ne "leaf" and not defined $new_path
!ll&&!rl&&r
030
$get_obj or $obj->get_type ne "leaf"
l!l&&r!l&&!r
120
MC/DC0%0/3
$get_obj || $obj->get_type ne "leaf" and not defined $new_path
$get_obj $obj->get_type ne "leaf" not defined $new_path
Truth table40%2/5
A or B and not(C)
A= $get_objB= $obj->get_type ne "leaf"C= not defined $new_path
ABCresult
00X0
1X00
0100
1X11
0111
736 3 return $obj->get( path => $new_path, get_obj => $get_obj, %args );
737 }
738  
739 1 ▶sub set ($self, $path, @args) {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
740 1 $path =~ s!^/!!;
741 1 my ( $item, $new_path ) = split m!/!, $path, 2;
742 1 return $self->fetch_with_id($item)->set( $new_path, @args );
743 }
744  
745 16 ▶sub copy ( $self, $from, $to ) {
Subroutine copy: called CC=5 SCAR=16.7
Pod documented
746  
747 16 ▶ Config::Model::Exception::User->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($from) 1 15 16
748 object => $self,
749 message => "move: unknow from key $from"
750 ) unless $self->exists($from);
751  
752 15 my $from_obj = $self->fetch_with_id($from);
753 15 my $ok = $self->check_idx($to);
754  
755 15 ▶ if ( $ok && $self->{cargo}{type} eq 'leaf' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ok and $self->{'cargo'}{'type'} eq 'leaf') { } 8 7 15
elsif ($ok) { } 7 0 7
Condition66%2/3
$ok and $self->{"cargo"}{"type"} eq "leaf"
!ll&&!rl&&r
078
MC/DC50%1/2
$ok and $self->{"cargo"}{"type"} eq "leaf"
$ok $self->{"cargo"}{"type"} eq "leaf"
Truth table66%2/3
A and B
A= $okB= $self->{"cargo"}{"type"} eq "leaf"
ABresult
0X0
100
111
756 8 $logger->trace( "AnyId: copy leaf value from " . $self->name . " $from to $to" );
757 8 return $self->fetch_with_id($to)->store( $from_obj->fetch() );
758 }
759 elsif ($ok) {
760  
761 # node object
762 7 $logger->trace( "AnyId: deep copy node from " . $self->name );
763 7 my $target = $self->fetch_with_id($to);
764 7 $logger->trace( "AnyId: deep copy node to " . $target->name );
765 7 return $target->copy_from($from_obj);
766 }
767 else {
768 Config::Model::Exception::WrongValue->throw(
769 0 error => join( "\n\t", @{ $self->{idx_error_list} } ),
770 object => $self
771 );
772 }
773 0 return;
774 }
775  
776 48 ▶sub fetch_all {
Subroutine fetch_all: called CC=1 SCAR=0.0
Pod documented
777 48 my $self = shift;
778 48 my @keys = $self->fetch_all_indexes;
779 48 return map { $self->fetch_with_id($_); } @keys;
780 }
781  
782 0 ▶sub fetch_size {
Subroutine fetch_size: not called CC=1 SCAR=6.9
Pod documented
783 0 my $self = shift;
784 0 return scalar $self->fetch_all_indexes;
785 }
786  
787 1 ▶sub fetch ($self, @args) {
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
788 1 return join(',', $self->fetch_all_values(@args) );
789 }
790  
791 671 ▶sub fetch_value ($self, @args) {
Subroutine fetch_value: called CC=1 SCAR=0.0
Pod documented
792 671 my %args = _resolve_arg_shortcut(\@args, 'idx');
793 671 return $self->_fetch_value(%args, sub => 'fetch');
794 }
795  
796 19 ▶sub fetch_summary ($self, @args) {
Subroutine fetch_summary: called CC=1 SCAR=0.0
Pod documented
797 19 my %args = _resolve_arg_shortcut(\@args, 'idx');
798 19 return $self->_fetch_value(%args, sub => 'fetch_summary');
799 }
800  
801 690 ▶sub _fetch_value ($self, %args) {
Subroutine _fetch_value: called CC=2 SCAR=7.9
802 690 my $check = $self->_check_check( $args{check} );
803 690 my $sub = delete $args{sub};
804  
805 690 ▶ if ( $self->{cargo}{type} eq 'leaf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cargo'}{'type'} eq 'leaf') { } 690 0 690
806 690 return $self->fetch_with_id($args{idx})->$sub( check => $check, mode => $args{mode} );
807 }
808 else {
809 Config::Model::Exception::WrongType->throw(
810 object => $self,
811 function => 'fetch_values',
812 got_type => $self->{cargo}{type},
813 0 expected_type => 'leaf',
814 info => "with index $args{idx}",
815 );
816 }
817 0 return;
818 }
819  
820 359 ▶sub fetch_all_values ($self, @args) {
Subroutine fetch_all_values: called CC=4 SCAR=13.9
Pod documented
821 359 my %args = _resolve_arg_shortcut(\@args, 'mode');
822 359 my $mode = $args{mode};
823 359 my $check = $self->_check_check( $args{check} );
824  
825 359 my @keys = $self->fetch_all_indexes;
826  
827 # verify content restrictions applied to List (e.g. no duplicate values)
828 359 ▶ my $ok = $check eq 'no' ? 1 : $self->check_or_fix_content();
Branch: 1 branch
BranchTrueFalseTotal
$check eq 'no' ? : 14 345 359
829  
830 359 ▶ if ( $ok or $check eq 'no' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok or $check eq 'no') { } 357 2 359
Condition66%2/3
$ok or $check eq "no"
l!l&&r!l&&!r
35702
MC/DC50%1/2
$ok or $check eq "no"
$ok $check eq "no"
Truth table66%2/3
A or B
A= $okB= $check eq "no"
ABresult
1X1
011
000
831 671 return grep { defined $_ }
832 357 map { $self->fetch_value(idx => $_, check => $check, mode => $mode ); } @keys;
833 }
834 else {
835 Config::Model::Exception::WrongValue->throw(
836 2 error => join( "\n\t", @{ $self->{content_error_list} } ),
837 object => $self
838 );
839 }
840 0 return;
841 }
842  
843 2116 ▶sub fetch_all_indexes {
Subroutine fetch_all_indexes: called CC=1 SCAR=0.0
Pod documented
844 2116 my $self = shift;
845 2116 $self->create_default; # will check itself if creation is necessary
846 2116 $self->_migrate;
847 2116 return $self->_fetch_all_indexes;
848 }
849  
850 # TODO: remove in 2027
851 0 ▶sub get_all_indexes {
Subroutine get_all_indexes: not called CC=1 SCAR=6.9
Pod undocumented
852 0 my $self = shift;
853 0 croak "get_all_indexes is obsolete. use fetch_all_indexes";
854 }
855  
856 0 ▶sub children {
Subroutine children: not called CC=1 SCAR=6.9
Pod documented
857 0 my $self = shift;
858 0 return $self->fetch_all_indexes;
859 }
860  
861 2 ▶sub has_data {
Subroutine has_data: called CC=1 SCAR=0.0
Pod documented
862 2 my $self = shift;
863 2 return $self->fetch_size ;
864 }
865  
866 # auto vivify must create according to cargo}{type
867 # node -> Node or user class
868 # leaf -> Value or user class
869  
870 # warped node cannot be used. Same effect can be achieved by warping
871 # cargo_args
872  
873 39 my %element_default_class = (
874 warped_node => 'WarpedNode',
875 node => 'Node',
876 leaf => 'Value',
877 );
878  
879 39 my %can_override_class = (
880 node => 0,
881 leaf => 1,
882 );
883  
884 #internal
885 954 ▶sub auto_vivify {
Subroutine auto_vivify: called CC=8 SCAR=21.1
Pod undocumented
886 954 my ( $self, $idx ) = @_;
887 954 my %cargo_args = %{ $self->cargo };
888 954 my $override_class = delete $cargo_args{class}; # to override class in cargo
889  
890 954 my $cargo_type = delete $cargo_args{type};
891  
892 954 ▶ unless (defined $element_default_class{$cargo_type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $element_default_class{$cargo_type}) 0 954 954
893 0 my $msg = sprintf("unknown '%s' cargo type in cargo_args. Expected %s",
894 $cargo_type, join( ' or ', keys %element_default_class ));
895 0 Config::Model::Exception::Model->throw(object => $self, message => $msg);
896 }
897  
898 954 my $el_class = 'Config::Model::' . $element_default_class{$cargo_type};
899  
900 954 ▶ if ( defined $override_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $override_class) 39 915 954
901 Config::Model::Exception::Model->throw(
902 object => $self,
903 message => "$cargo_type class cannot be overidden by '$override_class'"
904 39 ▶ ) unless $can_override_class{$cargo_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $can_override_class{$cargo_type} 0 39 39
905 39 $el_class = $override_class;
906 }
907  
908 954 foreach my $parm (qw/default upstream_default/) {
909 1907 ▶ if ($cargo_args{$parm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cargo_args{$parm}) 1 1906 1907
910 1 my $type = $self->get_type;
911 1 Config::Model::Exception::Model->throw(
912 object => $self,
913 message => "Setting $parm value in a $type does not make sense."
914 );
915 }
916 }
917  
918 953 ▶ if ($self->{default_with_init}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default_with_init"}) 0 953 953
919 0 Config::Model::Exception::Model->throw(
920 object => $self,
921 message => "default_with_init is obsolete"
922 );
923 }
924  
925 my @common_args = (
926 element_name => $self->{element_name},
927 index_value => $idx,
928 instance => $self->{instance},
929 953 parent => $self->parent,
930 container => $self,
931 %cargo_args,
932 );
933  
934 953 my $item;
935  
936 # check parameters passed by the user
937 953 ▶ if ( $cargo_type eq 'node' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cargo_type eq 'node') { } 300 653 953
938 300 $item = $self->load_node( @common_args, config_class_name => $self->config_class_name );
939 }
940 else {
941 653 Mouse::Util::load_class($el_class);
942 653 $item = $el_class->new(@common_args);
943 }
944  
945 953 my $imode = $self->instance->get_data_mode;
946 953 $self->set_data_mode( $idx, $imode );
947  
948 953 $self->_store( $idx, $item );
949 953 return;
950 }
951  
952 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
953 0 ▶sub defined {
Subroutine defined: not called CC=1 SCAR=6.9
Pod documented
954 0 my ( $self, $idx ) = @_;
955  
956 0 return $self->_defined($idx);
957 }
958  
959 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
960 65 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
961 65 my ( $self, $idx ) = @_;
962  
963 65 return $self->_exists($idx);
964 }
965  
966 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
967 54 ▶sub delete {
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
968 54 my ( $self, $idx ) = @_;
969  
970 54 delete $self->{warning_hash}{$idx};
971 54 my $ret = $self->_delete($idx);
972 # notification is not needed if the value was already delete or missing
973 54 ▶ $self->notify_change( note => "deleted entry $idx" ) if defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ret 53 1 54
974 54 return $ret;
975 }
976  
977 24 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
978 24 my ($self) = @_;
979  
980 24 $self->{warning_hash} = {};
981 24 $self->_clear;
982 24 $self->clear_data_mode;
983 24 $self->notify_change( note => "cleared all entries" );
984 24 return;
985 }
986  
987 0 ▶sub clear_values {
Subroutine clear_values: not called CC=3 SCAR=24.8
Pod documented
988 0 my ($self) = @_;
989 0 carp "clear_values deprecated";
990  
991 0 my $ct = $self->get_cargo_type;
992 0 ▶ Config::Model::Exception::User->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $ct ne "leaf" 0 0 0
993 object => $self,
994 message => "clear_values() called on non leaf cargo type: '$ct'"
995 ) if $ct ne 'leaf';
996  
997 # this will trigger a notify_change
998 0 for ( $self->fetch_all_indexes ) {
999 0 $self->fetch_with_id($_)->store(undef);
1000 }
1001 0 $self->notify_change( note => "cleared all values" );
1002 0 return;
1003 }
1004  
1005 1 ▶sub warning_msg {
Subroutine warning_msg: called CC=4 SCAR=15.0
Pod documented
1006 1 my ( $self, $idx ) = @_;
1007 1 my $list ;
1008 1 ▶ if ( defined $idx ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $idx) { } 0 1 1
elsif (@{$self->{'content_warning_list'};}) { } 1 0 1
1009 0 $list = $self->{warning_hash}{$idx} ;
1010 }
1011 1 elsif ( @{ $self->{content_warning_list} } ) {
1012 1 $list = $self->{content_warning_list} ;
1013 }
1014 1 ▶ return $list ? join( "\n", @$list ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 1 0 1
1015 }
1016  
1017 60 ▶sub has_warning {
Subroutine has_warning: called CC=1 SCAR=0.0
Pod documented
1018 60 my $self = shift;
1019  
1020 60 return @{ $self->{content_warning_list} };
1021 }
1022  
1023 0 ▶sub error_msg {
Subroutine error_msg: not called CC=5 SCAR=34.0
Pod documented
1024 0 my $self = shift;
1025 0 my @list;
1026 0 for (qw/idx_error_list content_error_list/) {
1027 0 ▶ push @list, @{ $self->{$_} } if $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$_} 0 0 0
1028 }
1029  
1030 0 ▶ return unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 0 0 0
1031 0 ▶ return wantarray ? @list : join( "\n\t", @list );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1032 }
1033  
1034 39 __PACKAGE__->meta->make_immutable;
1035  
1036 39 1;
1037  
1038 # ABSTRACT: Base class for hash or list element
1039  
1040 __END__
« blib/lib/Config/Model/Backend/IniFile.pm ↑ Summary blib/lib/Config/Model/Node.pm »