Skip to content

Commit

Permalink
Add new ::Schema::Verifier framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed May 2, 2015
1 parent cf9f359 commit 1adc477
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for {{$dist->name}}

{{$NEXT}}
- Add new ::Schema::Verifier framework

2.025003 2015-04-06 16:28:20-05:00 America/Chicago
- Make ::OnColumnChange always allow mutating values before update
Expand Down
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ requires 'parent' => 0;
requires 'String::CamelCase' => 0;
requires 'namespace::clean' => 0.23;
requires 'List::Util' => 0;
requires 'DBIx::Class::Candy' => 0.001003;
requires 'DBIx::Class::Candy' => 0.003001;
requires 'DBIx::Introspector' => 0.001002;
requires 'Module::Runtime';
requires 'Try::Tiny';
Expand Down
63 changes: 63 additions & 0 deletions lib/DBIx/Class/Helper/Schema/Verifier.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package DBIx::Class::Helper::Schema::Verifier;

# ABSTRACT: Verify the Results and ResultSets of your Schemata

use strict;
use warnings;

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

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

sub result_verifiers {
return ()
}

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

$self->$_($rclass->result_class, $rclass->resultset_class)
for $self->result_verifiers;

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

1;

=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 miniscule 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.
12 changes: 12 additions & 0 deletions t/Schema/Verifier.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use strict;
use warnings;

use lib 't/lib';
use Test::More;
use Test::Fatal;

like(exception {
require VerifySchema;
}, qr/^Derp/, 'Schema verify checks all input');

done_testing;
5 changes: 5 additions & 0 deletions t/lib/Herp.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Herp;

sub noise {}

1;
25 changes: 25 additions & 0 deletions t/lib/VerifySchema.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package VerifySchema;

use strict;
use warnings;

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

use parent 'DBIx::Class::Schema';

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

die 'Derp' if $set->isa('Herp');
}, shift->next::method)
}

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

__PACKAGE__->load_namespaces;

'zomg';
14 changes: 14 additions & 0 deletions t/lib/VerifySchema/Result/A.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package VerifySchema::Result::A;

use DBIx::Class::Candy -base => 'DBIx::Class::Core';

table 'A';

column id => {
data_type => 'integer',
size => 12,
};

primary_key 'id';

1;
7 changes: 7 additions & 0 deletions t/lib/VerifySchema/ResultSet/A.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package VerifySchema::ResultSet::A;

use DBIx::Class::Candy::ResultSet;

use base 'Herp';

1;

0 comments on commit 1adc477

Please sign in to comment.