Brick/Result.pm

filter: Statementstmt 19.2% 20 / 104 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 6 / 12 Podpod 100% 6 / 6 totaltotal 17.5% 32 / 182 CC 32 SCAR 42.2
CC
32
Cov
12%
explain
90.9
flatten_by_field
90.0
flatten_by
90.0
CRAP 68.2
↑ Summary Brick/Composers.pm »
1 package Brick::Result;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 $VERSION = '0.904';
9  
10 =encoding utf8
11  
12 =head1 NAME
13  
14 Brick::Result - the result of applying a profile
15  
16 =head1 SYNOPSIS
17  
18 use Brick;
19  
20 my $result = $brick->apply( $Profile, $Input );
21  
22 $result->explain;
23  
24 =head1 DESCRIPTION
25  
26 This class provides methods to turn the data structure returned
27 by apply() into a useable form for particular situations.
28  
29  
30 =head2 Class methods
31  
32 =over 4
33  
34 =item result_item_class
35  
36 Loads and returns the class name to use for the elements of the Results
37 data structure.
38  
39 =cut
40  
41 0 ▶sub result_item_class { require Brick::Result::Item; 'Brick::Result::Item' };
Subroutine result_item_class: not called CC=1 SCAR=6.9
Pod documented
42  
43 =back
44  
45 =head2 Instance methods
46  
47 =over
48  
49 =item explain
50  
51 Create a string the shows the result in an outline form.
52  
53 =cut
54  
55  
56 # for the $pair thing in explain
57 2 ▶use constant LEVEL => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use constant MESSAGE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60  
61 sub explain {
62 0 ▶ my( $result_set ) = @_;
Subroutine explain: not called CC=10 SCAR=45.1
Pod documented
63  
64 0 my $str = '';
65  
66 0 foreach my $element ( @$result_set ) {
67 0 my $level = 0;
68  
69 0 $str .= "$$element[0]: " . do {
70 0 ▶ if( $element->passed ) { "passed " }
Branch: 3 branches
BranchTrueFalseTotal
if ($element->passed) { } 0 0 0
elsif ($element->is_validation_error) { } 0 0 0
elsif ($element->is_code_error) { } 0 0 0
71 0 elsif( $element->is_validation_error ) { "failed " }
72 0 elsif( $element->is_code_error ) { "code error in " }
73 };
74  
75 0 $str .= $element->get_method() . "\n";
76  
77 0 ▶ if( $element->passed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->passed) 0 0 0
78 0 $str .= "\n";
79 0 next;
80 }
81  
82 # this descends into the error tree (without using recursion
83 0 my @uses = ( [ $level, $element->get_messages ] );
84  
85 0 ▶ while( my $pair = shift @uses ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pair = shift @uses) 0 0 0
86 # is it a single error or a composition?
87 0 ▶ if( ! ref $pair->[ MESSAGE ] ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $pair->[1]) { } 0 0 0
elsif (!ref($pair->[1]) eq ref {}) { } 0 0 0
elsif (exists $pair->[1]{'errors'}) { } 0 0 0
88 0 $str .= $pair->[ MESSAGE ] . "foo";
89 }
90 elsif( ! ref $pair->[ MESSAGE ] eq ref {} ) {
91 0 next;
92 }
93 elsif( exists $pair->[ MESSAGE ]->{errors} ) {
94 # something else to process, but put it back into @uses
95 unshift @uses, map {
96 0 [ $pair->[ LEVEL ] + 1, $_ ]
97 0 } @{ $pair->[ MESSAGE ]->{errors} };
98 }
99 else {
100 # this could come back as an array ref instead of a string
101 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 $str .= "\t" . #x $pair->[ LEVEL ] .
103 0 join( ": ", @{ $pair->[ MESSAGE ]
104 0 }{qw(failed_field handler message)} ) . "\n";
105 }
106  
107 }
108  
109 0 $str.= "\n";
110 }
111  
112 0 $str;
113 }
114  
115 =item flatten
116  
117 Collapse the result structure to an array of flat hashes.
118  
119 =cut
120  
121 sub flatten {
122 0 ▶ my( $result_set ) = @_;
Subroutine flatten: not called CC=7 SCAR=40.3
Pod documented
123  
124 0 my $str = '';
125  
126 0 my @flatten;
127  
128 0 foreach my $element ( @$result_set ) { # one element per profile element
129 0 bless $element, $result_set->result_item_class;
130 0 ▶ next if $element->passed;
Branch: 1 branch
BranchTrueFalseTotal
if $element->passed 0 0 0
131 0 my $constraint = $element->get_method;
132  
133 0 my @uses = ( $element->get_messages );
134  
135 0 ▶ while( my $hash = shift @uses ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash = shift @uses) 0 0 0
136 0 ▶ if( ! ref $hash eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ref($hash) eq ref {}) 0 0 0
137 0 carp "Non-hash reference in messages result key! Skipping";
138 0 next;
139 }
140  
141 # is it a single error or a composition?
142 0 ▶ unless( ref $hash ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash) { } 0 0 0
if (do { exists $hash->{'errors'} }) { } 0 0 0
143 0 next;
144 }
145 elsif( exists $hash->{errors} ) {
146 0 unshift @uses, @{ $hash->{errors} };
147 }
148 else {
149 0 push @flatten, { %$hash, constraint => $constraint };
150 }
151 }
152 }
153  
154 0 \@flatten;
155 }
156  
157 =item flatten_by_field
158  
159 Similar to flatten, but keyed by the field that failed the constraint.
160  
161 =cut
162  
163 sub flatten_by_field {
164 0 ▶ my( $result_set ) = @_;
Subroutine flatten_by_field: not called CC=9 SCAR=45.0
Pod documented
165  
166 0 my $str = '';
167  
168 0 my %flatten;
169 my %Seen;
170  
171 0 foreach my $element ( @$result_set ) { # one element per profile element
172 0 ▶ next if $element->passed;
Branch: 1 branch
BranchTrueFalseTotal
if $element->passed 0 0 0
173 0 my $constraint = $element->get_method;
174  
175 0 my @uses = ( $element->get_messages );
176  
177 0 ▶ while( my $hash = shift @uses ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash = shift @uses) 0 0 0
178 # is it a single error or a composition?
179 0 ▶ unless( ref $hash ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash) { } 0 0 0
if (do { exists $hash->{'errors'} }) { } 0 0 0
180 0 next;
181 }
182 elsif( exists $hash->{errors} ) {
183 0 unshift @uses, @{ $hash->{errors} };
184 }
185 else {
186 0 my $field = $hash->{failed_field};
187 0 ▶ next if $hash->{handler} and $Seen{$field}{$hash->{handler}}++;
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"handler"} and $Seen{$field}{$hash->{"handler"}}++ 0 0 0
Condition0%0/3
$hash->{"handler"} and $Seen{$field}{$hash->{"handler"}}++
!ll&&!rl&&r
000
MC/DC0%0/2
$hash->{"handler"} and $Seen{$field}{$hash->{"handler"}}++
$hash->{"handler"} $Seen{$field}{$hash->{"handler"}}++
Truth table0%0/3
A and B
A= $hash->{"handler"}B= $Seen{$field}{$hash->{"handler"}}++
ABresult
0X0
100
111
188 0 ▶ $flatten{ $field } = [] unless exists $flatten{ $field };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $flatten{$field} 0 0 0
189 0 push @{ $flatten{ $field } },
190 { %$hash, constraint => $constraint };
191 0 $Seen{$field}{$hash->{handler}}++;
192 }
193 }
194 }
195  
196 0 \%flatten;
197 }
198  
199 =item flatten_by
200  
201 Similar to flatten, but keyed by the hash key named in the argument list.
202  
203 =cut
204  
205 sub flatten_by {
206 0 ▶ my( $result_set, $key ) = @_;
Subroutine flatten_by: not called CC=9 SCAR=45.0
Pod documented
207  
208 0 my $str = '';
209  
210 0 my %flatten;
211 my %Seen;
212  
213 0 foreach my $element ( @$result_set ) { # one element per profile element
214 0 ▶ next if $element->passed;
Branch: 1 branch
BranchTrueFalseTotal
if $element->passed 0 0 0
215 0 my $constraint = $element->get_method;
216  
217 0 my @uses = ( $element->get_messages );
218  
219 0 ▶ while( my $hash = shift @uses ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash = shift @uses) 0 0 0
220 # is it a single error or a composition?
221 0 ▶ unless( ref $hash ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash) { } 0 0 0
if (do { exists $hash->{'errors'} }) { } 0 0 0
222 0 next;
223 }
224 elsif( exists $hash->{errors} ) {
225 0 unshift @uses, @{ $hash->{errors} };
226 }
227 else {
228 0 my $field = $hash->{$key};
229 0 ▶ next if $hash->{handler} and $Seen{$field}{$hash->{handler}}++;
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"handler"} and $Seen{$field}{$hash->{"handler"}}++ 0 0 0
Condition0%0/3
$hash->{"handler"} and $Seen{$field}{$hash->{"handler"}}++
!ll&&!rl&&r
000
MC/DC0%0/2
$hash->{"handler"} and $Seen{$field}{$hash->{"handler"}}++
$hash->{"handler"} $Seen{$field}{$hash->{"handler"}}++
Truth table0%0/3
A and B
A= $hash->{"handler"}B= $Seen{$field}{$hash->{"handler"}}++
ABresult
0X0
100
111
230 0 ▶ $flatten{ $field } = [] unless exists $flatten{ $field };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $flatten{$field} 0 0 0
231 0 push @{ $flatten{ $field } },
232 { %$hash, constraint => $constraint };
233 0 $Seen{$field}{$hash->{handler}}++;
234 }
235 }
236 }
237  
238 0 \%flatten;
239 }
240  
241 =item dump
242  
243 What should this do?
244  
245 =cut
246  
247 0 ▶sub dump { croak "Not yet implemented" }
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
248  
249 =back
250  
251 =head1 TO DO
252  
253 TBA
254  
255 =head1 SEE ALSO
256  
257 L<Brick::Tutorial>, L<Brick::UserGuide>
258  
259 =head1 SOURCE AVAILABILITY
260  
261 This source is in Github:
262  
263 https://github.com/briandfoy/brick
264  
265 =head1 AUTHOR
266  
267 brian d foy, C<< <briandfoy@pobox.com> >>
268  
269 =head1 COPYRIGHT
270  
271 Copyright © 2007-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
272  
273 You may redistribute this under the terms of the Artistic License 2.0.
274  
275 =cut
276  
277 2 1;
↑ Summary Brick/Composers.pm »