blib/lib/Config/Model/WarpedNode.pm

filter: Statementstmt 93.3% 127 / 136 Branchbran 67.6% 23 / 34 Conditioncond 63.1% 12 / 19 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 96.7% 30 / 31 Podpod 31.2% 5 / 16 totaltotal 81.3% 201 / 247 CC 29 SCAR 18.8
CC
29
Cov
86%
set_properties
17.9
check
7.5
trigger_warp
4.0
CRAP 6.6
« blib/lib/Config/Model/IdElementReference.pm ↑ Summary blib/lib/Config/Model/Backend/Any.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::WarpedNode 2.169;
11  
12 22 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 22 ▶use Carp qw(cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 22 ▶use Config::Model::Warper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 22 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 22 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 22 ▶use Storable qw/dclone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 22 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 22 extends qw/Config::Model::AnyThing/;
25  
26 22 with "Config::Model::Role::NodeLoader";
27 22 with "Config::Model::Role::Grab";
28 # this requires backup method from Config::Model::AnyThing
29 22 with "Config::Model::Role::WarpSubject";
30  
31 22 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 22 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 22 my $logger = get_logger("Tree::Node::Warped");
35  
36 # don't authorize to warp 'morph' parameter as it may lead to
37 # difficult maintenance
38  
39 # status is not warpable either as an obsolete parameter must stay
40 # obsolete
41  
42 22 my @allowed_warp_params = qw/config_class_name level gist/;
43  
44 0 ▶has 'warp' => ( is => 'rw', isa => 'HashRef', default => sub { {}; });
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 22 has 'morph' => ( is => 'ro', isa => 'Bool', default => 0 );
46  
47 22 has warper => ( is => 'rw', isa => 'Config::Model::Warper' );
48  
49 22 my @backup_list = @allowed_warp_params;
50  
51 139 ▶around BUILDARGS => sub ($orig, $class, %args) {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 139 my %h = map { ( $_ => $args{$_} ); } grep { defined $args{$_} } @backup_list;
53 139 return $class->$orig( backup => dclone( \%h ), %args );
54 22 };
55  
56 # used by roles
57 185 ▶sub allowed_warp_params {
Subroutine allowed_warp_params: called CC=1 SCAR=0.0
Pod undocumented
58 185 return @allowed_warp_params;
59 }
60  
61 139 ▶sub BUILD ($self, $) {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
62 # WarpedNode registers this object in a Value object (the
63 # warper). When the warper gets a new value, it modifies the
64 # WarpedNode according to the data passed by the user.
65  
66 139 my $warp_info = $self->warp;
67 139 ▶ $warp_info->{follow} //= {};
Condition100%2/2
$warp_info->{"follow"} //= {}
l!l
1381
MC/DC100%1/1
$warp_info->{"follow"} //= {}
$warp_info->{"follow"}
Truth table100%2/2
A
A= $warp_info->{"follow"}
Aresult
11
00
68 139 ▶ $warp_info->{rules} //= [];
Condition50%1/2
$warp_info->{"rules"} //= []
l!l
1390
MC/DC0%0/1
$warp_info->{"rules"} //= []
$warp_info->{"rules"}
Truth table50%1/2
A
A= $warp_info->{"rules"}
Aresult
11
00
69 139 my $w = Config::Model::Warper->new(
70 warped_object => $self,
71 %$warp_info,
72 allowed => \@allowed_warp_params
73 );
74  
75 139 $self->warper($w);
76 139 return $self;
77 }
78  
79 363 ▶sub config_model ($self) {
Subroutine config_model: called CC=1 SCAR=0.0
Pod undocumented
80 363 return $self->parent->config_model;
81 }
82  
83 # get_type and get_cargo_type are duplicated from Node, because these
84 # methods can be called before the actual node is set up.
85 310 ▶sub get_type {
Subroutine get_type: called CC=1 SCAR=0.0
Pod undocumented
86 310 return 'node';
87 }
88  
89 300 ▶sub get_cargo_type {
Subroutine get_cargo_type: called CC=1 SCAR=0.0
Pod undocumented
90 300 return 'node';
91 }
92  
93 # Forward selected methods (See man perltootc)
94 22 foreach my $method (
95 qw/fetch_element config_class_name copy_from get_element_name
96 get_info fetch_gist has_element is_element_available element_type load
97 fetch_element_value dump_tree needs_save
98 describe get_help get_help_as_text children get set accept_regexp/
99 ) {
100 # to register new methods in package
101 22 ▶ no strict "refs"; ## no critic TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 1238 ▶ *$method = sub ($self,@args) {
Subroutine __ANON__: called CC=2 SCAR=8.1
104 1238 ▶ if ($self->check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check) 1238 0 1238
105 1238 return $self->{data}->$method(@args);
106 }
107  
108 # return undef if no class was warped in
109 0 return ;
110 440 };
111 }
112  
113 2527 ▶sub name ($self) {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
114 2527 return $self->location;
115 }
116  
117 2 ▶sub is_accessible ($self) {
Subroutine is_accessible: called CC=2 SCAR=7.6
Pod documented
118 2 ▶ return defined $self->{data} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'data'} ? : 2 0 2
119 }
120  
121 285 ▶sub get_actual_node ($self) {
Subroutine get_actual_node: called CC=1 SCAR=0.0
Pod documented
122 285 $self->check;
123 285 return $self->{data}; # might be undef
124 }
125  
126 1523 ▶sub check ($self, $check = 'yes') {
Subroutine check: called CC=4 SCAR=20.1
Pod undocumented
127 # must croak if element is not available
128 1523 ▶ if ( not defined $self->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"data"}) 0 1523 1523
129  
130 # a node can be retrieved either for a store operation or for
131 # a fetch.
132 0 ▶ if ($check eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($check eq 'yes') { } 0 0 0
133 0 Config::Model::Exception::User->throw(
134 object => $self,
135 message => "Object '$self->{element_name}' is not accessible.\n\t"
136 . $self->warp_error
137 );
138 }
139 else {
140 0 return 0;
141 }
142 }
143 1523 return 1;
144 }
145  
146 185 ▶sub set_properties ($self, %raw_args) {
Subroutine set_properties: called CC=16 SCAR=28.9
Pod undocumented
147 # merge data passed to set_properties with data passed to the constructor
148 185 my %args = $self->merge_properties(%raw_args);
149  
150 # mega cleanup
151 185 foreach my $awp (@allowed_warp_params) {
152 555 delete $self->{$awp}
153 }
154  
155 185 $logger->trace( $self->name . " set_properties called with ",
156 Data::Dumper->Dump( [ \%args ], ['set_properties_args'] ) );
157  
158 185 my $config_class_name = delete $args{config_class_name};
159 185 ▶ my $node_class = delete $args{class} || 'Config::Model::Node';
Condition50%1/2
delete $args{'class'} || 'Config::Model::Node'
l!l
0185
MC/DC0%0/1
delete $args{'class'} || 'Config::Model::Node'
delete $args{'class'}
Truth table50%1/2
A
A= delete $args{'class'}
Aresult
11
00
160  
161 185 my @prop_args = ( qw/property level element/, $self->element_name );
162  
163 185 my $original_level = $self->config_model->get_element_property(
164 class => $self->parent->config_class_name,
165 @prop_args,
166 );
167  
168 my $next_level =
169 defined $args{level} ? $args{level}
170 185 ▶ : defined $config_class_name ? $original_level
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'level'} ? : 7 178 185
defined $config_class_name ? : 171 7 178
171 : 'hidden';
172  
173 185 ▶ $self->parent->set_element_property( @prop_args, value => $next_level )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->index_value 174 11 185
174 unless defined $self->index_value;
175  
176 185 ▶ unless ( defined $config_class_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config_class_name) 7 178 185
177 7 $self->clear;
178 7 return;
179 }
180  
181 178 my @class_args;
182 178 ▶ ( $config_class_name, @class_args ) = @$config_class_name
Branch: 1 branch
BranchTrueFalseTotal
if ref $config_class_name 2 176 178
183 if ref $config_class_name;
184  
185 # check if some action is needed (ie. create or morph node)
186 return
187 if defined $self->{config_class_name}
188 178 ▶ and $self->{config_class_name} eq $config_class_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"config_class_name"} and $self->{"config_class_name"} eq $config_class_name 0 178 178
Condition33%1/3
defined $self->{"config_class_name"} and $self->{"config_class_name"} eq $config_class_name
!ll&&!rl&&r
17800
MC/DC0%0/2
defined $self->{"config_class_name"} and $self->{"config_class_name"} eq $config_class_name
defined $self->{"config_class_name"} $self->{"config_class_name"} eq $config_class_name
Truth table33%1/3
A and B
A= defined $self->{"config_class_name"}B= $self->{"config_class_name"} eq $config_class_name
ABresult
0X0
100
111
189  
190 178 my $old_object = $self->{data};
191 178 my $old_config_class_name = $self->{config_class_name};
192  
193 # create a new object from scratch
194 178 my $new_object = $self->create_node( $config_class_name, @class_args );
195  
196 178 $self->{config_class_name} = $config_class_name;
197 178 $self->{data} = $new_object;
198  
199 178 ▶ if ( defined $old_object and $self->{morph} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_object and $self->{"morph"}) 26 152 178
Condition100%3/3
defined $old_object and $self->{"morph"}
!ll&&!rl&&r
1371526
MC/DC100%2/2
defined $old_object and $self->{"morph"}
defined $old_object $self->{"morph"}
Truth table100%3/3
A and B
A= defined $old_objectB= $self->{"morph"}
ABresult
0X0
100
111
200  
201 # there an old object that we need to translate
202 26 ▶ $logger->debug( "WarpedNode: morphing ", $old_object->name, " to ", $new_object->name )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_debug 0 26 26
203 if $logger->is_debug;
204  
205 26 $new_object->copy_from( from => $old_object, check => 'skip' );
206 }
207  
208 # bringing a new object does not really modify the content of the config tree.
209 # only changes underneath changes the tree. And these changes below triggers
210 # their own change notif. So there's no need to call notify_change when transitioning
211 # from an undef object into a real object. On the other hand, warping out an object does
212 # NOT trigger notify_changes from below. So notify_change must be called
213 178 ▶ if ( defined $old_object and $old_config_class_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_object and $old_config_class_name) 0 178 178
Condition66%2/3
defined $old_object and $old_config_class_name
!ll&&!rl&&r
137410
MC/DC0%0/2
defined $old_object and $old_config_class_name
defined $old_object $old_config_class_name
Truth table66%2/3
A and B
A= defined $old_objectB= $old_config_class_name
ABresult
0X0
100
111
214 0 my $from = $old_config_class_name ;
215 0 ▶ my $to = $config_class_name // '<undef>';
Condition0%0/2
$config_class_name // "<undef>"
l!l
00
MC/DC0%0/1
$config_class_name // "<undef>"
$config_class_name
Truth table0%0/2
A
A= $config_class_name
Aresult
11
00
216 0 $self->notify_change( note => "warped node from $from to $to" );
217 }
218  
219 # need to call trigger on all registered objects only after all is setup
220 178 $self->trigger_warp;
221  
222 178 return;
223 }
224  
225 178 ▶sub create_node ($self, $config_class_name) {
Subroutine create_node: called CC=2 SCAR=6.9
Pod undocumented
226 my @args = (
227 config_class_name => $config_class_name,
228 instance => $self->{instance},
229 element_name => $self->{element_name},
230 178 parent => $self->parent,
231 container => $self->container,
232 );
233  
234 178 ▶ push @args, index_value => $self->index_value if defined $self->index_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->index_value 9 169 178
235  
236 178 return $self->load_node(@args);
237 }
238  
239 7 ▶sub clear ($self) {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
240 7 delete $self->{data};
241 7 return;
242 }
243  
244 4 ▶sub load_data ($self, @args) {
Subroutine load_data: called CC=3 SCAR=11.8
Pod documented
245 4 ▶ my %args = @args > 1 ? @args : ( data => $args[0] );
Branch: 1 branch
BranchTrueFalseTotal
@args > 1 ? : 4 0 4
246 4 my $data = $args{data};
247 4 my $check = $self->_check_check( $args{check} );
248  
249 4 ▶ if ( ref($data) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "HASH") 0 4 4
250 0 Config::Model::Exception::LoadData->throw(
251 object => $self,
252 message => "load_data called with non hash ref arg",
253 wrong_data => $data,
254 );
255 }
256  
257 4 return $self->get_actual_node->load_data(%args);
258 }
259  
260 3 ▶sub is_auto_write_for_type ($self, @args) {
Subroutine is_auto_write_for_type: called CC=1 SCAR=0.0
Pod undocumented
261 3 $self->get_actual_node->is_auto_write_for_type(@args);
262 3 return;
263 }
264  
265 # register warper that goes through this path when looking for warp master value
266 128 ▶sub register ($self, $warped, $w_idx) {
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
267 128 $logger->debug( "WarpedNode: " . $self->name, " registered " . $warped->name );
268  
269 # weaken only applies to the passed reference, and there's no way
270 # to duplicate a weak ref. Only a strong ref is created. See
271 # qw(weaken) module for weaken()
272 128 my @tmp = ( $warped, $w_idx );
273 128 weaken( $tmp[0] );
274 128 push @{ $self->{warp_these_objects} }, \@tmp;
275 128 return;
276 }
277  
278 178 ▶sub trigger_warp ($self) {
Subroutine trigger_warp: called CC=4 SCAR=13.9
Pod undocumented
279 # warp_these_objects is modified by the calls below, so this copy
280 # must be done before the loop
281 178 ▶ my @list = @{ $self->{warp_these_objects} || [] };
Condition100%2/2
$self->{'warp_these_objects'} || []
l!l
2176
MC/DC100%1/1
$self->{'warp_these_objects'} || []
$self->{'warp_these_objects'}
Truth table100%2/2
A
A= $self->{'warp_these_objects'}
Aresult
11
00
282  
283 178 foreach my $ref (@list) {
284 3 my ( $warped, $warp_index ) = @$ref;
285 3 ▶ next unless defined $warped; # $warped is a weak ref and may vanish
Branch: 1 branch
BranchTrueFalseTotal
unless defined $warped 0 3 3
286  
287 # pure warp of object
288 3 $logger->debug( "node trigger_warp: from '",
289 $self->name, "' warping '", $warped->name, "'" );
290  
291 # FIXME: this does not trigger new registration (or removal thereof)...
292 3 $warped->refresh_affected_registrations( $self->location );
293  
294 #$warped->refresh_values_from_master ;
295 3 $warped->do_warp;
296 3 $logger->debug( "node trigger_warp: from '",
297 $self->name, "' warping '", $warped->name, "' done" );
298 }
299 178 return;
300 }
301  
302 # FIXME: should we un-register ???
303  
304 22 1;
305  
306 # ABSTRACT: Node that change config class properties
307  
308 __END__
« blib/lib/Config/Model/IdElementReference.pm ↑ Summary blib/lib/Config/Model/Backend/Any.pm »