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   182806 use strict;
  5         12  
  5         124  
4 5     5   23 use warnings;
  5         10  
  5         126  
5              
6 5     5   676 use Params::Validate::Dependencies qw(:_of exclusively);
  5         12  
  5         24  
7 5     5   29 use Scalar::Util qw(blessed);
  5         10  
  5         217  
8 5     5   27 use Exporter qw(import);
  5         11  
  5         134  
9              
10 5     5   26 use base qw(Data::Domain);
  5         9  
  5         2313  
11              
12 5     5   604228 use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
  5         16  
  5         873  
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 302 my $sub = shift;
70 8         34 __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 25 my($class, $sub) = @_;
82 8 50 33     128 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       40 if(blessed($sub)) {
85 7         33 my $target_class = "${class}::".$sub->name();
86 5     5   31 no strict 'refs';
  5         13  
  5         1091  
87 7 50       16 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         24 @{"${target_class}::ISA"} = (
  7         86  
91             'Data::Domain::Dependencies',
92             blessed($sub)
93             );
94             }
95 7     15   67 return bless sub { $sub->(@_) }, $target_class;
  15         77  
96             } else {
97 1     2   7 return bless sub { $sub->(@_) }, $class;
  2         7  
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 91 my $self = shift;
112 6         48 $self->_document();
113             }
114              
115             # this is where the magic happens ...
116             sub inspect {
117 12     12 1 66 my $sub = shift;
118 12         21 my $data = shift;
119 12 50       63 return __PACKAGE__." can only inspect hashrefs\n"
120             unless(ref($data) =~ /HASH/i);
121              
122 12 100       257 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 or using L
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;