blib/lib/Config/Model/AnyThing.pm

filter: Statementstmt 84.5% 104 / 123 Branchbran 70.3% 45 / 64 Conditioncond 69.3% 34 / 49 MC/DCmcdc 35.7% 10 / 28 Subroutinesub 84.3% 27 / 32 Podpod 50.0% 8 / 16 totaltotal 73.0% 228 / 312 CC 52 SCAR 20.4
CC
52
Cov
78%
xpath
42.0
notify_change
11.4
_check_check
7.5
CRAP 7.7
« blib/lib/Config/Model/Exception.pm ↑ Summary blib/lib/Config/Model/Dumper.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::AnyThing 2.169;
11  
12 59 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # FIXME: must cleanup warp mechanism to implement this
15 # use MouseX::StrictConstructor;
16  
17 59 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 59 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 59 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 59 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 59 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 59 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 59 my $logger = get_logger("Anything");
27 59 my $change_logger = get_logger("ChangeTracker");
28  
29 59 has element_name => ( is => 'ro', isa => 'Str' );
30 59 has parent => ( is => 'ro', isa => 'Config::Model::Node', weak_ref => 1 );
31  
32 59 has instance => (
33 is => 'ro',
34 isa => 'Config::Model::Instance',
35 weak_ref => 1,
36 handles => [qw/show_message root_path/]
37 );
38  
39 # needs_check defaults to 1 to trap undef mandatory values
40 59 has needs_check => ( is => 'rw', isa => 'Bool', default => 1 );
41  
42 # index_value can be written to when move method is called. But let's
43 # not advertise this feature.
44 has index_value => (
45 is => 'rw',
46 isa => 'Str',
47 1145 ▶ trigger => sub ($self, @) { $self->{location} = $self->_location; },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 59 );
49  
50 59 has container => ( is => 'ro', isa => 'Ref', required => 1, weak_ref => 1 );
51  
52 59 has container_type => ( is => 'ro', isa => 'Str', builder => '_container_type', lazy => 1 );
53  
54 0 ▶sub _container_type ($self) {
Subroutine _container_type: not called CC=2 SCAR=17.9
55 0 my $p = $self->parent;
56 0 ▶ return defined $p
Branch: 1 branch
BranchTrueFalseTotal
defined $p ? : 0 0 0
57 ? $p->element_type( $self->element_name )
58 : 'node'; # root node
59  
60 }
61  
62 59 has root => (
63 is => 'ro',
64 isa => 'Config::Model::Node',
65 weak_ref => 1,
66 builder => '_root',
67 lazy => 1
68 );
69  
70 9 ▶sub _root ($self) {
Subroutine _root: called CC=2 SCAR=7.2
71 9 ▶ return $self->parent || $self;
Condition66%2/3
$self->parent || $self
l!l&&r!l&&!r
180
MC/DC0%0/2
$self->parent || $self
$self->parent $self
Truth table66%2/3
A || B
A= $self->parentB= $self
ABresult
1X1
011
000
72 }
73  
74 59 has location => ( is => 'ro', isa => 'Str', builder => '_location', lazy => 1 );
75 59 has location_short => ( is => 'ro', isa => 'Str', builder => '_location_short', lazy => 1 );
76  
77 59 has backend_support_annotation => (
78 is => 'ro',
79 isa => 'Bool',
80 builder => '_backend_support_annotation',
81 lazy => 1
82 );
83  
84 1 ▶sub _backend_support_annotation ($self) {
Subroutine _backend_support_annotation: called CC=1 SCAR=0.0
85 # this method is overridden in Config::Model::Node
86 1 return $self->parent->backend_support_annotation;
87 };
88  
89 59 has backup => (
90 is => 'bare',
91 isa => 'Maybe[HashRef]',
92 );
93  
94 5634 ▶sub backup ($self) {
Subroutine backup: called CC=1 SCAR=0.0
Pod undocumented
95 5634 return dclone($self->{backup});
96 }
97  
98  
99 4409 ▶sub notify_change ($self, %args) {
Subroutine notify_change: called CC=11 SCAR=24.3
Pod documented
100  
101 4409 ▶ return if $self->instance->initial_load and not $args{really};
Branch: 1 branch
BranchTrueFalseTotal
if $self->instance->initial_load and not $args{"really"} 0 4409 4409
Condition66%2/3
$self->instance->initial_load and not $args{"really"}
!ll&&!rl&&r
440360
MC/DC0%0/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
102  
103 4409 ▶ if ($change_logger->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($change_logger->is_trace) 216 4193 4409
104 216 ▶ my @with = map { "'$_' -> '". ($args{$_} // '<undef>') ."'" } sort keys %args;
Condition100%2/2
$args{$_} // "<undef>"
l!l
1112134
MC/DC100%1/1
$args{$_} // "<undef>"
$args{$_}
Truth table100%2/2
A
A= $args{$_}
Aresult
11
00
105 216 $change_logger->trace("called for ", $self->name, " from ",
106 join( ' ', caller ), " with ", join( ' ', @with ));
107 }
108  
109 # needs_save may be overridden by caller
110 4409 ▶ $args{needs_save} //= 1;
Condition100%2/2
$args{"needs_save"} //= 1
l!l
28741535
MC/DC100%1/1
$args{"needs_save"} //= 1
$args{"needs_save"}
Truth table100%2/2
A
A= $args{"needs_save"}
Aresult
11
00
111 4409 ▶ $args{path} //= $self->location;
Condition66%2/3
$args{"path"} //= $self->location
l!l&&r!l&&!r
285015590
MC/DC0%0/2
$args{"path"} //= $self->location
$args{"path"} $self->location
Truth table66%2/3
A //= B
A= $args{"path"}B= $self->location
ABresult
1X1
011
000
112 4409 ▶ $args{name} //= $self->element_name if $self->element_name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->element_name 2843 1566 4409
Condition66%2/3
$args{"name"} //= $self->element_name
l!l&&r!l&&!r
128715560
MC/DC0%0/2
$args{"name"} //= $self->element_name
$args{"name"} $self->element_name
Truth table66%2/3
A //= B
A= $args{"name"}B= $self->element_name
ABresult
1X1
011
000
113 4409 ▶ $args{index} //= $self->index_value if $self->index_value;
Branch: 1 branch
BranchTrueFalseTotal
if $self->index_value 712 3697 4409
Condition66%2/3
$args{"index"} //= $self->index_value
l!l&&r!l&&!r
306820
MC/DC0%0/2
$args{"index"} //= $self->index_value
$args{"index"} $self->index_value
Truth table66%2/3
A //= B
A= $args{"index"}B= $self->index_value
ABresult
1X1
011
000
114  
115 # better use %args instead of @_ to forward arguments. %args eliminates duplicated keys
116 4409 $self->container->notify_change(%args);
117 4409 return;
118 }
119  
120 3254 ▶sub _location ($self) {
Subroutine _location: called CC=3 SCAR=11.0
121 3254 my $str = '';
122 3254 ▶ $str .= $self->parent->location if defined $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->parent 3114 140 3254
123  
124 3254 ▶ $str .= ' ' if $str;
Branch: 1 branch
BranchTrueFalseTotal
if $str 1362 1892 3254
125  
126 3254 $str .= $self->composite_name;
127  
128 3254 return $str;
129 }
130  
131 54 ▶sub _location_short ($self) {
Subroutine _location_short: called CC=3 SCAR=11.0
132 54 my $str = '';
133 54 ▶ $str .= $self->parent->location_short if defined $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->parent 43 11 54
134  
135 54 ▶ $str .= ' ' if $str;
Branch: 1 branch
BranchTrueFalseTotal
if $str 15 39 54
136  
137 54 $str .= $self->composite_name_short;
138  
139 54 return $str;
140 }
141  
142 #has composite_name => (is => 'ro', isa => 'Str' , builder => '_composite_name', lazy => 1);
143  
144 3418 ▶sub composite_name ($self) {
Subroutine composite_name: called CC=4 SCAR=13.9
Pod documented
145 3418 my $element = $self->element_name;
146 3418 ▶ $element = '' unless defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 140 3278 3418
147  
148 3418 my $idx = $self->index_value;
149 3418 ▶ return $element unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 2223 1195 3418
150 1195 ▶ $idx = '"' . $idx . '"' if $idx =~ /\W/;
Branch: 1 branch
BranchTrueFalseTotal
if $idx =~ /\W/u 73 1122 1195
151  
152 1195 return "$element:$idx";
153 }
154  
155 54 ▶sub composite_name_short ($self) {
Subroutine composite_name_short: called CC=4 SCAR=13.9
Pod documented
156 54 my $element = $self->element_name;
157 54 ▶ $element = '' unless defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 11 43 54
158  
159 54 my $idx = $self->shorten_idx($self->index_value);
160 54 ▶ return $element unless length $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless length $idx 53 1 54
161 1 ▶ $idx = '"' . $idx . '"' if $idx =~ /\W/;
Branch: 1 branch
BranchTrueFalseTotal
if $idx =~ /\W/u 0 1 1
162 1 return "$element:$idx";
163 }
164  
165 61 ▶sub shorten_idx ($self, $long_index) {
Subroutine shorten_idx: called CC=4 SCAR=13.9
Pod undocumented
166 61 ▶ my @idx = split /\n/, $long_index // '' ;
Condition100%2/2
$long_index // ""
l!l
853
MC/DC100%1/1
$long_index // ""
$long_index
Truth table100%2/2
A
A= $long_index
Aresult
11
00
167 61 my $idx = shift @idx;
168 61 ▶ $idx .= '[...]' if @idx;
Branch: 1 branch
BranchTrueFalseTotal
if @idx 1 60 61
169  
170 61 ▶ return $idx // ''; # may be undef on freebsd with perl 5.10.1 ...
Condition100%2/2
$idx // ""
l!l
853
MC/DC100%1/1
$idx // ""
$idx
Truth table100%2/2
A
A= $idx
Aresult
11
00
171 }
172  
173 ## Fixme: not yet tested
174 0 ▶sub xpath ($self) {
Subroutine xpath: not called CC=6 SCAR=37.4
Pod undocumented
175 0 $logger->trace("xpath called on $self");
176  
177 0 my $element = $self->element_name;
178 0 ▶ $element = '' unless defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 0 0 0
179  
180 0 my $idx = $self->index_value;
181  
182 0 my $str = '';
183 0 ▶ $str .= $self->cim_parent->parent->xpath
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("cim_parent") and defined $self->cim_parent 0 0 0
Condition0%0/3
$self->can("cim_parent") and defined $self->cim_parent
!ll&&!rl&&r
000
MC/DC0%0/2
$self->can("cim_parent") and defined $self->cim_parent
$self->can("cim_parent") defined $self->cim_parent
Truth table0%0/3
A and B
A= $self->can("cim_parent")B= defined $self->cim_parent
ABresult
0X0
100
111
184 if $self->can('cim_parent')
185 and defined $self->cim_parent;
186  
187 0 ▶ $str .= '/' . $element . ( defined $idx ? "[\@id=$idx]" : '' ) if $element;
Branch: 2 branches
BranchTrueFalseTotal
if $element 0 0 0
defined $idx ? : 0 0 0
188  
189 0 return $str;
190 }
191  
192 7903 ▶sub annotation ($self, @args) {
Subroutine annotation: called CC=7 SCAR=19.5
Pod documented
193 7903 ▶ my $old_note = $self->{annotation} || '';
Condition100%2/2
$self->{'annotation'} || ''
l!l
3447559
MC/DC100%1/1
$self->{'annotation'} || ''
$self->{'annotation'}
Truth table100%2/2
A
A= $self->{'annotation'}
Aresult
11
00
194 7903 ▶ if (@args and not $self->instance->preset and not $self->instance->layered) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and not $self->instance->preset and not $self->instance->layered) 190 7713 7903
Condition66%4/6
@args and not $self->instance->preset and not $self->instance->layered
!ll&&!rl&&r
77130190
@args and not $self->instance->preset
!ll&&!rl&&r
77130190
MC/DC33%1/3
@args and not $self->instance->preset and not $self->instance->layered
@args not $self->instance->preset not $self->instance->layered
Truth table50%2/4
A and not(B) and not(C)
A= @argsB= not $self->instance->presetC= not $self->instance->layered
ABCresult
0XX0
10X0
1100
1111
195 190 my $new = $self->{annotation} = join( "\n", grep { defined $_} @args );
196 190 ▶ $self->notify_change(note => 'updated annotation') unless $new eq $old_note;
Branch: 1 branch
BranchTrueFalseTotal
unless $new eq $old_note 185 5 190
197 }
198  
199 7903 ▶ return $self->{annotation} || '';
Condition100%2/2
$self->{'annotation'} || ''
l!l
5287375
MC/DC100%1/1
$self->{'annotation'} || ''
$self->{'annotation'}
Truth table100%2/2
A
A= $self->{'annotation'}
Aresult
11
00
200 }
201  
202 1 ▶sub clear_annotation ($self) {
Subroutine clear_annotation: called CC=2 SCAR=7.2
Pod documented
203 1 ▶ $self->notify_change(note => 'deleted annotation') if $self->{annotation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"annotation"} 1 0 1
204 1 return $self->{annotation} = '';
205 }
206  
207 # fallback method for object that don't implement has_data
208 0 ▶sub has_data ($self) {
Subroutine has_data: not called CC=2 SCAR=17.9
Pod undocumented
209 0 ▶ $logger->trace("called fall-back has_data for element", $self->name) if $logger->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 0 0 0
210 0 return 1;
211 }
212  
213 8 ▶sub model_searcher ($self, %args) {
Subroutine model_searcher: called CC=1 SCAR=0.0
Pod documented
214 8 my $model = $self->instance->config_model;
215 8 return Config::Model::SearchElement->new( model => $model, node => $self, %args );
216 }
217  
218 0 ▶sub searcher {
Subroutine searcher: not called CC=1 SCAR=6.9
Pod undocumented
219 0 carp "Config::Model::AnyThing searcher is deprecated";
220 0 goto &model_searcher;
221 }
222  
223 44 ▶sub dump_as_data ($self, %args) {
Subroutine dump_as_data: called CC=4 SCAR=17.9
Pod documented
224 44 ▶ my $full = delete $args{full_dump} || 0;
Condition50%1/2
delete $args{'full_dump'} || 0
l!l
044
MC/DC0%0/1
delete $args{'full_dump'} || 0
delete $args{'full_dump'}
Truth table50%1/2
A
A= delete $args{'full_dump'}
Aresult
11
00
225 44 ▶ if ($full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full) 0 44 44
226 0 carp "dump_as_data: full_dump parameter is deprecated. Please use 'mode => user' instead";
227 0 ▶ $args{mode} //= 'user';
Condition0%0/2
$args{"mode"} //= "user"
l!l
00
MC/DC0%0/1
$args{"mode"} //= "user"
$args{"mode"}
Truth table0%0/2
A
A= $args{"mode"}
Aresult
11
00
228 }
229 44 my $dumper = Config::Model::DumpAsData->new;
230 44 return $dumper->dump_as_data( node => $self, %args );
231 }
232  
233 # hum, check if the check information is valid
234 44010 ▶sub _check_check ($self, $p) {
Subroutine _check_check: called CC=7 SCAR=20.1
235 44010 ▶ return 'yes' if not defined $p or $p eq '1' or $p eq 'yes';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $p or $p eq 1 or $p eq "yes" 37572 6438 44010
Condition83%5/6
not defined $p or $p eq 1 or $p eq "yes"
l!l&&r!l&&!r
13592239806438
not defined $p or $p eq 1
l!l&&r!l&&!r
13592030418
MC/DC66%2/3
not defined $p or $p eq 1 or $p eq "yes"
not defined $p $p eq 1 $p eq "yes"
Truth table75%3/4
not(A) or B or C
A= not defined $pB= $p eq 1C= $p eq "yes"
ABCresult
1XX1
01X1
0011
0000
236 6438 ▶ return 'no' if $p eq '0' or $p eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if $p eq 0 or $p eq "no" 1745 4693 6438
Condition66%2/3
$p eq 0 or $p eq "no"
l!l&&r!l&&!r
017454693
MC/DC50%1/2
$p eq 0 or $p eq "no"
$p eq 0 $p eq "no"
Truth table66%2/3
A or B
A= $p eq 0B= $p eq "no"
ABresult
1X1
011
000
237 4693 ▶ return $p if $p eq 'skip';
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "skip" 4693 0 4693
238  
239 0 croak "Internal error: Unvalid check value: $p";
240 }
241  
242 0 ▶sub has_fixes ($self) {
Subroutine has_fixes: not called CC=1 SCAR=6.9
Pod undocumented
243 0 $logger->trace( "dummy has_fixes called on " . $self->name );
244 0 return 0;
245 }
246  
247 4 ▶sub has_warning ($self) {
Subroutine has_warning: called CC=1 SCAR=0.0
Pod undocumented
248 4 $logger->trace( "dummy has_warning called on " . $self->name );
249 4 return 0;
250 }
251  
252 16 ▶sub warp_error ($self) {
Subroutine warp_error: called CC=2 SCAR=6.9
Pod documented
253 16 ▶ return '' unless defined $self->{warper};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"warper"} 9 7 16
254 7 return $self->{warper}->warp_error;
255 }
256  
257 # used by Value and AnyId
258 4 ▶sub set_convert ($self, $arg_ref) {
Subroutine set_convert: called CC=4 SCAR=14.0
Pod undocumented
259 4 my $convert = delete $arg_ref->{convert};
260  
261 # convert_sub keeps a subroutine reference
262 $self->{convert_sub} =
263 2 ▶ $convert eq 'uc' ? sub ($input) { uc($input); }
Subroutine __ANON__: called CC=1 SCAR=0.0
264 3 ▶ : $convert eq 'lc' ? sub ($input) { lc($input); }
Subroutine __ANON__: called CC=1 SCAR=0.0
265 4 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
$convert eq 'uc' ? : 2 2 4
$convert eq 'lc' ? : 2 0 2
266  
267 Config::Model::Exception::Model->throw(
268 object => $self,
269 error => "Unexpected convert value: $convert, " . "expected lc or uc"
270 4 ▶ ) unless defined $self->{convert_sub};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"convert_sub"} 0 4 4
271 4 return;
272 }
273  
274 59 __PACKAGE__->meta->make_immutable;
275  
276 59 1;
277  
278 # ABSTRACT: Base class for configuration tree item
279  
280 __END__
« blib/lib/Config/Model/Exception.pm ↑ Summary blib/lib/Config/Model/Dumper.pm »