File Coverage

blib/lib/Data/Rx/Tools/ShareDirValidator.pm
Criterion Covered Total %
statement 39 39 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 15 15 100.0
pod 4 4 100.0
total 67 67 100.0


line stmt bran cond sub pod time code
1 3     3   385723 use strict;
  3         9  
  3         87  
2 3     3   17 use warnings;
  3         6  
  3         159  
3              
4             package Data::Rx::Tools::ShareDirValidator;
5             BEGIN {
6 3     3   93 $Data::Rx::Tools::ShareDirValidator::AUTHORITY = 'cpan:KENTNL';
7             }
8             {
9             $Data::Rx::Tools::ShareDirValidator::VERSION = '0.1.3';
10             }
11              
12             # ABSTRACT: A Simple base class for generating simple validators based on Data::Rx
13              
14              
15              
16 3     3   2628 use Data::Rx;
  3         79943  
  3         88  
17 3     3   29 use File::ShareDir qw();
  3         4  
  3         46  
18 3     3   15 use Path::Tiny qw();
  3         6  
  3         48  
19 3     3   14 use Scalar::Util qw( blessed );
  3         5  
  3         1478  
20              
21              
22 2     2 1 6 sub filename { return 'schema' }
23              
24              
25 2     2 1 15 sub suffix { return '.json' }
26              
27             my $cache;
28              
29              
30             sub check {
31 8     8 1 3858 my ( $self, $data ) = @_;
32 8 100       29 if ( not exists $cache->{ _CLASS($self) } ) {
33 2         7 $cache->{ _CLASS($self) } = _CLASS($self)->_make_rx;
34             }
35 6         40 return $cache->{ _CLASS($self) }->check($data);
36             }
37              
38              
39             sub decode_file {
40 2     2 1 149 my ( $self, $file ) = @_;
41 2         16 require JSON;
42 2         63 return JSON->new()->utf8(1)->relaxed(1)->decode( scalar $file->slurp() );
43             }
44              
45             sub _make_rx {
46 2     2   5 my ($self) = @_;
47 2         18 return Data::Rx->new()->make_schema( _CLASS($self)->decode_file( _CLASS($self)->_specfile ) );
48             }
49              
50             sub _sharedir {
51 2     2   5 my ($self) = @_;
52 2         7 return Path::Tiny::path( File::ShareDir::module_dir( _CLASS($self) ) );
53             }
54              
55             sub _specfile {
56 2     2   4 my ($self) = @_;
57 2         8 return _CLASS($self)->_sharedir->child( _CLASS($self)->filename . _CLASS($self)->suffix );
58             }
59              
60             sub _CLASS {
61 30     30   60049 my ($classname) = @_;
62 30 100 100     224 return blessed $classname if ( ref $classname && blessed $classname );
63 20 100       155 return $classname if not ref $classname;
64 2         13 require Carp;
65             ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
66 2         301 Carp::croak( q{Argument 0 was an unblessed ref instead of the expected classname,}
67             . q{ ensure you are calling the method right with $classname->check( $data ) or similar} );
68             }
69              
70             1;
71              
72             __END__