File Coverage

blib/lib/Data/Domain/Dependencies.pm
Criterion Covered Total %
statement 44 44 100.0
branch 7 10 70.0
condition 3 6 50.0
subroutine 14 14 100.0
pod 4 4 100.0
total 72 78 92.3


line stmt bran cond sub pod time code
1             package Data::Domain::Dependencies;
2              
3 5     5   245787 use strict;
  5         23  
  5         131  
4 5     5   26 use warnings;
  5         9  
  5         148  
5              
6 5     5   681 use Params::Validate::Dependencies qw(:_of exclusively);
  5         14  
  5         30  
7 5     5   36 use Scalar::Util qw(blessed);
  5         11  
  5         226  
8 5     5   28 use Exporter qw(import);
  5         10  
  5         169  
9              
10 5     5   29 use base qw(Data::Domain);
  5         19  
  5         2933  
11              
12 5     5   596175 use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
  5         11  
  5         1032  
13             $VERSION = '1.30';
14              
15             @EXPORT = ();
16             @EXPORT_OK = (
17             @{$Params::Validate::Dependencies::EXPORT_TAGS{_of}},
18             qw(exclusively Dependencies)
19             );
20             %EXPORT_TAGS = (all => \@EXPORT_OK);
21              
22             =head1 NAME
23              
24             Data::Domain::Dependencies
25              
26             =head1 DESCRIPTION
27              
28             A sub-class of Data::Domain which provides functions and objects
29             to let Data::Domain use the same
30             functions as Params::Validate::Dependencies.
31              
32             NB now this only works on perl 5.10 and higher as Data::Domain started
33             using some features of more modern perls.
34              
35             =head1 SYNOPSIS
36              
37             This creates a domain which, when passed a hashref to inspect, will
38             check that it contains at least one of an 'alpha' or 'beta' key, or
39             both of 'foo' and 'bar'.
40              
41             use Data::Domain::Dependencies qw(:all);
42              
43             my $domain = Dependencies(
44             any_of(
45             qw(alpha beta),
46             all_of(qw(foo bar))
47             )
48             );
49              
50             my $errors = $domain->inspect(\%somehash);
51              
52             =head1 SUBROUTINES and EXPORTS
53              
54             Nothing is exported by default, but you can export any of the *_of
55             functions of Params::Validate::Dependencies, and the 'Dependencies'
56             and 'exclusively' functions. They are all available under the 'all' tag.
57              
58             =head2 Dependencies
59              
60             This takes a code-ref argument as returned by the *_of functions.
61              
62             It returns an object which is a sub-class of Data::Domain::Dependencies
63             and so has an 'inspect' method that you can use to check for errors
64             when passing it a hash-ref.
65              
66             =cut
67              
68             sub Dependencies {
69 8     8 1 258 my $sub = shift;
70 8         39 __PACKAGE__->new($sub);
71             }
72              
73             =head2 new
74              
75             'Dependencies' above is really just a thin wrapper around this
76             constructor. You are encouraged to not call this directly.
77              
78             =cut
79              
80             sub new {
81 8     8 1 18 my($class, $sub) = @_;
82 8 50 33     115 die("$class constructor must be passed a Params::Validate::Dependencies object or a code-ref\n")
      66        
83             unless(ref($sub) =~ /CODE/ || (blessed($sub) && $sub->isa('Params::Validate::Dependencies::Documenter')));
84 8 100       41 if(blessed($sub)) {
85 7         33 my $target_class = "${class}::".$sub->name();
86 5     5   70 no strict 'refs';
  5         13  
  5         1361  
87 7 50       15 unless(@{"${target_class}::ISA"}) {
  7         63  
88             # multiple inheritance so we can get at Data::Domain->inspect()
89             # and Params::Validate::Dependencies::Documenter->_document()
90 7         20 @{"${target_class}::ISA"} = (
  7         163  
91             'Data::Domain::Dependencies',
92             blessed($sub)
93             );
94             }
95 7     15   85 return bless sub { $sub->(@_) }, $target_class;
  15         46  
96             } else {
97 1     2   10 return bless sub { $sub->(@_) }, $class;
  2         8  
98             }
99             }
100              
101             =head2 generate_documentation
102              
103             This is an additional method, not found in Data::Domain, which
104             generates vaguely readable
105             documentation for the domain. Broadly speaking, it spits out the
106             source code.
107              
108             =cut
109              
110             sub generate_documentation {
111 6     6 1 68 my $self = shift;
112 6         66 $self->_document();
113             }
114              
115             # this is where the magic happens ...
116             sub inspect {
117 12     12 1 55 my $sub = shift;
118 12         18 my $data = shift;
119 12 50       64 return __PACKAGE__." can only inspect hashrefs\n"
120             unless(ref($data) =~ /HASH/i);
121              
122 12 100       246 return $sub->($data) ? () : __PACKAGE__.": validation failed";
123             }
124              
125             =head1 LIES
126              
127             Some of the above is incorrect. If you really want to know what's
128             going on, look at L.
129              
130             =head1 BUGS, LIMITATIONS, and FEEDBACK
131              
132             I like to know who's using my code. All comments, including constructive
133             criticism, are welcome.
134              
135             Please report any bugs either by email
136             or at L.
137              
138             Bug reports should contain enough detail that I can replicate the
139             problem and write a test. The best bug reports have those details
140             in the form of a .t file. If you also include a patch I will love
141             you for ever.
142              
143             =head1 SEE ALSO
144              
145             L
146              
147             L
148              
149             =head1 SOURCE CODE REPOSITORY
150              
151             L
152              
153             L
154              
155             =head1 COPYRIGHT and LICENCE
156              
157             Copyright 2011 David Cantrell EFE
158              
159             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.
160              
161             =head1 CONSPIRACY
162              
163             This module is also free-as-in-mason.
164              
165             =cut
166              
167             1;