Params/Validate/Dependencies.pm

filter: Statementstmt 100% 151 / 151 Branchbran 95.2% 40 / 42 Conditioncond 90.0% 27 / 30 MC/DCmcdc 68.7% 11 / 16 Subroutinesub 100% 35 / 35 Podpod 100% 8 / 8 totaltotal 96.4% 272 / 282 SCAR 35.8
CC
36
Cov
98%
validate
9.0
validate_with
6.0
__ANON__
6.0
CRAP 36.0
↑ Summary Data/Domain/Dependencies.pm »
1 package Params::Validate::Dependencies;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Params::Validate (); # don't import yet
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Params::Validate::Dependencies::Documenter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use PadWalker qw(closed_over);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS $DOC);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 $VERSION = '1.40';
17 12 $DOC = 0;
18  
19 # copy and update P::V's EXPORT* constants
20 12 my @_of = qw(any_of all_of none_of one_of);
21 12 @EXPORT = (@Params::Validate::EXPORT, @_of);
22 12 @EXPORT_OK = (@Params::Validate::EXPORT_OK, @_of, 'exclusively');
23 12 %EXPORT_TAGS = (%{clone(\%Params::Validate::EXPORT_TAGS)}, _of => \@_of);
24 12 push @{$EXPORT_TAGS{all}}, (@_of, 'exclusively');
25  
26 # because repeating the call to _validate_factory_args everywhere is BAD
27 12 foreach my $sub (@_of, 'exclusively') {
28 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 60 my $orig = \&{$sub};
31 60 *{$sub} = sub {
32 40 ▶ local *__ANON__ = $sub;
Subroutine __ANON__: called CC=1 SCAR=0.0
33 40 _validate_factory_args(@_);
34 39 $orig->(@_);
35 60 };
36 }
37  
38 sub import {
39 # import all of P::V except validate() and dvalidate_with()
40 15 ▶ Params::Validate->import(grep { ! /^validate(_with)?$/ } @Params::Validate::EXPORT_OK);
Subroutine import: called CC=1 SCAR=0.0
41 # now export all that P::V would have exported, plus *_of
42 15 __PACKAGE__->export_to_level(1, @_);
43 }
44  
45 =head1 NAME
46  
47 Params::Validate::Dependencies - check that the right combination of arguments is passed to a function
48  
49 =head1 DESCRIPTION
50  
51 Extends Params::Validate to make it easy to validate
52 that you have been passed the correct combinations of parameters.
53  
54 =head1 SYNOPSIS
55  
56 This example validates that sub 'foo's arguments are of the right types,
57 and that either we have at least one of alpha, beta and gamma, or
58 we have both of bar amd baz:
59  
60 use Params::Validate::Dependencies qw(:all);
61  
62 sub foo {
63 validate(@_,
64 {
65 alpha => { type => ARRAYREF, optional => 1 },
66 beta => { type => ARRAYREF, optional => 1 },
67 gamma => { type => ARRAYREF, optional => 1 },
68 bar => { type => SCALAR, optional => 1 },
69 baz => { type => SCALAR, optional => 1 },
70 },
71 any_of(
72 qw(alpha beta gamma),
73 all_of(qw(bar baz)),
74 )
75 );
76 }
77  
78 =head1 HOW IT WORKS
79  
80 Params::Validate::Dependencies extends Params::Validate's
81 validate() function to
82 support an arbitrary number of callbacks which are not associated
83 with any one parameter. All of those callbacks are run after
84 Params::Validate's normal validate() function.
85 If any of them return false, then validate() will die as normal.
86  
87 =head1 SUBROUTINES and EXPORTS
88  
89 All of the *_of functions are exported by default in addition to those
90 exported by default by Params::Validate. They are also available with the
91 tag ':_of' in case you want to use them without Params::Validate.
92 In that case you would load the module thus:
93  
94 use Params::Validate::Dependencies qw(:_of);
95  
96 All of the *_of functions take a list of scalars and code-refs and
97 return a code-ref (which is a closure over the list originally passed
98 to the function) suitable for use in validate() or in another *_of
99 function. All code-refs should take as their only argument a hashref
100 of parameters to check, returning true if the parameters are good
101 and false otherwise.
102  
103 =head2 document
104  
105 This takes a code-ref argument as generated by a tree of *_of calls,
106 and spits out some documentation of it. This function is not exported.
107  
108 =cut
109  
110 sub document {
111 38 ▶ my $sub = shift;
Subroutine document: called CC=3 SCAR=11.0
Pod documented
112 38 ▶ die(__PACKAGE__."::document: object $sub is wrong type\n")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($sub) and $sub->can("_document") 6 32 38
Condition66%2/3
builtin::blessed($sub) and $sub->can("_document")
!ll&&!rl&&r
6032
MC/DC50%1/2
builtin::blessed($sub) and $sub->can("_document")
builtin::blessed($sub) $sub->can("_document")
Truth table66%2/3
A and B
A= builtin::blessed($sub)B= $sub->can("_document")
ABresult
0X0
100
111
113 unless(blessed($sub) && $sub->can('_document'));
114 32 $sub->_document();
115 }
116  
117 =head2 validate
118  
119 Overrides and extends Params::Validate's function of the same name.
120  
121 =cut
122  
123 sub validate (\@@) {
124 46 ▶ my @args;
Subroutine validate: called CC=9 SCAR=22.0
Pod documented
125  
126 46 my $p = shift;
127 46 ▶ if ( ref $p eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq "ARRAY") 46 0 46
128 # First argument might have been a hash reference
129 46 ▶ @args = @{ ref $p->[0] ? [ %{ $p->[0] } ] : $p };
Branch: 1 branch
BranchTrueFalseTotal
ref $p->[0] ? : 1 45 46
130 }
131  
132 46 my $pv_spec;
133 46 ▶ if(ref($_[0]) && ref($_[0]) =~ /HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref($_[0]) =~ /HASH/) 23 23 46
Condition66%2/3
ref $_[0] and ref($_[0]) =~ /HASH/
!ll&&!rl&&r
02323
MC/DC50%1/2
ref $_[0] and ref($_[0]) =~ /HASH/
ref $_[0] ref($_[0]) =~ /HASH/
Truth table66%2/3
A and B
A= ref $_[0]B= ref($_[0]) =~ /HASH/
ABresult
0X0
100
111
134 23 $pv_spec = shift;
135 }
136 46 my @coderefs = @_;
137  
138 46 my %rval = @args;
139 # P::V::validate may alter it by applying defaults
140 46 ▶ %rval = Params::Validate::validate(@args, $pv_spec) if($pv_spec);
Branch: 1 branch
BranchTrueFalseTotal
if $pv_spec 23 23 46
141  
142 44 foreach (@coderefs) {
143 41 ▶ die 'code-ref checking failed: arguments were not ' . document($_) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless &$_({@args}) 18 22 40
144 unless $_->({@args});
145 }
146  
147 25 ▶ return wantarray ? %rval : \%rval;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 23 25
148 }
149  
150 =head2 validate_with
151  
152 Overrides and extends Params::Validate's function of the same name.
153  
154 The code-ref, or an array-ref of code-refs, are passed as the
155 extra 'dependencies' argument.
156  
157 =cut
158  
159 sub validate_with {
160 5 ▶ my %args = @_;
Subroutine validate_with: called CC=6 SCAR=17.9
Pod documented
161 5 my $params = [ @{$args{params}} ];
162  
163 5 ▶ $args{dependencies} = [] unless defined $args{dependencies};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"dependencies"} 2 3 5
164 5 my $coderefs = delete $args{dependencies};
165 5 ▶ $coderefs = ref($coderefs) eq 'ARRAY' ? $coderefs : [ $coderefs ];
Branch: 1 branch
BranchTrueFalseTotal
ref $coderefs eq 'ARRAY' ? : 3 2 5
166  
167 5 my %rval = Params::Validate::validate_with(@_);
168  
169 5 foreach (@{$coderefs}) {
170 4 ▶ die('code-ref checking failed: arguments were not '.document($_)."\n") unless($_->({@{$params}}));
Branch: 1 branch
BranchTrueFalseTotal
unless &$_({@{$params;}}) 1 3 4
171 }
172  
173 4 ▶ return wantarray ? %rval : \%rval;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
174 }
175  
176 =head2 exclusively
177  
178 Takes a single subref as its only argument (this would normally be the
179 results of one of the *_of functions), and returns a code-ref which
180 returns true if the hashref it is given only contains fields
181 mentioned in the original function or any of its children. For example
182 ...
183  
184 validate(@_,
185 exclusively(
186 any_of(
187 qw(alpha beta gamma),
188 all_of(qw(bar baz)),
189 )
190 )
191 );
192  
193 will not tolerate arguments such as:
194  
195 bar => ...,
196 baz => ...,
197 sheep => ...
198  
199 because sheep aren't mentioned in the 'any_of' and 'all_of's. Internally
200 this uses the auto-documenter interface to interrogate the child sub. This
201 means that if you want to use C<exclusively()> with third-party extensions
202 then they must support auto-documentation.
203  
204 This function is not exported by default but can be.
205  
206 =cut
207  
208 sub exclusively {
209 1 ▶ my @options = @_;
Subroutine exclusively: called CC=1 SCAR=0.0
Pod documented
210 1 my $childsub = shift;
211 _bless_right_class(
212 sub {
213 7 ▶ my $documentation = document($childsub);
Subroutine __ANON__: called CC=4 SCAR=13.9
214 7 ▶ if($Params::Validate::Dependencies::DOC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Params::Validate::Dependencies::DOC) 3 4 7
215 3 return "exclusively ($documentation)";
216 }
217  
218 my @strings = map {
219 4 s/\\'/'/g; $_
220 } $documentation =~ /
221 '
222 (
223 (?:\\'|[^'])+
224 )
225 '
226 /xg;
227  
228 4 my %params = %{shift()};
229 4 foreach my $param (keys %params) {
230 8 ▶ return 0 if(!grep { $param eq $_ } @strings);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$param eq $_;} @strings 2 6 8
231 }
232 2 return 1;
233 }
234 1 );
235 }
236  
237 =head2 none_of
238  
239 Returns a code-ref which checks that the hashref it receives matches
240 none of the options given.
241  
242 You might want to use it thus:
243  
244 all_of(
245 'alpha',
246 none_of(qw(bar baz))
247 )
248  
249 to validate that 'alpha' must *not* be accompanied by 'bar' or 'baz'.
250  
251 =cut
252  
253 sub none_of {
254 7 ▶ my @options = @_;
Subroutine none_of: called CC=1 SCAR=0.0
Pod documented
255 7 ▶ _bless_right_class(_mk_autodoc(sub { _count_of(\@options, 0)->(@_) }));
Subroutine __ANON__: called CC=1 SCAR=0.0
256 }
257  
258 =head2 one_of
259  
260 Returns a code-ref which checks that the hashref it receives matches
261 only one of the options given.
262  
263 =cut
264  
265 sub one_of {
266 13 ▶ my @options = @_;
Subroutine one_of: called CC=1 SCAR=0.0
Pod documented
267 13 ▶ _bless_right_class(_mk_autodoc(sub { _count_of(\@options, 1)->(@_) }));
Subroutine __ANON__: called CC=1 SCAR=0.0
268 }
269  
270 =head2 any_of
271  
272 Returns a code-ref which checks that the hashref it receives matches
273 one or more of the options given.
274  
275 =cut
276  
277 sub any_of {
278 7 ▶ my @options = @_;
Subroutine any_of: called CC=1 SCAR=0.0
Pod documented
279 _bless_right_class(_mk_autodoc(sub {
280 17 ▶ my %params = %{shift()};
Subroutine __ANON__: called CC=6 SCAR=17.9
281 17 foreach my $option (@options) {
282 43 ▶ return 1 if(!ref($option) && exists($params{$option}));
Branch: 1 branch
BranchTrueFalseTotal
if not ref $option and exists $params{$option} 6 37 43
Condition100%3/3
not ref $option and exists $params{$option}
!ll&&!rl&&r
13246
MC/DC100%2/2
not ref $option and exists $params{$option}
not ref $option exists $params{$option}
Truth table100%3/3
not(A) and B
A= not ref $optionB= exists $params{$option}
ABresult
0X0
100
111
283 37 ▶ return 1 if(ref($option) && $option->(\%params));
Branch: 1 branch
BranchTrueFalseTotal
if ref $option and &$option(\%params) 4 33 37
Condition100%3/3
ref $option and &$option(\%params)
!ll&&!rl&&r
2494
MC/DC100%2/2
ref $option and &$option(\%params)
ref $option &$option(\%params)
Truth table100%3/3
A and B
A= ref $optionB= &$option(\%params)
ABresult
0X0
100
111
284 }
285 7 return 0;
286 7 }));
287 }
288  
289 =head2 all_of
290  
291 Returns a code-ref which checks that the hashref it receives matches
292 all of the options given.
293  
294 =cut
295  
296 sub all_of {
297 11 ▶ my @options = @_;
Subroutine all_of: called CC=1 SCAR=0.0
Pod documented
298  
299 11 ▶ _bless_right_class(_mk_autodoc(sub { _count_of(\@options, $#options + 1)->(@_) }));
Subroutine __ANON__: called CC=1 SCAR=0.0
300 }
301  
302 # {none,one,all}_of are thin wrappers around this
303 sub _count_of {
304 57 ▶ my @options = @{shift()};
Subroutine _count_of: called CC=1 SCAR=0.0
305 57 my $desired_count = shift;
306 sub {
307 57 ▶ my %params = %{shift()};
Subroutine __ANON__: called CC=6 SCAR=17.9
308 57 my $matches = 0;
309 57 foreach my $option (@options) {
310 $matches++ if(
311 125 ▶ (!ref($option) && exists($params{$option})) ||
Branch: 1 branch
BranchTrueFalseTotal
if not ref $option and exists $params{$option} or ref $option and &$option(\%params) 66 59 125
Condition100%9/9
not ref $option and exists $params{$option} or ref $option and &$option(\%params)
l!l&&r!l&&!r
562346
not ref $option and exists $params{$option}
!ll&&!rl&&r
175256
ref $option and &$option(\%params)
!ll&&!rl&&r
52710
MC/DC50%2/4
not ref $option and exists $params{$option} or ref $option and &$option(\%params)
not ref $option exists $params{$option} ref $option &$option(\%params)
Truth table42%3/7
not(A) and B or C and D
A= not ref $optionB= exists $params{$option}C= ref $optionD= &$option(\%params)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
312 (ref($option) && $option->(\%params))
313 );
314 }
315 57 return ($matches == $desired_count);
316 }
317 57 }
318  
319 sub _mk_autodoc {
320 38 ▶ my $sub = shift;
Subroutine _mk_autodoc: called CC=1 SCAR=0.0
321 sub {
322 137 ▶ if($DOC) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($Params::Validate::Dependencies::DOC) 63 74 137
323 63 return $DOC->_doc_me(list => (closed_over($sub))[0]->{'@options'});
324 }
325 74 $sub->(@_);
326 }
327 38 }
328  
329 sub _bless_right_class {
330 39 ▶ my($sub, $class) = (shift(), (caller(1))[3]);
Subroutine _bless_right_class: called CC=2 SCAR=6.9
331 39 (my $subname = $class) =~ s/.*:://;
332 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
333 39 ▶ unless(@{"${class}::ISA"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{"${class}::ISA";}) 25 14 39
334 25 @{"${class}::ISA"} = ('Params::Validate::Dependencies::Documenter');
335 25 ▶ *{"${class}::name"} = sub { $subname };
Subroutine __ANON__: called CC=1 SCAR=0.0
336 25 ▶ *{"${class}::join_with"} = sub { $subname eq 'all_of' ? 'and' : 'or' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$subname eq 'all_of' ? : 17 32 49
337 }
338 39 bless $sub, $class;
339 }
340  
341 sub _validate_factory_args {
342 40 ▶ my @options = @_;
Subroutine _validate_factory_args: called CC=5 SCAR=16.1
343 40 my $sub = (caller(1))[3];
344 die("$sub takes only SCALARs, code-refs, and Params::Validate::Dependencies::* objects\n")
345 if(grep {
346 40 ▶ ref($_) && ref($_) !~ /CODE/ &&
Branch: 2 branches
BranchTrueFalseTotal
if grep {ref $_ and not ref($_) =~ /CODE/ and not builtin::blessed($_) && $_->isa("Params::Validate::Dependencies::Documenter");} @options 1 39 40
ref $_ and not ref($_) =~ /CODE/ and not builtin::blessed($_) && $_->isa("Params::Validate::Dependencies::Documenter") 18 73 91
Condition88%8/9
ref $_ && !(ref($_) =~ /CODE/) && !(builtin::blessed($_) && $_->isa('Params::Validate::Dependencies::Documenter'))
!ll&&!rl&&r
73171
ref $_ and not ref($_) =~ /CODE/
!ll&&!rl&&r
71218
builtin::blessed($_) && $_->isa('Params::Validate::Dependencies::Documenter')
!ll&&!rl&&r
1017
MC/DC75%3/4
ref $_ && !(ref($_) =~ /CODE/) && !(builtin::blessed($_) && $_->isa('Params::Validate::Dependencies::Documenter'))
ref $_ not ref($_) =~ /CODE/ builtin::blessed($_) $_->isa('Params::Validate::Dependencies::Documenter')
Truth table80%4/5
A and not(B) && not(C && D)
A= ref $_B= not ref($_) =~ /CODE/C= builtin::blessed($_)D= $_->isa('Params::Validate::Dependencies::Documenter')
ABCDresult
0XXX0
10XX0
11110
110X1
11101
347 !(blessed($_) && $_->isa('Params::Validate::Dependencies::Documenter'))
348 } @options);
349 }
350  
351 =head1 LIES
352  
353 Some of the above is incorrect. If you really want to know what's
354 going on, look at L<Params::Validate::Dependencies::Extending>.
355  
356 =head1 BUGS, LIMITATIONS, and FEEDBACK
357  
358 I like to know who's using my code. All comments, including constructive
359 criticism, are welcome.
360  
361 Please report any bugs either by email or
362 at L<https://github.com/DrHyde/perl-modules-Params-Validate-Dependencies/issues>.
363  
364 Any incompatibility with Params::Validate will be considered to be a bug,
365 with the exception of minor differences in error messages.
366  
367 Bug reports should contain enough detail that I can replicate the
368 problem and write a test. The best bug reports have those details
369 in the form of a .t file. If you also include a patch I will love
370 you for ever.
371  
372 =head1 SEE ALSO
373  
374 L<Params::Validate>
375  
376 L<Data::Domain>
377  
378 =head1 SOURCE CODE REPOSITORY
379  
380 L<git://github.com/DrHyde/perl-modules-Params-Validate-Dependencies.git>
381  
382 L<https://github.com/DrHyde/perl-modules-Params-Validate-Dependencies/>
383  
384 =head1 COPYRIGHT and LICENCE
385  
386 Copyright 2016 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
387  
388 This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
389  
390 =head1 CONSPIRACY
391  
392 This module is also free-as-in-mason.
393  
394 =cut
395  
396 12 1;
↑ Summary Data/Domain/Dependencies.pm »