blib/lib/Config/Model/SearchElement.pm

filter: Statementstmt 93.6% 133 / 142 Branchbran 80.4% 37 / 46 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 95.6% 22 / 23 Podpod 75.0% 9 / 12 totaltotal 89.0% 212 / 238 CC 33 SCAR 15.2
CC
33
Cov
90%
_sniff_class
10.0
choose_from_node
6.3
prepare
5.6
CRAP 4.6
« blib/lib/Config/Model/Utils/GenClassPod.pm ↑ Summary blib/lib/Config/Model/ListId.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::SearchElement 2.169;
11 59 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 59 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 59 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 59 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 59 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 59 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 59 my $logger = get_logger("Model::Searcher");
24  
25 8 ▶sub new ($type, %args) {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
26 8 my $self = {};
27 8 foreach my $p (qw/model node/) {
28 16 ▶ $self->{$p} = delete $args{$p}
Branch: 1 branch
BranchTrueFalseTotal
$self->{$p} = delete $args{$p} or croak("Searcher->new: Missing $p parameter") 16 0 16
29 or croak "Searcher->new: Missing $p parameter";
30 }
31  
32 8 bless $self, $type;
33  
34 8 my $root_class = $self->{node}->config_class_name;
35  
36 8 $self->{data} = $self->_sniff_class( $root_class, {} );
37  
38 8 return $self;
39 }
40  
41 # to verify the data structure returned by search_element, you can used
42 # either Data::Dumper or Tk::ObjScanner (both are available on CPAN)
43  
44 103 ▶sub _sniff_class ($self, $class, $found_ref) {
Subroutine _sniff_class: called CC=10 SCAR=23.0
45 103 my %h;
46 103 my $model = $self->{model};
47 103 my $c_model = $model->get_model_clone($class);
48  
49 103 $logger->trace("sniffing config class $class");
50  
51 croak "Recursive config class $class detected, aborting..."
52 103 ▶ if defined $found_ref->{$class};
Branch: 1 branch
BranchTrueFalseTotal
if defined $found_ref->{$class} 0 103 103
53  
54 103 $found_ref->{$class} = 1;
55  
56 103 my @elements = $model->get_element_name(
57 class => $class,
58 );
59  
60 103 foreach my $element (@elements) {
61 529 my $element_model = $c_model->{element}{$element};
62 529 my $element_type = $element_model->{type};
63 529 my $cargo = $element_model->{cargo};
64 529 ▶ my $c_type = defined $cargo ? $cargo->{type} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cargo ? : 55 474 529
65 my $cfg_class_name =
66 defined $cargo
67 ? $cargo->{config_class_name}
68 529 ▶ : $element_model->{config_class_name};
Branch: 1 branch
BranchTrueFalseTotal
defined $cargo ? : 55 474 529
69 529 my %local_found = %$found_ref;
70  
71 529 ▶ if ( $element_type =~ /(warped_)?node/
Branch: 1 branch
BranchTrueFalseTotal
if ($element_type =~ /(warped_)?node/u or $c_type =~ /(warped_)?node/u) { } 76 453 529
Condition100%3/3
$element_type =~ /(warped_)?node/u or $c_type =~ /(warped_)?node/u
l!l&&r!l&&!r
5125453
MC/DC100%2/2
$element_type =~ /(warped_)?node/u or $c_type =~ /(warped_)?node/u
$element_type =~ /(warped_)?node/u $c_type =~ /(warped_)?node/u
Truth table100%3/3
A or B
A= $element_type =~ /(warped_)?node/uB= $c_type =~ /(warped_)?node/u
ABresult
1X1
011
000
72 or $c_type =~ /(warped_)?node/ ) {
73 76 ▶ my $tmp =
Branch: 1 branch
BranchTrueFalseTotal
$element_type eq 'node' || $c_type eq 'node' ? : 57 19 76
Condition100%3/3
$element_type eq "node" or $c_type eq "node"
l!l&&r!l&&!r
322519
MC/DC100%2/2
$element_type eq "node" or $c_type eq "node"
$element_type eq "node" $c_type eq "node"
Truth table100%3/3
A or B
A= $element_type eq "node"B= $c_type eq "node"
ABresult
1X1
011
000
74 $element_type eq 'node' || $c_type eq 'node'
75 ? $self->_sniff_class( $cfg_class_name, \%local_found )
76 : $self->_sniff_warped_node( $element_model, \%local_found );
77  
78 # merge all tmp in %h
79 76 for (keys %$tmp) { $h{$_}{next_step}{$element} = $tmp->{$_}; }
80 }
81 else {
82 453 $h{$element}{next_step}{$element} = '';
83 }
84 }
85 103 $logger->trace("done sniffing config class $class");
86 103 return \%h;
87 }
88  
89 19 ▶sub _sniff_warped_node ($self, $element_model, $found_ref) {
Subroutine _sniff_warped_node: called CC=4 SCAR=13.9
90 19 my %warp_tmp;
91  
92 19 foreach my $apply ( map { $_->{apply} } $element_model->{warp}{rules}->@* ) {
93 38 my $res = $apply->{config_class_name};
94 38 ▶ my $sub_class = ref $res ? $res->[0] : $res;
Branch: 1 branch
BranchTrueFalseTotal
ref $res ? : 0 38 38
95  
96 # sniff all classes mentionned in warped node rules
97 38 my %local_found = %$found_ref;
98 38 my $tmp = $self->_sniff_class( $sub_class, \%local_found );
99  
100 # merge all tmp in %warp_tmp
101 38 for ( keys %$tmp ) { $warp_tmp{$_}{next_class}{$sub_class} = $tmp->{$_}; }
102 }
103  
104 19 return \%warp_tmp;
105 }
106  
107 1 ▶sub get_searchable_elements ($self) {
Subroutine get_searchable_elements: called CC=1 SCAR=0.0
Pod documented
108 1 my @elts = sort keys %{ $self->{data} };
109 1 return @elts;
110 }
111  
112 7 ▶sub prepare ($self, %args) {
Subroutine prepare: called CC=4 SCAR=17.2
Pod documented
113 7 foreach my $p (qw/element/) {
114 7 ▶ $self->{$p} = delete $args{$p}
Branch: 1 branch
BranchTrueFalseTotal
$self->{$p} = delete $args{$p} or croak("Searcher->prepare: Missing $p parameter") 7 0 7
115 or croak "Searcher->prepare: Missing $p parameter";
116 }
117  
118 7 $self->reset; # initialize the search engine
119  
120 7 ▶ unless ( defined $self->{search_tree} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"search_tree"}) 0 7 7
121 0 my $searched = $self->{element};
122 0 my $root_class = $self->{node}->config_class_name;
123 Config::Model::Exception::User->throw(
124 message => "Searcher cannot find element '$searched' "
125 . "from $root_class. Found only "
126 0 . join( ' ', sort keys %{ $self->{data} } ) );
127 }
128  
129 7 return $self;
130 }
131  
132 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
133 10 ▶sub reset ($self) {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
134 10 my $searched = $self->{element};
135 10 $self->{search_tree} = $self->{data}{$searched};
136 10 $self->{current}{object} = $self->{node};
137 10 $self->{current}{element_name} = 'Root';
138 10 $self->{current}{element_type} = 'node';
139 10 return;
140 }
141  
142 0 ▶sub searched ($self) {
Subroutine searched: not called CC=1 SCAR=6.9
Pod documented
143 0 return $self->{element};
144 }
145  
146 23 ▶sub next_step ($self) {
Subroutine next_step: called CC=5 SCAR=16.2
Pod documented
147 23 my $current_obj = $self->{current}{object};
148  
149 23 my @result;
150 23 ▶ if ( $current_obj->get_type =~ /list|hash/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_obj->get_type =~ /list|hash/u) { } 1 22 23
151 1 @result = $current_obj->fetch_all_indexes;
152 }
153 else {
154 22 my $next_step = $self->{search_tree}{next_step};
155  
156 22 ▶ @result =
Branch: 2 branches
BranchTrueFalseTotal
ref $next_step ? : 14 8 22
defined $next_step ? : 0 8 8
157 ref $next_step ? sort keys %$next_step
158 : defined $next_step ? die "next_step error"
159 : ();
160 }
161  
162 #my $name = $self->{current}{element_name} ;
163 #print "From $name, next_step is @result\n";
164 23 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 23 23
165 }
166  
167 3 ▶sub next_choice ($self) {
Subroutine next_choice: called CC=2 SCAR=7.0
Pod documented
168 3 my $result;
169  
170 3 while (1) {
171 7 $result = $self->next_step;
172 7 $logger->trace("next_choice: result is @$result");
173 7 ▶ return $result if scalar @$result != 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$result != 1 3 4 7
174  
175 4 $self->choose(@$result);
176 }
177  
178 0 return;
179 }
180  
181 17 ▶sub choose ($self, $choice) {
Subroutine choose: called CC=2 SCAR=6.9
Pod documented
182 #print "choose $choice from node\n";
183 17 my $obj = $self->{current}{object};
184 17 ▶ if ( $obj->get_type =~ /hash|list/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->get_type =~ /hash|list/u) { } 1 16 17
185 1 return $self->choose_from_id_element($choice);
186 }
187 else {
188 16 return $self->choose_from_node($choice);
189 }
190 }
191  
192 1 ▶sub choose_from_id_element ($self, $choice) {
Subroutine choose_from_id_element: called CC=1 SCAR=0.0
Pod undocumented
193 #print "choose $choice from id\n";
194 1 my $id_obj = $self->{current}{object};
195 1 my $class = $id_obj->config_class_name;
196  
197 # the following line may trigger an exception for warped out
198 # elements
199 1 my $next_node = $id_obj->fetch_with_id($choice);
200  
201 1 $self->{current}{object} = $next_node;
202 1 return $next_node;
203 }
204  
205 16 ▶sub choose_from_node ($self, $choice) {
Subroutine choose_from_node: called CC=6 SCAR=18.3
Pod undocumented
206 #print "choose $choice from node\n";
207 16 my $next = $self->{search_tree}{next_step};
208 16 my $node = $self->{current}{object};
209 16 my $node_class = $node->config_class_name;
210  
211 16 ▶ if ( ref($next) and not defined $next->{$choice} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next and not defined $next->{$choice}) 0 16 16
Condition33%1/3
ref $next and not defined $next->{$choice}
!ll&&!rl&&r
0160
MC/DC0%0/2
ref $next and not defined $next->{$choice}
ref $next not defined $next->{$choice}
Truth table33%1/3
A and not(B)
A= ref $nextB= not defined $next->{$choice}
ABresult
0X0
100
111
212 0 Config::Model::Exception::User->throw( message => "Searcher: wrong choice '$choice' "
213 . "from $node_class. expected "
214 . join( ' ', sort keys %$next ) );
215 }
216  
217 # the following line may trigger an exception for warped out
218 # elements
219 16 my $next_node = $node->fetch_element($choice);
220  
221 # $next is a scalar for leaf element of a ref for node element
222 16 ▶ if ( $next->{$choice} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->{$choice}) { } 9 7 16
223 9 my $data = $next->{$choice};
224  
225 # gobble next_class for warped_node element
226 9 ▶ if ( defined $data->{next_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"next_class"}) 4 5 9
227 4 my $chosen_class = $next_node->config_class_name;
228 4 $data = $data->{next_class}{$chosen_class};
229 4 ▶ unless ( defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 4 4
230 0 Config::Model::Exception::User->throw( message => "Searcher: choice '$choice' "
231 . "from $node_class leads to a warped out node: "
232 . $next_node->warp_error );
233 }
234 }
235  
236 9 $self->{search_tree} = $data;
237 }
238 else {
239 7 $self->{search_tree} = { next_step => undef };
240 7 $next_node = $node->fetch_element($choice);
241 }
242  
243 16 $self->{current}{object} = $next_node;
244 16 $self->{current}{element_type} = $node->element_type($choice);
245 16 $self->{current}{element_name} = $choice;
246 16 return $next_node;
247 }
248  
249 2 ▶sub current_object ($self) {
Subroutine current_object: called CC=1 SCAR=0.0
Pod documented
250 2 return $self->{current}{object};
251 }
252  
253 5 ▶sub auto_choose ($self, $elt_cb, $id_cb) {
Subroutine auto_choose: called CC=3 SCAR=11.0
Pod documented
254 5 my $object = $self->{current}{object};
255 5 while (1) {
256 14 my $next_step = $self->next_step;
257 14 ▶ if ( scalar @$next_step == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$next_step == 0) 5 9 14
258  
259 # found target
260 5 return $self->{current}{object};
261 }
262  
263 9 ▶ my $next_choice =
Branch: 1 branch
BranchTrueFalseTotal
scalar @$next_step == 1 ? : 6 3 9
264 ( scalar @$next_step == 1 ) ? $next_step->[0] : $elt_cb->( $object, @$next_step );
265  
266 9 $self->_auto_choose_elt( $next_choice, $id_cb );
267 }
268 0 return;
269 }
270  
271 9 ▶sub _auto_choose_elt ($self, $next_choice, $id_cb) {
Subroutine _auto_choose_elt: called CC=3 SCAR=11.0
272 9 $self->choose($next_choice);
273  
274 9 my $elt_type = $self->{current}{element_type};
275 9 ▶ if ( $elt_type =~ /list|hash/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt_type =~ /list|hash/u) 2 7 9
276 2 my $object = $self->{current}{object};
277 2 my @choice = $object->fetch_all_indexes();
278  
279 2 ▶ my $id =
Branch: 1 branch
BranchTrueFalseTotal
@choice == 1 ? : 1 1 2
280 @choice == 1
281 ? $choice[0]
282 : $id_cb->( $object, @choice );
283  
284 2 $self->{current}{object} = $object->fetch_with_id($id);
285 }
286 9 return;
287 }
288  
289 59 1;
290  
291 # ABSTRACT: Search an element in a configuration model
292  
293 __END__
« blib/lib/Config/Model/Utils/GenClassPod.pm ↑ Summary blib/lib/Config/Model/ListId.pm »