File Coverage

blib/lib/DBIx/Class/Helper/Schema/Verifier.pm
Criterion Covered Total %
statement 39 40 97.5
branch 3 6 50.0
condition n/a
subroutine 13 13 100.0
pod 4 4 100.0
total 59 63 93.6


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::Schema::Verifier;
2             $DBIx::Class::Helper::Schema::Verifier::VERSION = '2.034002';
3             # ABSTRACT: Verify the Results and ResultSets of your Schemata
4              
5 5     5   49468 use strict;
  5         11  
  5         118  
6 5     5   21 use warnings;
  5         9  
  5         96  
7              
8 5     5   19 use MRO::Compat;
  5         8  
  5         94  
9 5     5   28 use mro 'c3';
  5         9  
  5         31  
10              
11 5     5   114 use Try::Tiny;
  5         8  
  5         246  
12 5     5   1515 use namespace::clean;
  5         42879  
  5         24  
13              
14 5     5   956 use base 'DBIx::Class::Schema';
  5         9  
  5         2344  
15              
16             sub result_verifiers {
17             return ()
18 16     16 1 189 }
19              
20             our $_FATAL = 1;
21             our @_ERRORS;
22              
23             sub register_source {
24 4     4 1 19319 my ($self, $name, $rclass) = @_;
25              
26 4 50       13 unless ($_FATAL) {
27             $self->$_($rclass->result_class, $rclass->resultset_class)
28 0         0 for $self->result_verifiers;
29             } else {
30 4         14 for ($self->result_verifiers) {
31             try {
32 12     12   592 $self->$_($rclass->result_class, $rclass->resultset_class)
33             } catch {
34 4     4   342 push @_ERRORS, $_
35             }
36 12         409 }
37             }
38              
39 4         71 $self->next::method($name, $rclass);
40             }
41              
42             sub load_namespaces {
43 1     1 1 9 local $_FATAL = 1;
44              
45 1         5 shift->next::method(@_);
46              
47 1         248 my @e = @_ERRORS;
48 1         3 @_ERRORS = ();
49 1 50       23 die sort @e if @e;
50             }
51              
52             sub load_classes {
53 1     1 1 171 local $_FATAL = 1;
54              
55 1         4 shift->next::method(@_);
56              
57 1         264 my @e = @_ERRORS;
58 1         3 @_ERRORS = ();
59 1 50       4 die sort @e if @e;
60             }
61              
62             1;
63              
64             __END__
65              
66             =pod
67              
68             =head1 NAME
69              
70             DBIx::Class::Helper::Schema::Verifier - Verify the Results and ResultSets of your Schemata
71              
72             =head1 SYNOPSIS
73              
74             package MyApp::Schema;
75              
76             __PACKAGE__->load_components('Helper::Schema::Verifier');
77              
78             sub result_verifiers {
79             (
80             sub {
81             my ($self, $result, $set) = @_;
82              
83             for ($result, $set) {
84             die "$_ does not start with the letter A" unless m/^A/
85             }
86             },
87             shift->next::method,
88             )
89             }
90              
91             =head1 DESCRIPTION
92              
93             C<DBIx::Class::Helper::Schema::Verifier> is a minuscule framework to assist in
94             creating schemata that are to your very own exacting specifications. It is
95             inspired by my own travails in discovering that C<< use mro 'c3' >> is both
96             required and barely documented in much Perl code. As time goes by I expect to
97             add many more verifiers, but with this inaugural release I am merely including
98             L<DBIx::Class::Helper::Schema::Verifier::C3>.
99              
100             =head1 INTERFACE METHODS
101              
102             =head2 result_verifiers
103              
104             You must implement C<result_verifiers> in your subclass of C<::Verifier>. Each
105             verifier gets called on the schema and gets each result and resultset together
106             as arguments. You can use this to validate almost anything about the results
107             and resultsets of a schema; contributions are warmly welcomed.
108              
109             =head1 MORE ERRORS
110              
111             Initially I kept this module simple, but after using it in production at
112             L<ZipRecruiter|https://www.ziprecruiter.com> I found that showing the user the
113             first error that occurred and then giving up was pretty annoying. Now
114             C<Schema::Verifier> wraps both L<DBIx::Class::Schema/load_namespaces> and
115             L<DBIx::Class::Schema/load_classes> and shows all the exceptions encoutered as a
116             list at the end of loading all the results.
117              
118             =head1 AUTHOR
119              
120             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
121              
122             =head1 COPYRIGHT AND LICENSE
123              
124             This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128              
129             =cut