File Coverage

lib/Data/Validation/Utils.pm
Criterion Covered Total %
statement 35 35 100.0
branch 3 4 100.0
condition n/a
subroutine 11 11 100.0
pod 3 3 100.0
total 52 53 100.0


line stmt bran cond sub pod time code
1             package Data::Validation::Utils;
2              
3 1     1   5 use strict;
  1         1  
  1         30  
4 1     1   4 use warnings;
  1         1  
  1         29  
5 1     1   4 use parent 'Exporter::Tiny';
  1         2  
  1         6  
6              
7 1     1   65 use Data::Validation::Constants qw( EXCEPTION_CLASS TRUE );
  1         1  
  1         6  
8 1     1   174 use Module::Runtime qw( require_module );
  1         1  
  1         10  
9 1     1   38 use Try::Tiny;
  1         1  
  1         67  
10 1     1   3 use Unexpected::Functions qw( is_class_loaded );
  1         1  
  1         11  
11              
12             our @EXPORT_OK = qw( ensure_class_loaded load_class throw );
13              
14             sub ensure_class_loaded ($) {
15 39     39 1 79 my $class = shift;
16              
17 39     39   341 try { require_module( $class ) } catch { throw( $_ ) };
  39         1622  
  1         704  
18              
19             # uncoverable branch true
20 38 50       6807 is_class_loaded( $class )
21             or throw( 'Class [_1] loaded but package undefined', [ $class ] );
22              
23 38         1451 return TRUE;
24             }
25              
26             sub load_class ($$$) {
27 34     34 1 104 my ($proto, $prefix, $class) = @_; $class =~ s{ \A $prefix }{}mx;
  34         281  
28              
29 34 100       134 if ('+' eq substr $class, 0, 1) { $class = substr $class, 1 }
  3         9  
30 31         103 else { $class = "${proto}::".(ucfirst $class) }
31              
32 34         77 ensure_class_loaded $class;
33              
34 33         1193 return $class;
35             }
36              
37             sub throw (;@) {
38 47     47 1 251 EXCEPTION_CLASS->throw( @_ );
39             }
40              
41             1;
42              
43             __END__
44              
45             =pod
46              
47             =encoding utf-8
48              
49             =head1 Name
50              
51             Data::Validation::Utils - Utility methods for constraints and filters
52              
53             =head1 Synopsis
54              
55             use Data::Validation::Utils qw( ensure_class_loaded );
56              
57             =head1 Description
58              
59             Defines some functions L<Data::Validation::Constraints> and
60             L<Data::Validation::Filters>
61              
62             =head1 Configuration and Environment
63              
64             Defines no attributes
65              
66             =head1 Subroutines/Methods
67              
68             =head2 ensure_class_loaded
69              
70             Throws if class cannot be loaded
71              
72             =head2 load_class
73              
74             Load the external plugin subclass at run time
75              
76             =head2 throw
77              
78             Throws an exception of class
79             L<EXCEPTION_CLASS|Data::Validation::Constants/EXCEPTION_CLASS>
80              
81             =head1 Diagnostics
82              
83             None
84              
85             =head1 Dependencies
86              
87             =over 3
88              
89             =item L<Module::Runtime>
90              
91             =item L<Try::Tiny>
92              
93             =item L<Unexpected>
94              
95             =back
96              
97             =head1 Incompatibilities
98              
99             There are no known incompatibilities in this module
100              
101             =head1 Bugs and Limitations
102              
103             There are no known bugs in this module. Please report problems to
104             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Validation. Patches are welcome
105              
106             =head1 Acknowledgements
107              
108             Larry Wall - For the Perl programming language
109              
110             =head1 Author
111              
112             Peter Flanigan, C<< <pjfl@cpan.org> >>
113              
114             =head1 License and Copyright
115              
116             Copyright (c) 2016 Peter Flanigan. All rights reserved
117              
118             This program is free software; you can redistribute it and/or modify it
119             under the same terms as Perl itself. See L<perlartistic>
120              
121             This program is distributed in the hope that it will be useful,
122             but WITHOUT WARRANTY; without even the implied warranty of
123             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
124              
125             =cut
126              
127             # Local Variables:
128             # mode: perl
129             # tab-width: 3
130             # End: