blib/lib/Config/Model/HashId.pm

filter: Statementstmt 88.9% 242 / 272 Branchbran 68.7% 99 / 144 Conditioncond 58.3% 21 / 36 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 95.1% 39 / 41 Podpod 78.9% 15 / 19 totaltotal 79.0% 422 / 534 CC 95 SCAR 21.2
CC
95
Cov
80%
firstkey
20.0
nextkey
20.0
_load_data_from_hash
18.2
CRAP 8.3
« blib/lib/Config/Model/ObjTreeScanner.pm ↑ Summary blib/lib/Config/Model/Role/ComputeFunction.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::HashId 2.169;
11  
12 34 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 34 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 34 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 34 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 34 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 34 subtype 'HaskKeyArray' => as 'ArrayRef' ;
24 2 ▶coerce 'HaskKeyArray' => from 'Str' => via { [$_] } ;
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 34 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 34 my $logger = get_logger("Tree::Element::Id::Hash");
29  
30 34 extends qw/Config::Model::AnyId/;
31  
32 34 with "Config::Model::Role::Grab";
33 34 with "Config::Model::Role::ComputeFunction";
34 # this requires backup method from Config::Model::AnyThing
35 34 with "Config::Model::Role::WarpSubject";
36  
37 247 ▶has data => ( is => 'rw', isa => 'HashRef', default => sub { {}; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
38 has list => (
39 is => 'rw',
40 isa => 'ArrayRef[Str]',
41 traits => ['Array'],
42 247 ▶ default => sub { []; },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 34 handles => {
44 _sort => 'sort_in_place',
45 }
46 );
47  
48 has [qw/default_keys auto_create_keys/] => (
49 is => 'rw',
50 isa => 'HaskKeyArray',
51 coerce => 1,
52 469 ▶ default => sub { []; }
Subroutine __ANON__: called CC=1 SCAR=0.0
53 34 );
54 34 has [qw/ordered write_empty_value/] => ( is => 'ro', isa => 'Bool', default => 0 );
55  
56 247 ▶sub BUILD ($self,$) {
Subroutine BUILD: called CC=2 SCAR=7.2
Pod documented
57  
58 # foreach my $wrong (qw/migrate_values_from/) {
59 # Config::Model::Exception::Model->throw (
60 # object => $self,
61 # error => "Cannot use $wrong with ".$self->get_type." element"
62 # ) if defined $self->{$wrong};
63 # }
64  
65 # could use "required", but we'd get a Moose error instead of a Config::Model
66 # error
67 247 ▶ Config::Model::Exception::Model->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->index_type 0 247 247
68 object => $self,
69 error => "Undefined index_type"
70 ) unless defined $self->index_type;
71  
72 247 return $self;
73 }
74  
75 280 ▶sub set_properties ($self, @args) {
Subroutine set_properties: called CC=3 SCAR=11.5
Pod undocumented
76 280 $self->SUPER::set_properties(@args);
77  
78 279 my $idx_type = $self->{index_type};
79  
80 # remove unwanted items
81 279 my $data = $self->{data};
82  
83 279 my $idx = 1;
84 52 ▶ my $wrong = sub {
Subroutine __ANON__: called CC=8 SCAR=22.1
85 52 my $k = shift;
86 52 ▶ if ( $idx_type eq 'integer' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx_type eq "integer") 42 10 52
87 42 ▶ return 1 if defined $self->{max_index} and $k > $self->{max_index};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max_index"} and $k > $self->{"max_index"} 0 42 42
Condition66%2/3
defined $self->{"max_index"} and $k > $self->{"max_index"}
!ll&&!rl&&r
6360
MC/DC0%0/2
defined $self->{"max_index"} and $k > $self->{"max_index"}
defined $self->{"max_index"} $k > $self->{"max_index"}
Truth table66%2/3
A and B
A= defined $self->{"max_index"}B= $k > $self->{"max_index"}
ABresult
0X0
100
111
88 42 ▶ return 1 if defined $self->{min_index} and $k < $self->{min_index};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"min_index"} and $k < $self->{"min_index"} 0 42 42
Condition66%2/3
defined $self->{"min_index"} and $k < $self->{"min_index"}
!ll&&!rl&&r
6360
MC/DC0%0/2
defined $self->{"min_index"} and $k < $self->{"min_index"}
defined $self->{"min_index"} $k < $self->{"min_index"}
Truth table66%2/3
A and B
A= defined $self->{"min_index"}B= $k < $self->{"min_index"}
ABresult
0X0
100
111
89 }
90 52 ▶ return 1 if defined $self->{max_nb} and $idx++ > $self->{max_nb};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max_nb"} and $idx++ > $self->{"max_nb"} 0 52 52
Condition66%2/3
defined $self->{"max_nb"} and $idx++ > $self->{"max_nb"}
!ll&&!rl&&r
4660
MC/DC0%0/2
defined $self->{"max_nb"} and $idx++ > $self->{"max_nb"}
defined $self->{"max_nb"} $idx++ > $self->{"max_nb"}
Truth table66%2/3
A and B
A= defined $self->{"max_nb"}B= $idx++ > $self->{"max_nb"}
ABresult
0X0
100
111
91 52 return 0;
92 279 };
93  
94 # delete entries that no longer fit the constraints imposed by the
95 # warp mechanism
96 279 foreach my $k ( sort keys %$data ) {
97 52 ▶ next unless $wrong->($k);
Branch: 1 branch
BranchTrueFalseTotal
unless &$wrong($k) 52 0 52
98 0 $logger->trace( "set_properties: ", $self->name, " deleting id $k" );
99 0 delete $data->{$k};
100 }
101 279 return;
102 }
103  
104 1132 ▶sub _migrate ($self) {
Subroutine _migrate: called CC=11 SCAR=24.4
105 1132 ▶ return if $self->{migration_done};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"migration_done"} 767 365 1132
106  
107 # migration must be done *after* initial load to make sure that all data
108 # were retrieved from the file before migration.
109 365 ▶ return if $self->instance->initial_load;
Branch: 1 branch
BranchTrueFalseTotal
if $self->instance->initial_load 122 243 365
110 243 $self->{migration_done} = 1;
111  
112 243 ▶ if ( $self->{migrate_keys_from} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'migrate_keys_from'}) { } 1 242 243
elsif ($self->{'migrate_values_from'}) { } 2 240 242
113 1 my $followed = $self->safe_typed_grab( param => 'migrate_keys_from', check => 'no' );
114 1 ▶ if ( $logger->is_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger->is_debug) 0 1 1
115 0 $logger->debug( $self->name, " migrate keys from ", $followed->name );
116 }
117  
118 1 for my $idx ($followed->fetch_all_indexes) {
119 4 ▶ $self->_store( $idx, undef ) unless $self->_defined($idx);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_defined($idx) 4 0 4
120 }
121 }
122 elsif ( $self->{migrate_values_from} ) {
123 2 my $followed = $self->safe_typed_grab( param => 'migrate_values_from', check => 'no' );
124 2 ▶ $logger->debug( $self->name, " migrate values from ", $followed->name )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_debug 0 2 2
125 if $logger->is_debug;
126 2 foreach my $item ( $followed->fetch_all_indexes ) {
127 5 ▶ next if $self->exists($item); # don't clobber existing entries
Branch: 1 branch
BranchTrueFalseTotal
if $self->exists($item) 0 5 5
128 5 my $data = $followed->fetch_with_id($item)->dump_as_data( check => 'no' );
129 5 $self->fetch_with_id($item)->load_data($data);
130 }
131 }
132 243 return;
133 }
134  
135 560 ▶sub get_type ($self) {
Subroutine get_type: called CC=1 SCAR=0.0
Pod documented
136 560 return 'hash';
137 }
138  
139 1 ▶sub get_info ($self) {
Subroutine get_info: called CC=5 SCAR=16.5
Pod documented
140 1 ▶ my @items = (
Branch: 1 branch
BranchTrueFalseTotal
$self->ordered ? : 0 1 1
141 'type: ' . $self->get_type . ( $self->ordered ? '(ordered)' : '' ),
142 'index: ' . $self->index_type,
143 'cargo: ' . $self->cargo_type,
144 );
145  
146 1 ▶ if ( $self->cargo_type eq 'node' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cargo_type eq "node") 1 0 1
147 1 push @items, "cargo class: " . $self->config_class_name;
148 }
149  
150 1 foreach my $what (qw/min_index max_index max_nb warn_if_key_match warn_unless_key_match/) {
151 5 my $v = $self->$what();
152 5 my $str = $what;
153 5 $str =~ s/_/ /g;
154 5 ▶ push @items, "$str: $v" if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 5 5
155 }
156  
157 1 return @items;
158 }
159  
160 2 ▶sub fetch_gist ($self) {
Subroutine fetch_gist: called CC=2 SCAR=6.9
Pod documented
161 2 my @v = $self->fetch_all_indexes(check => 'no');
162 2 my $str = join(",", @v);
163 2 ▶ $str = substr( $str, 0, 15 ) . '...' if length($str) > 15;
Branch: 1 branch
BranchTrueFalseTotal
if length $str > 15 1 1 2
164 2 return $str;
165 }
166  
167 # important: return the actual size (not taking into account auto-created stuff)
168 483 ▶sub fetch_size ($self) {
Subroutine fetch_size: called CC=1 SCAR=0.0
Pod documented
169 483 return scalar keys %{ $self->{data} };
170 }
171  
172 1184 ▶sub _fetch_all_indexes ($self) {
Subroutine _fetch_all_indexes: called CC=2 SCAR=6.9
173 return $self->{ordered}
174 146 ? @{ $self->{list} }
175 1184 ▶ : sort keys %{ $self->{data} };
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ordered'} ? : 146 1038 1184
176 }
177  
178 # fetch without any check
179 1247 ▶sub _fetch_with_id ($self, $key) {
Subroutine _fetch_with_id: called CC=1 SCAR=0.0
180 1247 return $self->{data}{$key};
181 }
182  
183 # store without any check
184 433 ▶sub _store ($self, $key, $value) {
Subroutine _store: called CC=3 SCAR=11.0
185 431 push @{ $self->{list} }, $key
186 433 ▶ unless exists $self->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$key} 431 2 433
187 433 ▶ $self->notify_change(note => "added entry $key") if $self->write_empty_value;
Branch: 1 branch
BranchTrueFalseTotal
if $self->write_empty_value 14 419 433
188 433 return $self->{data}{$key} = $value;
189 }
190  
191 531 ▶sub _exists ($self, $key) {
Subroutine _exists: called CC=1 SCAR=0.0
192 531 return exists $self->{data}{$key};
193 }
194  
195 2511 ▶sub _defined {
Subroutine _defined: called CC=2 SCAR=6.9
196 2511 my ( $self, $key ) = @_;
197 2511 ▶ return defined $self->{data}{$key} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'data'}{$key} ? : 1692 819 2511
198 }
199  
200 #internal
201 279 ▶sub auto_create_elements ($self) {
Subroutine auto_create_elements: called CC=5 SCAR=16.1
Pod undocumented
202 279 my $auto_p = $self->auto_create_keys;
203 279 ▶ return unless defined $auto_p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $auto_p 272 7 279
204  
205 # create empty slots
206 7 ▶ foreach my $slot ( ref $auto_p ? @$auto_p : ($auto_p) ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $auto_p ? : 7 0 7
207 32 ▶ $self->_store( $slot, undef ) unless exists $self->{data}{$slot};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$slot} 12 20 32
208 }
209  
210 7 return;
211 }
212  
213 # internal
214 892 ▶sub create_default ($self) {
Subroutine create_default: called CC=2 SCAR=6.9
Pod undocumented
215 892 my @temp = keys %{ $self->{data} };
216  
217 892 ▶ return if @temp;
Branch: 1 branch
BranchTrueFalseTotal
if @temp 584 308 892
218  
219 # hash is empty so create empty element for default keys
220 308 my $def = $self->get_default_keys;
221 308 map { $self->_store( $_, undef ) } @$def;
222 308 return;
223 }
224  
225 19 ▶sub _delete ( $self, $key ) {
Subroutine _delete: called CC=1 SCAR=0.0
226 # remove key in ordered list
227 19 @{ $self->{list} } = grep { $_ ne $key } @{ $self->{list} };
228  
229 19 return delete $self->{data}{$key};
230 }
231  
232 6 ▶sub remove ($self, @args) {
Subroutine remove: called CC=1 SCAR=0.0
Pod undocumented
233 6 return $self->delete(@args);
234 }
235  
236 8 ▶sub _clear ($self) {
Subroutine _clear: called CC=1 SCAR=0.0
237 8 $self->{list} = [];
238 8 $self->{data} = {};
239 8 return;
240 }
241  
242 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
243 2 ▶sub sort ($self) {
Subroutine sort: called CC=2 SCAR=7.6
Pod documented
244 2 ▶ if ($self->ordered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ordered) { } 2 0 2
245 2 $self->_sort;
246 }
247 else {
248 0 Config::Model::Exception::User->throw(
249 object => $self,
250 message => "cannot call sort on non ordered hash"
251 );
252 }
253 2 return;
254 }
255  
256 4 ▶sub insort ($self, $id) {
Subroutine insort: called CC=2 SCAR=7.9
Pod documented
257 4 ▶ if ($self->ordered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ordered) { } 4 0 4
258 4 my $elt = $self->fetch_with_id($id);
259 4 $self->_sort;
260 4 return $elt;
261 }
262 else {
263 0 Config::Model::Exception::User->throw(
264 object => $self,
265 message => "cannot call insort on non ordered hash"
266 );
267 }
268 0 return;
269 }
270  
271 # hash only method
272 0 ▶sub firstkey ($self) {
Subroutine firstkey: not called CC=4 SCAR=30.0
Pod documented
273 $self->warp
274 0 ▶ if ( $self->{warp} and @{ $self->{warp_info}{computed_master} } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"warp"} and @{$self->{"warp_info"}{"computed_master"};} 0 0 0
Condition0%0/3
$self->{"warp"} and @{$self->{"warp_info"}{"computed_master"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"warp"} and @{$self->{"warp_info"}{"computed_master"};}
$self->{"warp"} @{$self->{"warp_info"}{"computed_master"};}
Truth table0%0/3
A and B
A= $self->{"warp"}B= @{$self->{"warp_info"}{"computed_master"};}
ABresult
0X0
100
111
275  
276 0 ▶ $self->create_default if defined $self->{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"default"} 0 0 0
277  
278 # reset "each" iterator (to be sure, map is also an iterator)
279 0 my @list = $self->_fetch_all_indexes;
280 0 $self->{each_list} = \@list;
281 0 return shift @list;
282 }
283  
284 # hash only method
285 0 ▶sub nextkey ($self) {
Subroutine nextkey: not called CC=4 SCAR=30.0
Pod documented
286 $self->warp
287 0 ▶ if ( $self->{warp} and @{ $self->{warp_info}{computed_master} } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"warp"} and @{$self->{"warp_info"}{"computed_master"};} 0 0 0
Condition0%0/3
$self->{"warp"} and @{$self->{"warp_info"}{"computed_master"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"warp"} and @{$self->{"warp_info"}{"computed_master"};}
$self->{"warp"} @{$self->{"warp_info"}{"computed_master"};}
Truth table0%0/3
A and B
A= $self->{"warp"}B= @{$self->{"warp_info"}{"computed_master"};}
ABresult
0X0
100
111
288  
289 0 my $res = shift @{ $self->{each_list} };
290  
291 0 ▶ return $res if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 0 0 0
292  
293 # reset list for next call to next_keys
294 0 $self->{each_list} = [ $self->_fetch_all_indexes ];
295  
296 0 return;
297 }
298  
299 2 ▶sub swap ($self, $key1, $key2 ) {
Subroutine swap: called CC=6 SCAR=17.9
Pod documented
300 2 foreach my $k ($key1, $key2) {
301 Config::Model::Exception::User->throw(
302 object => $self,
303 message => "swap: unknow key $k"
304 4 ▶ ) unless exists $self->{data}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$k} 0 4 4
305 }
306  
307 2 my @copy = @{ $self->{list} };
308 2 ▶ for ( my $idx = 0 ; $idx <= $#copy ; $idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $#copy) 6 2 8
309 6 ▶ if ( $copy[$idx] eq $key1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copy[$idx] eq $key1) 2 4 6
310 2 $self->{list}[$idx] = $key2;
311 }
312 6 ▶ if ( $copy[$idx] eq $key2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copy[$idx] eq $key2) 2 4 6
313 2 $self->{list}[$idx] = $key1;
314 }
315 }
316  
317 2 $self->notify_change( note => "swap ordered hash keys '$key1' and '$key2'" );
318 2 return;
319 }
320  
321 9 ▶sub move ($self, $from, $to, %args) {
Subroutine move: called CC=8 SCAR=21.3
Pod documented
322 9 $logger->trace("moving item from $from to $to");
323  
324 Config::Model::Exception::User->throw(
325 object => $self,
326 message => "move: unknow from key $from"
327 9 ▶ ) unless exists $self->{data}{$from};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$from} 1 8 9
328  
329 8 my $ok = $self->check_idx($to);
330  
331 8 my $check = $args{check};
332 8 ▶ if ($ok or $check eq 'no') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ok or $check eq 'no') { } 8 0 8
elsif ($check eq 'yes') { } 0 0 0
Condition33%1/3
$ok or $check eq "no"
l!l&&r!l&&!r
800
MC/DC0%0/2
$ok or $check eq "no"
$ok $check eq "no"
Truth table33%1/3
A or B
A= $okB= $check eq "no"
ABresult
1X1
011
000
333 # this places $to at the end of the ordered list (for ordered hash)
334 8 $self->copy($from, $to);
335  
336 8 $self->notify_change( note => "rename key from '$from' to '$to'" );
337  
338 # data_mode is preset or layered or user. Actually only user
339 # mode makes sense here
340 8 my $imode = $self->instance->get_data_mode;
341 8 $self->set_data_mode( $to, $imode );
342  
343 # find where are $to and $from keys
344 8 my ( $to_idx, $from_idx );
345 8 my $list = $self->{list};
346 8 ▶ for (my $idx = 0; $idx <= $#$list; $idx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $list->$#*) 37 8 45
347 37 ▶ $to_idx = $idx if $list->[$idx] eq $to;
Branch: 1 branch
BranchTrueFalseTotal
if $list->[$idx] eq $to 8 29 37
348 37 ▶ $from_idx = $idx if $list->[$idx] eq $from;
Branch: 1 branch
BranchTrueFalseTotal
if $list->[$idx] eq $from 8 29 37
349 }
350  
351 # replace $from with $to in the order list of the ordered hash
352 # Since $to is clobbered, $from takes its place in the list
353 8 $list->[$from_idx] = $to;
354  
355 # and the obsolete place for $to entry is removed from the list
356 8 splice @$list, $to_idx, 1;
357  
358 8 $self->_delete($from);
359 8 delete $self->{warning_hash}{$from};
360  
361 }
362 elsif ($check eq 'yes') {
363 Config::Model::Exception::WrongValue->throw(
364 0 error => join( "\n\t", @{ $self->{error} } ),
365 object => $self
366 );
367 }
368 8 $logger->debug("Skipped move $from -> $to");
369 8 return $ok;
370 }
371  
372 3 ▶sub move_after ($self, $key_to_move, $ref_key = undef) {
Subroutine move_after: called CC=9 SCAR=22.2
Pod documented
373 3 ▶ if ( not $self->ordered ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ordered) 0 3 3
374 0 $logger->warn("called move_after on unordered hash");
375 0 return;
376 }
377  
378 3 foreach my $k ($key_to_move, $ref_key) {
379 6 ▶ next unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 1 5 6
380 Config::Model::Exception::User->throw(
381 object => $self,
382 message => "swap: unknow key $k"
383 5 ▶ ) unless exists $self->{data}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$k} 0 5 5
384 }
385  
386 # remove the key to move in ordered list
387 3 @{ $self->{list} } = grep { $_ ne $key_to_move } @{ $self->{list} };
388  
389 3 my $list = $self->{list};
390  
391 3 my $msg;
392 3 ▶ if ( defined $ref_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref_key) { } 2 1 3
393 2 ▶ for ( my $idx = 0 ; $idx <= $#$list ; $idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $list->$#*) 6 0 6
394 6 ▶ if ( $list->[$idx] eq $ref_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[$idx] eq $ref_key) 2 4 6
395 2 splice @$list, $idx + 1, 0, $key_to_move;
396 2 last;
397 }
398 }
399  
400 2 $msg = "moved key '$key_to_move' after '$ref_key'";
401 }
402 else {
403 1 unshift @$list, $key_to_move;
404 1 $msg = "moved key '$key_to_move' at beginning";
405 }
406  
407 3 $self->notify_change( note => $msg );
408  
409 3 return;
410 }
411  
412 1 ▶sub move_up ($self, $key) {
Subroutine move_up: called CC=5 SCAR=17.4
Pod documented
413 1 ▶ if ( not $self->ordered ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ordered) 0 1 1
414 0 $logger->warn("called move_up on unordered hash");
415 0 return;
416 }
417  
418 Config::Model::Exception::User->throw(
419 object => $self,
420 message => "move_up: unknow key $key"
421 1 ▶ ) unless exists $self->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$key} 0 1 1
422  
423 1 my $list = $self->{list};
424  
425 # we start from 1 as we can't move up idx 0
426 1 ▶ for ( my $idx = 1 ; $idx < scalar @$list ; $idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < scalar @$list) 1 0 1
427 1 ▶ if ( $list->[$idx] eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[$idx] eq $key) 1 0 1
428 1 $list->[$idx] = $list->[ $idx - 1 ];
429 1 $list->[ $idx - 1 ] = $key;
430 1 $self->notify_change( note => "moved up key '$key'" );
431 1 last;
432 }
433 }
434  
435 # notify_change is placed in the loop so the notification
436 # is not sent if the user tries to move up idx 0
437 1 return;
438 }
439  
440 1 ▶sub move_down ($self, $key) {
Subroutine move_down: called CC=5 SCAR=16.8
Pod documented
441 1 ▶ if ( not $self->ordered ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ordered) 0 1 1
442 0 $logger->warn("called move_down on unordered hash");
443 0 return;
444 }
445  
446 Config::Model::Exception::User->throw(
447 object => $self,
448 message => "move_down: unknown key $key"
449 1 ▶ ) unless exists $self->{data}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$key} 0 1 1
450  
451 1 my $list = $self->{list};
452  
453 # we end at $#$list -1 as we can't move down last idx
454 1 ▶ for ( my $idx = 0 ; $idx < scalar @$list - 1 ; $idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < scalar @$list - 1) 2 0 2
455 2 ▶ if ( $list->[$idx] eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[$idx] eq $key) 1 1 2
456 1 $list->[$idx] = $list->[ $idx + 1 ];
457 1 $list->[ $idx + 1 ] = $key;
458 1 $self->notify_change( note => "moved down key $key" );
459 1 last;
460 }
461 }
462  
463 # notify_change is placed in the loop so the notification
464 # is not sent if the user tries to move past last idx
465 1 return;
466 }
467  
468 20 ▶sub _load_data_from_hash ($self, %args) {
Subroutine _load_data_from_hash: called CC=18 SCAR=29.0
469 20 my $data = $args{data};
470 20 my %backup = %$data ;
471  
472 20 my @ordered_keys;
473 20 my $from = '';
474  
475 20 my $order_key = '__'.$self->element_name.'_order';
476 20 ▶ if ( $self->{ordered} and (defined $data->{$order_key} or defined $data->{__order} )) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'ordered'} and defined $data->{$order_key} || defined $data->{'__order'}) { } 3 17 20
elsif ($self->{'ordered'} and (not $data->{'__skip_order'} and keys %$data > 1)) { } 2 29 31
Condition83%5/6
$self->{"ordered"} and defined $data->{$order_key} || defined $data->{"__order"}
!ll&&!rl&&r
1343
defined $data->{$order_key} or defined $data->{"__order"}
l!l&&r!l&&!r
034
MC/DC66%2/3
$self->{"ordered"} and defined $data->{$order_key} || defined $data->{"__order"}
$self->{"ordered"} defined $data->{$order_key} defined $data->{"__order"}
Truth table75%3/4
A and B or C
A= $self->{"ordered"}B= defined $data->{$order_key}C= defined $data->{"__order"}
ABCresult
0XX0
1000
11X1
1011
477 3 ▶ @ordered_keys = @{ delete $data->{$order_key} or delete $data->{__order} };
Condition33%1/3
delete $data->{$order_key} || delete $data->{'__order'}
l!l&&r!l&&!r
030
MC/DC0%0/2
delete $data->{$order_key} || delete $data->{'__order'}
delete $data->{$order_key} delete $data->{'__order'}
Truth table33%1/3
A || B
A= delete $data->{$order_key}B= delete $data->{'__order'}
ABresult
1X1
011
000
478 3 $from = ' with '.$order_key;
479 }
480 ▶ elsif ( $self->{ordered} and (not $data->{__skip_order} and keys %$data > 1)) {
Condition100%6/6
$self->{"ordered"} and (not $data->{"__skip_order"} and keys %$data > 1)
!ll&&!rl&&r
1322
not $data->{"__skip_order"} and keys %$data > 1
!ll&&!rl&&r
112
MC/DC100%3/3
$self->{"ordered"} and (not $data->{"__skip_order"} and keys %$data > 1)
$self->{"ordered"} not $data->{"__skip_order"} keys %$data > 1
Truth table100%4/4
A and not(B) and C
A= $self->{"ordered"}B= not $data->{"__skip_order"}C= keys %$data > 1
ABCresult
0XX0
10X0
1100
1111
481 2 $logger->warn(
482 "HashId " . $self->location . ": loading ordered "
483 . "hash from hash ref without special key '__order'. Element "
484 . "order is not defined. If needed, this warning can be suppressed by passing "
485 . " key '__skip_order' set to 1."
486 );
487 2 $from = ' without '.$order_key;
488 }
489 20 delete $data->{__skip_order};
490  
491 20 ▶ if (@ordered_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ordered_keys) 3 17 20
492 3 my %data_keys = map { $_ => 1 ; } keys %$data;
493 3 my @left_keys;
494 3 foreach my $k (@ordered_keys) {
495 13 ▶ push @left_keys, $k unless delete $data_keys{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $data_keys{$k} 2 11 13
496 }
497 3 ▶ if ( %data_keys or @left_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (%data_keys or @left_keys) 1 2 3
Condition66%2/3
%data_keys or @left_keys
l!l&&r!l&&!r
012
MC/DC50%1/2
%data_keys or @left_keys
%data_keys @left_keys
Truth table66%2/3
A or B
A= %data_keysB= @left_keys
ABresult
1X1
011
000
498 1 my @msg ;
499 1 ▶ push @msg, "Unlisted keys in __order:", keys %data_keys if %data_keys;
Branch: 1 branch
BranchTrueFalseTotal
if %data_keys 0 1 1
500 1 ▶ push @msg, "Extra keys in __order:", @left_keys if @left_keys;
Branch: 1 branch
BranchTrueFalseTotal
if @left_keys 1 0 1
501 1 Config::Model::Exception::LoadData->throw(
502 object => $self,
503 message => "load_data: ordered keys mistmatch: @msg",
504 wrong_data => \%backup,
505 );
506 }
507 }
508 19 ▶ my @load_keys = @ordered_keys ? @ordered_keys : sort keys %$data;
Branch: 1 branch
BranchTrueFalseTotal
@ordered_keys ? : 2 17 19
509  
510 19 $logger->info(
511 "HashId load_data (" . $self->location .
512 ") will load idx @load_keys from hash ref $from"
513 );
514 19 my $res = 0;
515 19 foreach my $elt (@load_keys) {
516 43 my $obj = $self->fetch_with_id($elt);
517 43 ▶ $res += $obj->load_data( %args, data => $data->{$elt} ) if defined $data->{$elt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{$elt} 42 1 43
518 }
519 19 return !!$res;
520 }
521  
522 22 ▶sub load_data ($self, @args) {
Subroutine load_data: called CC=8 SCAR=22.6
Pod documented
523 22 ▶ my %args = @args > 1 ? @args : ( data => $args[0] );
Branch: 1 branch
BranchTrueFalseTotal
@args > 1 ? : 17 5 22
524 22 my $data = delete $args{data};
525 22 my $check = $self->_check_check( $args{check} );
526  
527 22 ▶ if ( ref($data) eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 20 2 22
elsif (ref $data eq 'ARRAY') { } 2 0 2
elsif (defined $data) { } 0 0 0
528 20 return $self->_load_data_from_hash(data => $data, %args);
529 }
530 elsif ( ref($data) eq 'ARRAY' ) {
531 2 my $res = 0;
532 2 $logger->info(
533 "HashId load_data (" . $self->location . ") will load idx 0..$#$data from array ref" );
534 2 ▶ $self->notify_change( note => "Converted ordered data to non ordered", really => 1) unless $self->ordered;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ordered 0 2 2
535 2 my $idx = 0;
536 2 ▶ while ( $idx < @$data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @$data) 8 2 10
537 8 my $elt = $data->[ $idx++ ];
538 8 my $obj = $self->fetch_with_id($elt);
539 8 $res += $obj->load_data( %args, data => $data->[ $idx++ ] );
540 }
541 2 return !!$res;
542 }
543 elsif ( defined $data ) {
544  
545 # we can skip undefined data
546 0 ▶ my $expected = $self->{ordered} ? 'array' : 'hash';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ordered'} ? : 0 0 0
547 0 Config::Model::Exception::LoadData->throw(
548 object => $self,
549 message => "load_data called with non $expected ref arg",
550 wrong_data => $data,
551 );
552 }
553 0 return;
554 }
555  
556 34 __PACKAGE__->meta->make_immutable;
557  
558 34 1;
559  
560 # ABSTRACT: Handle hash element for configuration model
561  
562 __END__
« blib/lib/Config/Model/ObjTreeScanner.pm ↑ Summary blib/lib/Config/Model/Role/ComputeFunction.pm »