File: Verifier.pm

package info (click to toggle)
libdbix-class-helpers-perl 2.037000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,012 kB
  • sloc: perl: 5,056; sql: 547; makefile: 7
file content (129 lines) | stat: -rw-r--r-- 3,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package DBIx::Class::Helper::Schema::Verifier;
$DBIx::Class::Helper::Schema::Verifier::VERSION = '2.037000';
# ABSTRACT: Verify the Results and ResultSets of your Schemata

use strict;
use warnings;

use MRO::Compat;
use mro 'c3';

use Try::Tiny;
use namespace::clean;

use base 'DBIx::Class::Schema';

sub result_verifiers {
   return ()
}

our $_FATAL = 1;
our @_ERRORS;

sub register_source {
   my ($self, $name, $rclass) = @_;

   unless ($_FATAL) {
      $self->$_($rclass->result_class, $rclass->resultset_class)
         for $self->result_verifiers;
   } else {
      for ($self->result_verifiers) {
         try {
            $self->$_($rclass->result_class, $rclass->resultset_class)
         } catch {
            push @_ERRORS, $_
         }
      }
   }

   $self->next::method($name, $rclass);
}

sub load_namespaces {
   local $_FATAL = 1;

   shift->next::method(@_);

   my @e = @_ERRORS;
   @_ERRORS = ();
   die sort @e if @e;
}

sub load_classes {
   local $_FATAL = 1;

   shift->next::method(@_);

   my @e = @_ERRORS;
   @_ERRORS = ();
   die sort @e if @e;
}

1;

__END__

=pod

=head1 NAME

DBIx::Class::Helper::Schema::Verifier - Verify the Results and ResultSets of your Schemata

=head1 SYNOPSIS

 package MyApp::Schema;

 __PACKAGE__->load_components('Helper::Schema::Verifier');

 sub result_verifiers {
   (
      sub {
         my ($self, $result, $set) = @_;

         for ($result, $set) {
            die "$_ does not start with the letter A" unless m/^A/
         }
      },
      shift->next::method,
   )
 }

=head1 DESCRIPTION

C<DBIx::Class::Helper::Schema::Verifier> is a minuscule framework to assist in
creating schemata that are to your very own exacting specifications.  It is
inspired by my own travails in discovering that C<< use mro 'c3' >> is both
required and barely documented in much Perl code.  As time goes by I expect to
add many more verifiers, but with this inaugural release I am merely including
L<DBIx::Class::Helper::Schema::Verifier::C3>.

=head1 INTERFACE METHODS

=head2 result_verifiers

You must implement C<result_verifiers> in your subclass of C<::Verifier>.  Each
verifier gets called on the schema and gets each result and resultset together
as arguments.  You can use this to validate almost anything about the results
and resultsets of a schema; contributions are warmly welcomed.

=head1 MORE ERRORS

Initially I kept this module simple, but after using it in production at
L<ZipRecruiter|https://www.ziprecruiter.com> I found that showing the user the
first error that occurred and then giving up was pretty annoying.  Now
C<Schema::Verifier> wraps both L<DBIx::Class::Schema/load_namespaces> and
L<DBIx::Class::Schema/load_classes> and shows all the exceptions encoutered as a
list at the end of loading all the results.

=head1 AUTHOR

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2024 by Arthur Axel "fREW" Schmidt.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut