blib/lib/OPP/Proc.pm

filter: Statementstmt 18.5% 23 / 124 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 6 / 15 Podpod 0.0% 0 / 9 totaltotal 13.3% 29 / 218 CC 42 SCAR 42.3
CC
42
Cov
12%
value
132.0
delete
132.0
fields
56.0
CRAP 68.5
« blib/lib/OPP/Proc/Regex.pm ↑ Summary blib/lib/OPP/Proc/Uniq.pm »
1 #
2 # $Id: Proc.pm,v 58d7ce835577 2023/03/25 10:34:10 gomor $
3 #
4 package OPP::Proc;
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.00';
9  
10 2 ▶use base qw(OPP);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our @AS = qw(
13 idx
14 options
15 nested
16 state
17 output
18 );
19 2 __PACKAGE__->cgBuildIndices;
20 2 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
21  
22 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #
27 # Always return values as ARRAY, undef when no value found:
28 #
29 sub value {
30 0 ▶ my $self = shift;
Subroutine value: not called CC=11 SCAR=48.8
Pod undocumented
31 0 my ($flat, $field) = @_;
32  
33 0 ▶ croak("value: need flat argument") unless defined($flat);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flat 0 0 0
34 0 ▶ croak("value: need field argument") unless defined($field);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 0 0
35  
36 0 my @value = ();
37  
38 #delete $flat->{data};
39 #print "$field: ".Data::Dumper::Dumper($flat)."\n";
40  
41 # Handle nested fields:
42 0 ▶ if (my $split = $self->is_nested($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $split = $self->is_nested($field)) { } 0 0 0
43 #print "*** is_nested ".$split->[0]." $field\n";
44 #print "*** is_nested ".$split->[1]." $field\n";
45 0 my $root = $split->[0];
46 0 my $leaf = $split->[1];
47 0 ▶ if (defined($leaf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $leaf) 0 0 0
48 0 for (@{$flat->{$root}}) {
49 0 ▶ if (defined($_->{$leaf})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->{$leaf}) 0 0 0
50 0 ▶ my $ary = ref($_->{$leaf}) ? $_->{$leaf} : [ $_->{$leaf} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_->{$leaf} ? : 0 0 0
51 0 push @value, @$ary;
52 }
53 }
54 }
55 }
56 # Handle standard fields:
57 else {
58 0 ▶ if (defined($flat->{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flat->{$field}) 0 0 0
59 0 ▶ my $ary = ref($flat->{$field}) eq 'ARRAY' ? $flat->{$field} : [ $flat->{$field} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $flat->{$field} eq 'ARRAY' ? : 0 0 0
60 0 push @value, @$ary;
61 }
62 }
63  
64 #print "value: ".Data::Dumper::Dumper(\@value)."\n";
65  
66 0 ▶ return @value ? \@value : undef;
Branch: 1 branch
BranchTrueFalseTotal
@value ? : 0 0 0
67 }
68  
69 sub fields {
70 0 ▶ my $self = shift;
Subroutine fields: not called CC=7 SCAR=40.3
Pod undocumented
71 0 my ($flat) = @_;
72  
73 0 ▶ croak("fields: need flat argument") unless defined($flat);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flat 0 0 0
74  
75 0 my @fields = ();
76  
77 0 my $flat_fields = [ map { $_ } keys %$flat ];
78 0 for my $field (@$flat_fields) {
79 0 ▶ if ($self->is_nested($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_nested($field)) { } 0 0 0
80 0 ▶ my $ary = ref($flat->{$field}) eq 'ARRAY' ? $flat->{$field} : [ $flat->{$field} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $flat->{$field} eq 'ARRAY' ? : 0 0 0
81 0 for (@$ary) {
82 0 for my $leaf (keys %$_) {
83 0 push @fields, "$field.$leaf";
84 }
85 }
86 }
87 else {
88 0 push @fields, $field;
89 }
90 }
91  
92 0 return \@fields;
93 }
94  
95 sub values {
96 0 ▶ my $self = shift;
Subroutine values: not called CC=3 SCAR=24.8
Pod undocumented
97 0 my ($flat) = @_;
98  
99 0 ▶ croak("values: need flat argument") unless defined($flat);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flat 0 0 0
100  
101 0 my @values = ();
102 0 my $fields = $self->fields($flat);
103  
104 0 for (@$fields) {
105 0 push @values, $flat->{$_};
106 }
107  
108 0 return \@values;
109 }
110  
111 sub dumper {
112 0 ▶ my $self = shift;
Subroutine dumper: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my ($arg) = @_;
114  
115 0 return Data::Dumper::Dumper($arg)."\n";
116 }
117  
118 #
119 # $self->delete($flat, "domain");
120 # $self->delete($flat, "app.http.component");
121 # $self->delete($flat, "app.http.component.product");
122 #
123 sub delete {
124 0 ▶ my $self = shift;
Subroutine delete: not called CC=11 SCAR=48.8
Pod undocumented
125 0 my ($flat, $field) = @_;
126  
127 0 ▶ croak("delete: need flat argument") unless defined($flat);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flat 0 0 0
128 0 ▶ croak("delete: need field argument") unless defined($field);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 0 0
129  
130 # Handle nested fields:
131 0 ▶ if (my $split = $self->is_nested($field)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $split = $self->is_nested($field)) { } 0 0 0
elsif (not $field =~ /^_/) { } 0 0 0
132 0 my $root = $split->[0];
133 0 my $leaf = $split->[1];
134 # Delete at the leaf level:
135 0 ▶ if ($root !~ m{^_} && defined($leaf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $root =~ /^_/ and defined $leaf) { } 0 0 0
elsif (not $root =~ /^_/) { } 0 0 0
Condition0%0/3
not $root =~ /^_/ and defined $leaf
!ll&&!rl&&r
000
MC/DC0%0/2
not $root =~ /^_/ and defined $leaf
not $root =~ /^_/ defined $leaf
Truth table0%0/3
not(A) and B
A= not $root =~ /^_/B= defined $leaf
ABresult
0X0
100
111
136 0 my @keep = ();
137 0 for my $this (@{$flat->{$root}}) {
138 0 delete $this->{$leaf};
139 0 ▶ push @keep, $this if keys %$this; # Keep the final object only when not empty
Branch: 1 branch
BranchTrueFalseTotal
if keys %$this 0 0 0
140 }
141 # Keep the final array only when not empty
142 0 ▶ if (@keep > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keep > 0) { } 0 0 0
143 0 $flat->{$root} = \@keep;
144 }
145 # And when empty, completly remove the root field:
146 else {
147 0 delete $flat->{$root};
148 }
149 }
150 # Or the complete root field when asked for:
151 elsif ($root !~ m{^_}) {
152 0 delete $flat->{$root};
153 }
154 }
155 # Handle standard fields:
156 elsif ($field !~ m{^_}) {
157 0 delete $flat->{$field};
158 }
159  
160 0 return $flat;
161 }
162  
163 #
164 # $self->set($flat, "domain", "example.com");
165 # $self->set($flat, "app.http.component.product", "HTTP Server");
166 #
167 sub set {
168 0 ▶ my $self = shift;
Subroutine set: not called CC=6 SCAR=37.4
Pod undocumented
169 0 my ($flat, $field, $value) = @_;
170  
171 0 ▶ croak("set: need flat argument") unless defined($flat);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flat 0 0 0
172 0 ▶ croak("set: need field argument") unless defined($field);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 0 0
173 0 ▶ croak("set: need value argument") unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
174  
175 # Handle nested fields:
176 0 ▶ if (my $split = $self->is_nested($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $split = $self->is_nested($field)) { } 0 0 0
177 0 my $root = $split->[0];
178 0 my $leaf = $split->[1];
179 # Delete at the leaf level:
180 0 ▶ if (defined($leaf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $leaf) 0 0 0
181 0 $flat->{$root} = [ { $leaf => $value } ];
182 }
183 }
184 # Handle standard fields:
185 else {
186 0 $flat->{$field} = $value;
187 }
188  
189 0 return $flat;
190 }
191  
192 #
193 # Clone given doc so we can duplicate it and modify on a new one:
194 #
195 sub clone {
196 0 ▶ my $self = shift;
Subroutine clone: not called CC=2 SCAR=17.9
Pod undocumented
197 0 my ($doc) = @_;
198  
199 0 ▶ croak("clone: need doc argument") unless defined($doc);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 0 0
200  
201 0 return Storable::dclone($doc);
202 }
203  
204 #
205 # Will return $arg parsed as usable arguments and also original $arg value:
206 #
207 sub parse {
208 0 ▶ my $self = shift;
Subroutine parse: not called CC=5 SCAR=34.0
Pod undocumented
209 0 my ($args) = @_;
210  
211 0 my @a = Text::ParseWords::quotewords('\s+', 0, $args);
212  
213 # Also keep original value, for use with placeholders, for instance:
214 0 my $parsed = {
215 args => $args,
216 };
217 0 my $idx = 0;
218 0 for (@a) {
219 0 my ($k, $v) = split(/\s*[=:]\s*/, $_, 2);
220 0 ▶ if (defined($k) && defined($v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $k and defined $v) { } 0 0 0
elsif (defined $k) { } 0 0 0
Condition0%0/3
defined $k and defined $v
!ll&&!rl&&r
000
MC/DC0%0/2
defined $k and defined $v
defined $k defined $v
Truth table0%0/3
A and B
A= defined $kB= defined $v
ABresult
0X0
100
111
221 0 $parsed->{$k} = [ sort { $a cmp $b } split(/\s*,\s*/, $v) ];
222 }
223 elsif (defined($k)) {
224 0 $parsed->{$idx++} = $k;
225 }
226 }
227  
228 0 return $parsed;
229 }
230  
231 sub placeholder {
232 0 ▶ my $self = shift;
Subroutine placeholder: not called CC=4 SCAR=30.0
Pod undocumented
233 0 my ($query, $flat) = @_;
234  
235 # Copy original to not modify it:
236 0 my $copy = $query;
237 0 my (@holders) = $query =~ m{[\w\.]+\s*:\s*\$([\w\.]+)}g;
238  
239 # Update search clause with placeholder values
240 0 my %searches = ();
241 0 for my $holder (@holders) {
242 0 my $values = $self->value($flat, $holder);
243 0 for my $value (@$values) {
244 0 ▶ while ($copy =~ s{(\S+)\s*:\s*\$$holder}{$1:$value}) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($copy =~ s/(\S+)\s*:\s*\$$holder/$1:$value/) 0 0 0
245 0 $searches{$copy}++; # Make them unique
246 }
247 }
248  
249 0 return [ keys %searches ];
250 }
251  
252 2 1;
253  
254 __END__
« blib/lib/OPP/Proc/Regex.pm ↑ Summary blib/lib/OPP/Proc/Uniq.pm »