Brick/Profile.pm

filter: Statementstmt 97.5% 80 / 82 Branchbran 68.1% 15 / 22 Conditioncond 44.4% 8 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 88.2% 15 / 17 Podpod 100% 10 / 10 totaltotal 81.2% 130 / 160 CC 20 SCAR 21.3
CC
20
Cov
84%
lint
18.3
new
4.1
explain
4.0
CRAP 8.4
« Brick/Filters.pm ↑ Summary Brick/Strings.pm »
1 package Brick::Profile;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Brick;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 $VERSION = '0.904';
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 Brick::Profile - the validation profile for Brick
18  
19 =head1 SYNOPSIS
20  
21  
22  
23 =head1 DESCRIPTION
24  
25 This class turns a profile description into a ready-to-use profile object
26 that has created all of the code it needs to validate input. In Brick
27 parlance, it creates the bucket and the bricks that need to go into the
28 bucket based on the validation description.
29  
30 =head2 Validation profile
31  
32 The validation profile is an array of arrays. Each item in the array specifies
33 three things: a label for that item, the name of the method to run to
34 validate the item, and optional arguments to pass to the the method.
35  
36 For instance, here's a simple validation description to check if a user
37 is registered with the system. This profile has one item:
38  
39 @profile = (
40 [ username => is_registered => { field => form_name } ],
41 );
42  
43 The label for the item is C<username>. When Brick reports the results
44 of the validation, the label C<username> will be attached to the
45 result for this part of the validation.
46  
47 The method that validates this item is C<is_registered>. When you
48 create the profile, Brick will look for that method in either it's
49 included methods in Brick::Bucket classes or the ones you load with
50 C<Brick::add_validator_packages>. This method is called a "brick"
51 because it's one piece of the entire validation.
52  
53 Additionally, Brick will pass the optional arguments, in this case C<{
54 field => form_name }>, to C<is_registered>. A brick merely creates
55 a closure that will run later, so the optional arguments are for
56 the initialization of that closure. The validation doesn't happen
57 until you C<apply> it.
58  
59 =head2 Class methods
60  
61 =over 4
62  
63 =item new( BRICK, ARRAY_OF_ARRAYS )
64  
65 Create a new profile object tied to the Brick object.
66  
67 =cut
68  
69 sub new {
70 4 ▶ my( $class, $brick, $array_ref ) = @_;
Subroutine new: called CC=4 SCAR=14.1
Pod documented
71  
72 4 ▶ unless( $brick->isa( $class->brick_class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($brick->isa($class->brick_class)) 0 4 4
73 0 carp "First argument to \$class->new() must be a brick object. " .
74 "Got [$brick]\n";
75 0 return;
76 }
77  
78 4 my $self = bless {}, $class;
79  
80 4 my $lint_errors = $class->lint( $array_ref );
81  
82 4 ▶ if( ! defined $lint_errors or $lint_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lint_errors or $lint_errors) 1 3 4
Condition66%2/3
not defined $lint_errors or $lint_errors
l!l&&r!l&&!r
013
MC/DC50%1/2
not defined $lint_errors or $lint_errors
not defined $lint_errors $lint_errors
Truth table66%2/3
not(A) or B
A= not defined $lint_errorsB= $lint_errors
ABresult
1X1
011
000
83 1 carp "Profile did not validate!";
84 1 return;
85 }
86  
87 3 my( $bucket, $refs ) = $brick->create_bucket( $array_ref );
88  
89 3 $self->set_bucket( $bucket );
90 3 $self->set_coderefs( $refs );
91 3 $self->set_array( $array_ref );
92  
93 3 return $self;
94 }
95  
96 =item brick_class()
97  
98 Return the class name to use to access class methods (such as
99 bucket_class) in the Brick namespace. If you want to provide
100 an alternate Brick class for your profile, override this method.
101  
102 =cut
103  
104 14 ▶sub brick_class { require Brick; 'Brick' }
Subroutine brick_class: called CC=1 SCAR=0.0
Pod documented
105  
106 =back
107  
108 =head2 Instance methods
109  
110 =over
111  
112 =item lint( PROFILE_ARRAYREF );
113  
114 Examine the profile and complain about irregularities in format. This
115 only checks the format; it does not try to determine if the profile
116 works or makes sense. It returns a hash whose key is the index of the
117 profile element and whose value is an anonymous hash to indicate what
118 had the error:
119  
120 format - the element is an arrayref
121 name - the name is a scalar
122 method - is a code ref or can be found in the package
123 $brick->bucket_class returns
124 args - the last element is a hash reference
125  
126 If the profile is not an array reference, C<lint> immediately returns
127 undef or the empty list. In scalar context, C<lint> returns 0 for
128 format success and the number of errors (so true) for format failures.
129 If there is a format error (e.g. an element is not an array ref), it
130 immediately returns the number of errors up to that point.
131  
132 my $lint = $brick->profile_class->lint( \@profile );
133  
134 print do {
135 if( not defined $lint ) { "Profile must be an array ref\n" }
136 elsif( $lint ) { "Did not validate, had $lint problems" }
137 else { "Woo hoo! Everything's good!" }
138 };
139  
140 In list context, it returns a hash (a list of one element). The result
141 will look something like this hash, which has keys for the elements
142 that lint thinks are bad, and the values are anonymous hashes with
143 keys for the parts that failed:
144  
145 %lint = (
146 1 => {
147 method => "Could not find method foo in package",
148 },
149 4 => {
150 args => "Arguments should be a hash ref, but it was a scalar",
151 }
152 );
153  
154 If you are using C<AUTOLOAD> to generate some of the methods at
155 runtime (i.e. after C<lint> has a chance to check for it), use a
156 C<can> method to let C<lint> know that it will be available later.
157  
158 TO DO:
159  
160 Errors for duplicate names?
161  
162 =cut
163  
164 sub lint {
165 12 ▶ my( $class, $array ) = @_;
Subroutine lint: called CC=14 SCAR=29.1
Pod documented
166  
167 return unless(
168 12 ▶ eval { $array->isa( ref [] ) } or
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $array->isa(ref []) } } or UNIVERSAL::isa($array, ref []) 0 12 12
Condition33%1/3
eval { do { $array->isa(ref []) } } or UNIVERSAL::isa($array, ref [])
l!l&&r!l&&!r
0120
MC/DC0%0/2
eval { do { $array->isa(ref []) } } or UNIVERSAL::isa($array, ref [])
eval { do { $array->isa(ref []) } } UNIVERSAL::isa($array, ref [])
Truth table33%1/3
A or B
A= eval { do { $array->isa(ref []) } }B= UNIVERSAL::isa($array, ref [])
ABresult
1X1
011
000
169 UNIVERSAL::isa( $array, ref [] )
170 );
171  
172 12 my $lint = {};
173  
174 12 foreach my $index ( 0 .. $#$array ) {
175 12 my $h = $lint->{$index} = {};
176  
177 12 ▶ unless( eval { $array->[$index]->isa( ref [] ) } or
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $array->[$index]->isa(ref []) } } or UNIVERSAL::isa($array->[$index], ref [])) 2 10 12
Condition66%2/3
eval { do { $array->[$index]->isa(ref []) } } or UNIVERSAL::isa($array->[$index], ref [])
l!l&&r!l&&!r
0102
MC/DC50%1/2
eval { do { $array->[$index]->isa(ref []) } } or UNIVERSAL::isa($array->[$index], ref [])
eval { do { $array->[$index]->isa(ref []) } } UNIVERSAL::isa($array->[$index], ref [])
Truth table66%2/3
A or B
A= eval { do { $array->[$index]->isa(ref []) } }B= UNIVERSAL::isa($array->[$index], ref [])
ABresult
1X1
011
000
178 UNIVERSAL::isa( $array->[$index], ref [] )
179 ) {
180 2 $h->{format} = "Not an array reference!";
181 2 last;
182 }
183  
184 10 my( $name, $method, $args ) = @{ $array->[$index] };
185  
186 10 ▶ $h->{name} = "Profile name is not a simple scalar!" if ref $name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 10 10
187  
188 $h->{args} = "Couldn't find method [$method]" unless
189 10 ▶ eval { $method->isa( ref sub {} ) } or
Subroutine __ANON__: not called CC=1 SCAR=0.0
190 ▶ UNIVERSAL::isa( $method, sub {} ) or
Subroutine __ANON__: not called CC=1 SCAR=0.0
191 10 ▶ eval { $class->brick_class->bucket_class->can( $method ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $method->isa(ref sub { } ) } } or UNIVERSAL::isa($method, sub { } ) or eval { do { $class->brick_class->bucket_class->can($method) } } 0 10 10
Condition33%2/6
eval { do { $method->isa(ref sub { } ) } } or UNIVERSAL::isa($method, sub { } ) or eval { do { $class->brick_class->bucket_class->can($method) } }
l!l&&r!l&&!r
0100
eval { do { $method->isa(ref sub { } ) } } or UNIVERSAL::isa($method, sub { } )
l!l&&r!l&&!r
0010
MC/DC0%0/3
eval { do { $method->isa(ref sub { } ) } } or UNIVERSAL::isa($method, sub { } ) or eval { do { $class->brick_class->bucket_class->can($method) } }
eval { do { $method->isa(ref sub { } ) } } UNIVERSAL::isa($method, sub { } ) eval { do { $class->brick_class->bucket_class->can($method) } }
Truth table25%1/4
A or B or C
A= eval { do { $method->isa(ref sub { } ) } }B= UNIVERSAL::isa($method, sub { } )C= eval { do { $class->brick_class->bucket_class->can($method) } }
ABCresult
1XX1
01X1
0011
0000
192  
193 $h->{args} = "Args is not a hash reference" unless
194 10 ▶ eval { $args->isa( ref {} ) } or
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $args->isa(ref {}) } } or UNIVERSAL::isa($args, ref {}) 0 10 10
Condition33%1/3
eval { do { $args->isa(ref {}) } } or UNIVERSAL::isa($args, ref {})
l!l&&r!l&&!r
0100
MC/DC0%0/2
eval { do { $args->isa(ref {}) } } or UNIVERSAL::isa($args, ref {})
eval { do { $args->isa(ref {}) } } UNIVERSAL::isa($args, ref {})
Truth table33%1/3
A or B
A= eval { do { $args->isa(ref {}) } }B= UNIVERSAL::isa($args, ref {})
ABresult
1X1
011
000
195 UNIVERSAL::isa( $args, ref {} );
196  
197 # args needs what?
198  
199 10 ▶ delete $lint->{$index} if 0 == keys %{$lint->{$index}};
Branch: 1 branch
BranchTrueFalseTotal
if 0 == keys %{$$lint{$index};} 10 0 10
200 }
201  
202 12 ▶ wantarray ? %$lint : ( scalar keys %$lint );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 11 12
203 }
204  
205 =item explain()
206  
207 Turn the profile into a textual description without applying it to any
208 data. This does not add the profile to instance and it does not add
209 the constraints to the bucket.
210  
211 If everything goes right, this returns a single string that represents
212 the profile.
213  
214 If the profile does not pass the C<lint> test, this returns undef or the
215 empty list.
216  
217 If you want to do something with a datastructure, you probably want to
218 write a different method very similar to this instead of trying to parse
219 the output.
220  
221 Future notes: maybe this is just really a dispatcher to things that do
222 it in different ways (text output, hash output).
223  
224 =cut
225  
226 sub explain {
227 2 ▶ my( $profile ) = @_;
Subroutine explain: called CC=4 SCAR=13.9
Pod documented
228  
229 2 my $bucket = $profile->get_bucket;
230 2 my $coderefs = $profile->get_coderefs;
231 2 my $array = $profile->get_array;
232  
233 my @entries = map {
234 2 my $e = $bucket->get_from_bucket( $_ );
235 2 [ map { $e->$_ } qw(get_coderef get_name) ]
236 } @$coderefs;
237  
238 #print STDERR Data::Dumper->Dump( [ \@entries ], [qw(entries)] );
239  
240 2 my $level = 0;
241 2 my $str = '';
242 2 foreach my $index ( 0 .. $#entries ) {
243 2 my $tuple = $entries[$index];
244  
245 2 my @uses = ( [ $level, $tuple->[0] ] );
246  
247 #print STDERR Data::Dumper->Dump( [ \@uses ], [qw(uses)] );
248  
249 2 ▶ while( my $pair = shift @uses ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pair = shift @uses) 14 2 16
250 14 my $entry = $bucket->get_from_bucket( $pair->[1] );
251 #print Data::Dumper->Dump( [ $entry ], [qw(entry)] );
252 14 ▶ next unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 14 14
253  
254 14 $str .= "\t" x $pair->[0] . $entry->get_name . "\n";
255  
256 unshift @uses, map {
257 12 [ $pair->[0] + 1, $_ ]
258 14 } @{ $entry->get_comprises( $pair->[1] ) };
259 #print Data::Dumper->Dump( [ \@uses ], [qw(uses)] );
260 }
261  
262 2 $str.= "\n";
263 }
264  
265 2 $str;
266 }
267  
268 =item get_bucket
269  
270 =cut
271  
272 sub get_bucket {
273 $_[0]->{bucket}
274 3 ▶ }
Subroutine get_bucket: called CC=1 SCAR=0.0
Pod documented
275  
276 =item set_bucket
277  
278 =cut
279  
280 sub set_bucket {
281 3 ▶ $_[0]->{bucket} = $_[1];
Subroutine set_bucket: called CC=1 SCAR=0.0
Pod documented
282 }
283  
284 =item get_coderefs
285  
286 =cut
287  
288 sub get_coderefs {
289 3 ▶ $_[0]->{coderefs};
Subroutine get_coderefs: called CC=1 SCAR=0.0
Pod documented
290 }
291  
292 =item set_coderefs
293  
294 =cut
295  
296 sub set_coderefs {
297 3 ▶ $_[0]->{coderefs} = $_[1];
Subroutine set_coderefs: called CC=1 SCAR=0.0
Pod documented
298 }
299  
300 =item get_array
301  
302 =cut
303  
304 sub get_array {
305 3 ▶ $_[0]->{array};
Subroutine get_array: called CC=1 SCAR=0.0
Pod documented
306 }
307  
308 =item set_array
309  
310 =cut
311  
312 sub set_array {
313 3 ▶ $_[0]->{array} = $_[1];
Subroutine set_array: called CC=1 SCAR=0.0
Pod documented
314 }
315  
316 =back
317  
318 =head2 Using a different class
319  
320 If you don't want to use this class, you can specify a different class
321 to use in your Brick subclass. Override the Brick::profile_class()
322 method to specify the name of the class that you want to use instead.
323 That might be a subclass or an unrelated class. Your class will need
324 to use the same interface even though it does things differently.
325  
326 =head1 TO DO
327  
328 TBA
329  
330 =head1 SEE ALSO
331  
332 L<Brick::Tutorial>, L<Brick::UserGuide>
333  
334 =head1 SOURCE AVAILABILITY
335  
336 This source is in Github:
337  
338 https://github.com/briandfoy/brick
339  
340 =head1 AUTHOR
341  
342 brian d foy, C<< <briandfoy@pobox.com> >>
343  
344 =head1 COPYRIGHT
345  
346 Copyright © 2007-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
347  
348 You may redistribute this under the terms of the Artistic License 2.0.
349  
350 =cut
351  
352 6 1;
« Brick/Filters.pm ↑ Summary Brick/Strings.pm »