blib/lib/Config/Model/Instance.pm

filter: Statementstmt 90.9% 261 / 287 Branchbran 66.6% 48 / 72 Conditioncond 62.9% 34 / 54 MC/DCmcdc 48.4% 16 / 33 Subroutinesub 92.7% 64 / 69 Podpod 82.1% 23 / 28 totaltotal 82.1% 446 / 543 CC 64 SCAR 22.1
CC
64
Cov
83%
write_back
43.8
list_changes
12.1
__ANON__
10.8
CRAP 9.1
« blib/lib/Config/Model/Iterator.pm ↑ Summary blib/lib/Config/Model/ValueComputer.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::Instance 2.169;
11  
12 59 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 59 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 59 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 59 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 59 ▶use MouseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 59 with "Config::Model::Role::NodeLoader";
21 59 with "Config::Model::Role::Utils";
22  
23 59 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 59 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 59 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 59 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 59 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 59 ▶use Carp qw/carp croak confess cluck/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 59 ▶use Config::Model::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 59 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 59 ▶use Config::Model::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 59 ▶use Config::Model::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 59 ▶use Config::Model::SearchElement;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 59 ▶use Config::Model::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 59 ▶use Config::Model::ObjTreeScanner;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 59 my $logger = get_logger("Instance");
40 59 my $change_logger = get_logger("Anything::Change");
41 59 my $user_logger = get_logger("User");
42  
43 59 has [qw/root_class_name/] => ( is => 'ro', isa => 'Str', required => 1 );
44  
45 143 ▶sub location { return "in instance" }
Subroutine location: called CC=1 SCAR=0.0
Pod undocumented
46  
47 59 has config_model => (
48 is => 'ro',
49 isa => 'Config::Model',
50 weak_ref => 1,
51 required => 1
52 );
53  
54 59 has check => (
55 is => 'ro',
56 isa => 'Str',
57 default => 'yes',
58 reader => 'read_check',
59 );
60  
61 # used by cme -create option
62 59 has auto_create => (
63 is => 'ro',
64 isa => 'Bool',
65 default => 0,
66 );
67  
68 # a unique (instance wise) placeholder for various tree objects
69 # to store information
70 has _safe => (
71 is => 'rw',
72 isa => 'HashRef',
73 traits => ['Hash'],
74 144 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 59 handles => {
76 data => 'accessor',
77 },
78 );
79  
80 has appli_info => (
81 is => 'rw',
82 isa => 'HashRef',
83 traits => ['Hash'],
84 110 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 59 handles => {
86 get_appli_info => 'get',
87 # currying See Moose::Manual::Delegation
88 get_support_info => [qw/get support_info/],
89 },
90 );
91  
92  
93 # preset mode: to load values found by HW scan or other automatic scheme
94 # layered mode: to load values found in included files
95 # canonical mode: write config data back using model order instead of user order
96 59 has [qw/preset layered canonical/] => (
97 is => 'ro',
98 isa => 'Bool',
99 default => 0,
100 );
101  
102 has changes => (
103 is => 'ro',
104 isa => 'ArrayRef',
105 traits => ['Array'],
106 144 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
107 59 handles => {
108 add_change => 'push',
109 c_count => 'count',
110 has_changes => 'count',
111  
112 #needs_save => 'count' ,
113 clear_changes => 'clear',
114 } );
115  
116 65 ▶sub needs_save ($self) {
Subroutine needs_save: called CC=1 SCAR=0.0
Pod documented
117 65 return $self->c_count;
118 }
119  
120 has errors => (
121 is => 'ro',
122 isa => 'HashRef',
123 traits => ['Hash'],
124 144 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
125 59 handles => {
126 _set_error => 'set',
127 cancel_error => 'delete',
128 has_error => 'count',
129 clear_errors => 'clear',
130 error_paths => 'keys'
131 } );
132  
133 20 ▶sub add_error {
Subroutine add_error: called CC=1 SCAR=0.0
Pod undocumented
134 20 my $self = shift;
135 20 $self->_set_error( shift, '' );
136 20 return;
137 }
138  
139 9 ▶sub error_messages {
Subroutine error_messages: called CC=2 SCAR=7.0
Pod documented
140 9 my $self = shift;
141 9 my @errs = map { "$_: " . $self->config_root->grab($_)->error_msg } $self->error_paths;
142 9 ▶ return wantarray ? @errs : join( "\n", @errs );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 9 9
143 }
144  
145 3 ▶sub has_warning {
Subroutine has_warning: called CC=1 SCAR=0.0
Pod documented
146 3 my $self = shift;
147  
148 75 ▶ my $count_leaf_warnings = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
149 75 my ( $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_;
150 75 $$data_ref += $leaf_object->has_warning;
151 3 };
152  
153 9 ▶ my $count_list_warnings = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
154 9 my ( $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_;
155 9 $$data_ref += $node->fetch_element($element_name)->has_warning;
156 3 };
157  
158 3 my $scan = Config::Model::ObjTreeScanner->new(
159 leaf_cb => $count_leaf_warnings,
160 list_element_hook => $count_list_warnings,
161 hash_element_hook => $count_list_warnings,
162 );
163  
164 3 my $result = 0;
165 3 $scan->scan_node( \$result, $self->config_root );
166  
167 3 return $result;
168 }
169  
170 has on_change_cb => (
171 is => 'rw',
172 traits => ['Code'],
173 isa => 'CodeRef',
174 144 ▶ default => sub { sub { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
175 59 );
176  
177 has on_message_cb => (
178 traits => ['Code'],
179 is => 'rw',
180 isa => 'CodeRef',
181 0 ▶ default => sub { sub { say @_; } },
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
182 59 handles => {
183 show_message => 'execute',
184 },
185 );
186  
187 # initial_load mode: when data is loaded the first time
188 59 has initial_load => (
189 is => 'rw',
190 isa => 'Bool',
191 default => 0,
192 trigger => \&_trace_initial_load,
193 traits => [qw/Bool/],
194 handles => {
195 initial_load_start => 'set',
196 initial_load_stop => 'unset',
197 } );
198  
199 216 ▶sub _trace_initial_load {
Subroutine _trace_initial_load: called CC=1 SCAR=0.0
200 216 my ( $self, $n, $o ) = @_;
201 216 $logger->debug("switched to $n");
202 216 return;
203 }
204  
205 # This array holds a set of sub ref that will be invoked when
206 # the user requires to write all configuration tree in their
207 # backend storage.
208 has _write_back => (
209 is => 'ro',
210 isa => 'HashRef',
211 traits => ['Hash'],
212 handles => {
213 count_write_back => 'count', # mostly for tests
214 has_no_write_back => 'is_empty',
215 nodes_to_write_back => 'keys',
216 write_back_node_info => 'get',
217 delete_write_back => 'delete',
218 clear_write_back => 'clear',
219 },
220 144 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
221 59 );
222  
223 98 ▶sub register_write_back {
Subroutine register_write_back: called CC=2 SCAR=6.9
Pod documented
224 98 my ($self, $path, $backend, $wb) = @_;
225 98 ▶ push @{ $self->_write_back->{$path} //= [] }, [$backend, $wb];
Condition100%2/2
$self->_write_back->{$path} //= []
l!l
197
MC/DC100%1/1
$self->_write_back->{$path} //= []
$self->_write_back->{$path}
Truth table100%2/2
A
A= $self->_write_back->{$path}
Aresult
11
00
226 98 return;
227 }
228  
229 # used for auto_read auto_write feature
230 59 has [qw/name application backend_arg backup/] => (
231 is => 'ro',
232 isa => 'Maybe[Str]',
233 );
234  
235 59 has 'root_dir' => (
236 is => 'ro',
237 isa => 'Config::Model::TypeContraints::Path',
238 coerce => 1
239 );
240  
241 59 has root_path => (
242 is => 'ro',
243 isa => 'Path::Tiny',
244 lazy_build => 1,
245 );
246  
247 2 ▶sub _build_root_path {
Subroutine _build_root_path: called CC=3 SCAR=11.0
248 2 my $self = shift;
249 2 ▶ my $root_dir = $self->root_dir // '';
Condition100%2/2
$self->root_dir // ""
l!l
11
MC/DC100%1/1
$self->root_dir // ""
$self->root_dir
Truth table100%2/2
A
A= $self->root_dir
Aresult
11
00
250 2 ▶ return $root_dir ? path($root_dir) : Path::Tiny->cwd;
Branch: 1 branch
BranchTrueFalseTotal
$root_dir ? : 1 1 2
251 }
252  
253 59 has [qw/config_dir config_file/] => (
254 is => 'ro',
255 isa => 'Config::Model::TypeContraints::Path',
256 coerce => 1
257 );
258  
259 59 has tree => (
260 is => 'ro',
261 isa => 'Config::Model::Node',
262 builder => '_build_tree',
263 lazy => 1,
264 clearer => '_clear_config',
265 reader => 'config_root',
266 handles => [qw/apply_fixes deep_check grab grab_value/],
267 );
268  
269 1 ▶sub reset_config {
Subroutine reset_config: called CC=1 SCAR=0.0
Pod documented
270 1 my $self = shift;
271 1 $self->_clear_config;
272 1 $self->clear_changes;
273 1 return $self->config_root;
274 }
275  
276 143 ▶sub _build_tree {
Subroutine _build_tree: called CC=1 SCAR=0.0
277 143 my $self = shift;
278  
279 return $self->load_node (
280 config_class_name => $self->{root_class_name},
281 instance => $self,
282 container => $self,
283 config_file => $self->{config_file},
284 143 );
285 }
286  
287 5 ▶sub preset_start {
Subroutine preset_start: called CC=2 SCAR=7.0
Pod documented
288 5 my $self = shift;
289 5 $logger->info("Starting preset mode");
290 carp "Cannot start preset mode during layered mode"
291 5 ▶ if $self->{layered};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"layered"} 0 5 5
292 5 $self->{preset} = 1;
293 5 return;
294 }
295  
296 5 ▶sub preset_stop {
Subroutine preset_stop: called CC=1 SCAR=0.0
Pod documented
297 5 my $self = shift;
298 5 $logger->info("Stopping preset mode");
299 5 $self->{preset} = 0;
300 5 return;
301 }
302  
303 1 ▶sub preset_clear {
Subroutine preset_clear: called CC=1 SCAR=0.0
Pod documented
304 1 my $self = shift;
305  
306 32 ▶ my $leaf_cb = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
307 32 my ( $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_;
308 32 ▶ $$data_ref ||= $leaf_object->clear_preset;
Condition100%3/3
$$data_ref ||= $leaf_object->clear_preset
l!l&&r!l&&!r
61214
MC/DC100%2/2
$$data_ref ||= $leaf_object->clear_preset
$$data_ref $leaf_object->clear_preset
Truth table100%3/3
A ||= B
A= $$data_refB= $leaf_object->clear_preset
ABresult
1X1
011
000
309 1 };
310  
311 1 $self->_stuff_clear($leaf_cb);
312 1 return;
313 }
314  
315 9 ▶sub layered_start {
Subroutine layered_start: called CC=2 SCAR=7.0
Pod documented
316 9 my $self = shift;
317 9 $logger->info("Starting layered mode");
318 carp "Cannot start layered mode during preset mode"
319 9 ▶ if $self->{preset};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"preset"} 0 9 9
320 9 $self->{layered} = 1;
321 9 return;
322 }
323  
324 8 ▶sub layered_stop {
Subroutine layered_stop: called CC=1 SCAR=0.0
Pod documented
325 8 my $self = shift;
326 8 $logger->info("Stopping layered mode");
327 8 $self->{layered} = 0;
328 8 return;
329 }
330  
331 4 ▶sub layered_clear {
Subroutine layered_clear: called CC=1 SCAR=0.0
Pod documented
332 4 my $self = shift;
333  
334 51 ▶ my $leaf_cb = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
335 51 my ( $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_;
336 51 ▶ $$data_ref ||= $leaf_object->clear_layered;
Condition100%3/3
$$data_ref ||= $leaf_object->clear_layered
l!l&&r!l&&!r
122415
MC/DC100%2/2
$$data_ref ||= $leaf_object->clear_layered
$$data_ref $leaf_object->clear_layered
Truth table100%3/3
A ||= B
A= $$data_refB= $leaf_object->clear_layered
ABresult
1X1
011
000
337 4 };
338  
339 4 $self->_stuff_clear($leaf_cb);
340 4 return;
341 }
342  
343 1675 ▶sub get_data_mode {
Subroutine get_data_mode: called CC=3 SCAR=11.0
Pod documented
344 1675 my $self = shift;
345 return
346 $self->{layered} ? 'layered'
347 1675 ▶ : $self->{preset} ? 'preset'
Branch: 2 branches
BranchTrueFalseTotal
$self->{'layered'} ? : 4 1671 1675
$self->{'preset'} ? : 12 1659 1671
348 : 'normal';
349 }
350  
351 5 ▶sub _stuff_clear {
Subroutine _stuff_clear: called CC=1 SCAR=0.0
352 5 my ( $self, $leaf_cb ) = @_;
353  
354 # this sub may remove hash keys that were entered by user if the
355 # corresponding hash value has no data.
356 # it also clear auto_created ids if there's no data in there
357 30 ▶ my $h_cb = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
358 30 my ( $scanner, $data_ref, $node, $element_name, @keys ) = @_;
359 30 my $obj = $node->fetch_element($element_name);
360  
361 # Since remove method uses splice(array) on list elements, the
362 # removal must be done in reverse order to avoid messing up
363 # the indexes of the array (i.e. the last indexes becomes
364 # greater than the length of the array).
365 30 foreach my $k (reverse @keys) {
366 47 my $has_data = 0;
367 47 $scanner->scan_hash( \$has_data, $node, $element_name, $k );
368 47 ▶ $obj->remove($k) unless $has_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_data 11 36 47
369 47 ▶ $$data_ref ||= $has_data;
Condition100%3/3
$$data_ref ||= $has_data
l!l&&r!l&&!r
24185
MC/DC100%2/2
$$data_ref ||= $has_data
$$data_ref $has_data
Truth table100%3/3
A ||= B
A= $$data_refB= $has_data
ABresult
1X1
011
000
370 }
371 5 };
372  
373 5 my $wiper = Config::Model::ObjTreeScanner->new(
374 auto_vivify => 0,
375 check => 'skip',
376 leaf_cb => $leaf_cb,
377 hash_element_cb => $h_cb,
378 list_element_cb => $h_cb,
379 );
380  
381 5 $wiper->scan_node( undef, $self->config_root );
382  
383 5 return;
384 }
385  
386 2 ▶sub modify ($self, @args) {
Subroutine modify: called CC=3 SCAR=11.4
Pod documented
387 2 my %args = _resolve_arg_shortcut(\@args, 'step');
388 2 ▶ my $force = delete $args{force_save} || delete $args{force};
Condition33%1/3
delete $args{'force_save'} || delete $args{'force'}
l!l&&r!l&&!r
002
MC/DC0%0/2
delete $args{'force_save'} || delete $args{'force'}
delete $args{'force_save'} delete $args{'force'}
Truth table33%1/3
A || B
A= delete $args{'force_save'}B= delete $args{'force'}
ABresult
1X1
011
000
389 2 my $quiet = delete $args{quiet};
390 2 $self->load(%args);
391 2 ▶ $self->say_changes() unless $quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 2 0 2
392 2 $self->write_back( force => $force );
393 2 return $self;
394 }
395  
396 6 ▶sub load ($self, @args) {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
397 6 my %args = _resolve_arg_shortcut(\@args, 'step');
398 6 my $loader = Config::Model::Loader->new( start_node => $self->config_root );
399 6 $loader->load( %args );
400 6 return $self;
401 }
402  
403 0 ▶sub wizard_helper {
Subroutine wizard_helper: not called CC=1 SCAR=6.9
Pod documented
404 0 carp __PACKAGE__, "::wizard_helper helped is deprecated. Call iterator instead";
405 0 goto &iterator;
406 }
407  
408 1 ▶sub iterator ($self, @args) {
Subroutine iterator: called CC=1 SCAR=0.0
Pod documented
409 1 my $tree_root = $self->config_root;
410  
411 1 return Config::Model::Iterator->new( root => $tree_root, @args );
412 }
413  
414 0 ▶sub read_directory {
Subroutine read_directory: not called CC=1 SCAR=6.9
Pod undocumented
415 0 carp "read_directory is deprecated";
416 0 return shift->root_dir;
417 }
418  
419 0 ▶sub write_directory {
Subroutine write_directory: not called CC=1 SCAR=6.9
Pod undocumented
420 0 my $self = shift;
421 0 carp "write_directory is deprecated";
422 0 return $self->root_dir;
423 }
424  
425 0 ▶sub write_root_dir {
Subroutine write_root_dir: not called CC=1 SCAR=6.9
Pod undocumented
426 0 my $self = shift;
427 0 carp "deprecated";
428 0 return $self->root_dir;
429 }
430  
431 # FIXME: record changes to implement undo/redo ?
432 1566 ▶sub notify_change ($self, %args) {
Subroutine notify_change: called CC=5 SCAR=18.4
Pod documented
433 1566 ▶ if ( $change_logger->is_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($change_logger->is_debug) 93 1473 1566
434 93 $change_logger->debug( "in instance ", $self->name, ' for path ', $args{path} );
435 }
436  
437 1566 foreach my $obsolete (qw/note_only msg/) {
438 3132 ▶ if ( my $m = delete $args{$obsolete} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = delete $args{$obsolete}) 0 3132 3132
439 0 carp "notify_change: param $obsolete is obsolete ($m)";
440 0 ▶ $args{note} //='';
Condition0%0/2
$args{"note"} //= ""
l!l
00
MC/DC0%0/1
$args{"note"} //= ""
$args{"note"}
Truth table0%0/2
A
A= $args{"note"}
Aresult
11
00
441 0 $args{note} .= $m;
442 }
443 }
444  
445 1566 $self->add_change( \%args );
446 1566 $self->on_change_cb->( %args );
447 1566 return;
448 }
449  
450 36 ▶sub _truncate {
Subroutine _truncate: called CC=4 SCAR=13.9
451 36 my @lines = @_;
452 36 foreach my $l (@lines) {
453 72 ▶ next unless defined $l;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l 20 52 72
454 52 $l =~ s/\n/ /g;
455 52 ▶ substr ($l, 60) = '[...]' if length $l > 60; # limit string length
Branch: 1 branch
BranchTrueFalseTotal
if length $l > 60 0 52 52
456 }
457 36 return @lines;
458 }
459  
460 18 ▶sub list_changes {
Subroutine list_changes: called CC=12 SCAR=24.9
Pod documented
461 18 my $self = shift;
462 18 my $l = $self->changes;
463 18 my @all;
464  
465 18 foreach my $c (@$l) {
466 36 my $path = $c->{path} ;
467  
468 36 ▶ my $vt = $c->{value_type} || '';
Condition100%2/2
$c->{'value_type'} || ''
l!l
333
MC/DC100%1/1
$c->{'value_type'} || ''
$c->{'value_type'}
Truth table100%2/2
A
A= $c->{'value_type'}
Aresult
11
00
469 36 my ( $o, $n ) = _truncate( $c->{old}, $c->{new} );
470  
471 36 ▶ my $note = $c->{note} ? " # $c->{note}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$c->{'note'} ? : 26 10 36
472  
473 36 ▶ if ( defined $n and not defined $o ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $n and not defined $o) { } 2 34 36
elsif (not defined $n and defined $o) { } 6 28 34
elsif (defined $o and defined $n) { } 22 6 28
elsif (defined $c->{'note'}) { } 6 0 6
Condition100%3/3
defined $n and not defined $o
!ll&&!rl&&r
12222
MC/DC100%2/2
defined $n and not defined $o
defined $n not defined $o
Truth table100%3/3
A and not(B)
A= defined $nB= not defined $o
ABresult
0X0
100
111
474 2 push @all, "$path has new value: '$n'$note";
475 }
476 ▶ elsif ( not defined $n and defined $o) {
Condition100%3/3
not defined $n and defined $o
!ll&&!rl&&r
2266
MC/DC100%2/2
not defined $n and defined $o
not defined $n defined $o
Truth table100%3/3
not(A) and B
A= not defined $nB= defined $o
ABresult
0X0
100
111
477 6 push @all, "$path deleted value: '$o'$note";
478 }
479 ▶ elsif ( defined $o and defined $n ) {
Condition66%2/3
defined $o and defined $n
!ll&&!rl&&r
6022
MC/DC50%1/2
defined $o and defined $n
defined $o defined $n
Truth table66%2/3
A and B
A= defined $oB= defined $n
ABresult
0X0
100
111
480 22 push @all, "$path: '$o' -> '$n'$note";
481 }
482 elsif ( defined $c->{note} ) {
483 6 push @all, "$path: ".$c->{note};
484 }
485 else {
486 # something's unexpected with the call to notify_change
487 0 push @all, "changed ".join(' ', each %$c);
488 }
489 }
490  
491 18 ▶ return wantarray ? @all : join( "\n", @all );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 17 1 18
492 }
493  
494 2 ▶sub say_changes {
Subroutine say_changes: called CC=3 SCAR=11.6
Pod documented
495 2 my $self = shift;
496 2 my @changes = $self->list_changes;
497 2 ▶ return $self unless @changes;
Branch: 1 branch
BranchTrueFalseTotal
unless @changes 0 2 2
498  
499 2 ▶ my $msg = "\n" .
Condition33%1/3
$self->application // $self->name
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->application // $self->name
$self->application $self->name
Truth table33%1/3
A // B
A= $self->applicationB= $self->name
ABresult
1X1
011
000
500 join( "\n- ", "Changes applied to " . ($self->application // $self->name) . " configuration:", @changes ) .
501 "\n";
502  
503 2 $user_logger->info($msg);
504 2 return $self;
505 }
506  
507 38 ▶sub write_back ($self, @args) {
Subroutine write_back: called CC=17 SCAR=37.8
Pod documented
508 38 ▶ my %args =
Branch: 2 branches
BranchTrueFalseTotal
scalar @args > 1 ? : 23 15 38
scalar @args == 1 ? : 0 15 15
509 scalar @args > 1 ? @args
510 : scalar @args == 1 ? ( config_dir => $args[0] )
511 : ();
512  
513 38 ▶ my $force_write = delete $args{force} || 0;
Condition100%2/2
delete $args{'force'} || 0
l!l
1919
MC/DC100%1/1
delete $args{'force'} || 0
delete $args{'force'}
Truth table100%2/2
A
A= delete $args{'force'}
Aresult
11
00
514  
515 38 ▶ if (delete $args{root}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args{"root"}) 0 38 38
516 0 say "write_back: root argument is no longer supported";
517 }
518  
519 # make sure that root node is loaded
520 38 $self->config_root->init;
521  
522 38 ▶ if ($force_write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_write) 19 19 38
523 # make sure that the whole tree is loaded
524 19 my $dump = $self->config_root->dump_tree;
525 }
526  
527 38 foreach my $k ( keys %args ) {
528 2 ▶ if ($k eq 'config_dir') {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'config_dir') { } 0 2 2
elsif ($k ne 'config_file') { } 0 2 2
529 0 ▶ $args{$k} ||= '';
Condition0%0/2
$args{$k} ||= ""
l!l
00
MC/DC0%0/1
$args{$k} ||= ""
$args{$k}
Truth table0%0/2
A
A= $args{$k}
Aresult
11
00
530 0 ▶ $args{$k} .= '/' if $args{$k} and $args{$k} !~ m!/$!;
Branch: 1 branch
BranchTrueFalseTotal
if $args{$k} and not $args{$k} =~ m[/$]u 0 0 0
Condition0%0/3
$args{$k} and not $args{$k} =~ m[/$]u
!ll&&!rl&&r
000
MC/DC0%0/2
$args{$k} and not $args{$k} =~ m[/$]u
$args{$k} not $args{$k} =~ m[/$]u
Truth table0%0/3
A and not(B)
A= $args{$k}B= not $args{$k} =~ m[/$]u
ABresult
0X0
100
111
531 }
532 elsif ( $k ne 'config_file' ) {
533 0 croak "write_back: wrong parameters $k";
534 }
535 }
536  
537 38 ▶ if ($self->has_no_write_back ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_no_write_back) 0 38 38
538 0 ▶ my $info = $self->application ? "the model of application ".$self->application
Branch: 1 branch
BranchTrueFalseTotal
$self->application ? : 0 0 0
539 : "model ".$self->root_class_name ;
540 0 croak "Don't know how to save data of $self->{name} instance. ",
541 "Either $info has no configured ",
542 "read/write backend or no node containing a backend was loaded. ",
543 "Try with -force option or add read/write backend to $info\n";
544 }
545  
546 38 foreach my $path ( sort $self->nodes_to_write_back ) {
547 56 $logger->info("write_back called on node $path");
548  
549 56 ▶ if ( $path and $self->{config_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and $self->{"config_file"}) 0 56 56
Condition66%2/3
$path and $self->{"config_file"}
!ll&&!rl&&r
37190
MC/DC0%0/2
$path and $self->{"config_file"}
$path $self->{"config_file"}
Truth table66%2/3
A and B
A= $pathB= $self->{"config_file"}
ABresult
0X0
100
111
550 0 $logger->warn("write_back: cannot override config_file in non root node ($path)");
551 delete $self->{config_file}
552 0 }
553  
554 56 $self->_write_back_node(%args, path => $path, force_write => $force_write) ;
555 }
556 38 $self->clear_changes;
557 38 return;
558 }
559  
560 56 ▶sub _write_back_node ($self, %args) {
Subroutine _write_back_node: called CC=9 SCAR=22.4
561 56 my $path = delete $args{path};
562 56 my $force_write = delete $args{force_write};
563  
564 56 my $node = $self->config_root->grab(
565 step => $path,
566 type => 'node',
567 mode => 'loose',
568 autoadd => 0,
569 );
570  
571 56 foreach my $wb_info (@{ $self->write_back_node_info($path) }) {
572 56 my ($backend, $cb) = @$wb_info;
573  
574 my @wb_args = (
575 %args,
576 config_file => $self->{config_file},
577 56 force => $force_write,
578 backup => $self->backup,
579 );
580  
581 56 ▶ if (defined $node and ($node->needs_save or $force_write)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node and $node->needs_save || $force_write) 53 3 56
Condition66%4/6
defined $node and $node->needs_save || $force_write
!ll&&!rl&&r
3053
$node->needs_save or $force_write
l!l&&r!l&&!r
32210
MC/DC33%1/3
defined $node and $node->needs_save || $force_write
defined $node $node->needs_save $force_write
Truth table75%3/4
A and B or C
A= defined $nodeB= $node->needs_saveC= $force_write
ABCresult
0XX0
1000
11X1
1011
582 53 my $dir = $args{config_dir};
583 53 ▶ mkpath( $dir, 0, oct(755) ) if $dir and not -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir and not -d $dir 0 53 53
Condition33%1/3
$dir and not -d $dir
!ll&&!rl&&r
5300
MC/DC0%0/2
$dir and not -d $dir
$dir not -d $dir
Truth table33%1/3
A and not(B)
A= $dirB= not -d $dir
ABresult
0X0
100
111
584  
585 # exit when write is successfull
586 53 my $res = $cb->(@wb_args);
587 53 ▶ $logger->info( "write_back called with $backend backend, result is ",
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 53 0 53
588 defined $res ? $res : '<undef>' );
589 }
590  
591 56 ▶ if (not defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 3 53 56
592 3 $logger->debug("deleting file for deleted node $path");
593 3 $cb->(@wb_args, force_delete => 1);
594 3 $self->delete_write_back($path);
595 }
596 }
597  
598 56 $logger->trace( "write_back on node '$path' done" );
599 56 return;
600 }
601  
602 2 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
603 2 goto &write_back;
604 }
605  
606 4 ▶sub update {
Subroutine update: called CC=2 SCAR=8.7
Pod documented
607 4 my ($self, %args) = @_;
608  
609 4 ▶ my $hook = sub {
Subroutine __ANON__: called CC=4 SCAR=23.7
610 4 my ($scanner, $data_ref,$node,@element_list) = @_;
611 4 ▶ if ($node->can('update')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can("update")) 0 4 4
612 0 my $loc = $node->location;
613 0 ▶ say "Calling update on node '$loc'" if $loc and not $args{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $loc and not $args{"quiet"} 0 0 0
Condition0%0/3
$loc and not $args{"quiet"}
!ll&&!rl&&r
000
MC/DC0%0/2
$loc and not $args{"quiet"}
$loc not $args{"quiet"}
Truth table0%0/3
A and not(B)
A= $locB= not $args{"quiet"}
ABresult
0X0
100
111
614 0 push (@$data_ref, $node->update(%args))
615 } ;
616 4 };
617  
618 4 ▶ my $leaf_cb = sub ($scanner, $data_ref,$node,$element_name,$index, $leaf_object) {
Subroutine __ANON__: called CC=1 SCAR=0.0
619 4 push @$data_ref, $leaf_object->update_from_file();
620 4 };
621  
622 4 my $root = $self->config_root ;
623  
624 4 my @msgs ;
625 Config::Model::ObjTreeScanner->new(
626 node_content_hook => $hook,
627 4 ▶ check => ($args{quiet} ? 'no' : 'yes'),
Branch: 1 branch
BranchTrueFalseTotal
$args{'quiet'} ? : 0 4 4
628 leaf_cb => $leaf_cb,
629 file_value_cb => $leaf_cb,
630 dir_value_cb => $leaf_cb,
631 )->scan_node( \@msgs, $root );
632  
633 4 return @msgs;
634 }
635  
636 123 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod documented
637 123 my $self = shift;
638 123 $self->clear_write_back; # avoid reference loops
639 123 return;
640 }
641  
642 59 __PACKAGE__->meta->make_immutable;
643  
644 59 1;
645  
646 # ABSTRACT: Instance of configuration tree
647  
648 __END__
« blib/lib/Config/Model/Iterator.pm ↑ Summary blib/lib/Config/Model/ValueComputer.pm »