File Coverage

blib/lib/Params/Validate/Dependencies/all_or_none_of.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 4 100.0
condition 11 12 91.6
subroutine 8 8 100.0
pod 1 3 33.3
total 47 50 94.0


line stmt bran cond sub pod time code
1             package Params::Validate::Dependencies::all_or_none_of;
2              
3 2     2   243678 use strict;
  2         7  
  2         84  
4 2     2   49 use warnings;
  2         4  
  2         69  
5              
6 2     2   10 use base qw(Exporter Params::Validate::Dependencies::Documenter);
  2         8  
  2         288  
7              
8 2     2   12 use vars qw($VERSION @EXPORT @EXPORT_OK);
  2         4  
  2         625  
9              
10             $VERSION = '1.0';
11             @EXPORT_OK = @EXPORT = ('all_or_none_of');
12              
13             =head1 NAME
14              
15             Params::Validate::Dependencies::all_or_none_of
16              
17             =head1 DESCRIPTION
18              
19             An extension for Params::Validate::Dependencies to validate that either
20             all of or none of a list of params are present.
21              
22             =head1 SYNOPSIS
23              
24             In this example, the 'foo' function takes named arguments, of which
25             the 'day', 'month', and 'year' args must either all be present or
26             none of them be present.
27              
28             use Params::Validate::Dependencies qw(:all);
29             use Params::Validate::Dependencies::all_or_none_of;
30              
31             sub foo {
32             validate(@_,
33             { ... normal Params::Validate stuff ...},
34             all_or_none_of(qw(day month year))
35             );
36             }
37              
38             =head1 SUBROUTINES and EXPORTS
39              
40             =head2 all_or_none_of
41              
42             This is exported by default. It takes a list of scalars and code-refs
43             and returns a code-ref which checks that the hashref it receives matches
44             either all or none of the options given.
45              
46             =cut
47              
48             sub all_or_none_of {
49 3     3 1 606 my @options = @_;
50             return bless sub {
51 24     24   10660 my $hashref = shift;
52 24 100       74 if($Params::Validate::Dependencies::DOC) {
53 2         241 return $Params::Validate::Dependencies::DOC->_doc_me(list => \@options);
54             }
55 22         31 my $count = 0;
56 22         35 foreach my $option (@options) {
57 60 100 100     368 $count++ if(
      100        
      66        
58             (!ref($option) && exists($hashref->{$option})) ||
59             (ref($option) && $option->($hashref))
60             );
61             }
62 22   100     228 return ($count == 0 || $count == $#options + 1);
63 3         58 }, __PACKAGE__;
64             }
65              
66 2     2 0 87 sub join_with { return 'or'; }
67 3     3 0 83 sub name { return 'all_or_none_of'; }
68              
69             =head1 LIES
70              
71             Some of the above is incorrect. If you really want to know what's
72             going on, look at L.
73              
74             =head1 BUGS, LIMITATIONS, and FEEDBACK
75              
76             I like to know who's using my code. All comments, including constructive
77             criticism, are welcome.
78              
79             Please report any bugs either by email or using L
80             or at L.
81              
82             Bug reports should contain enough detail that I can replicate the
83             problem and write a test. The best bug reports have those details
84             in the form of a .t file. If you also include a patch I will love
85             you for ever.
86              
87             =head1 SEE ALSO
88              
89             L
90              
91             L
92              
93             =head1 SOURCE CODE REPOSITORY
94              
95             L
96              
97             L
98              
99             =head1 COPYRIGHT and LICENCE
100              
101             Copyright 2011 David Cantrell EFE
102              
103             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.
104              
105             =head1 CONSPIRACY
106              
107             This module is also free-as-in-mason.
108              
109             =cut
110              
111             1;