-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ::ResultSet::Errors to help when calling Result methods on an RS
- Loading branch information
Arthur Axel 'fREW' Schmidt
committed
Feb 21, 2015
1 parent
9e22e03
commit e24db2f
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package DBIx::Class::Helper::ResultSet::Errors; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
my $std_err = qq{Can't locate object method "%s" via package "%s" } . | ||
qq{at %s line %d.\n}; | ||
|
||
my $cust_err = qq{You're trying to call a Result ("%s") method ("%s") } . | ||
qq{on a ResultSet ("%s") at %s line %d.\n}; | ||
|
||
sub AUTOLOAD { | ||
my $self = shift; | ||
|
||
my($class) = ref $self || $self; | ||
|
||
my($meth) = $DBIx::Class::Helper::ResultSet::Errors::AUTOLOAD | ||
=~ m/::([^:]+)$/; | ||
|
||
return if $meth eq 'DESTROY'; | ||
|
||
my($callpack, $callfile, $callline) = caller; | ||
|
||
my $rclass = $self->result_source->result_class; | ||
|
||
die sprintf $cust_err, $rclass, $meth, $class, $callfile, $callline | ||
if $rclass->can($meth); | ||
|
||
die sprintf $std_err, $meth, $class, $callfile, $callline; | ||
} | ||
|
||
1; | ||
|
||
__END__ | ||
=pod | ||
=head1 SYNOPSIS | ||
package MyApp::Schema::ResultSet::Foo; | ||
__PACKAGE__->load_components(qw{Helper::ResultSet::Errors}); | ||
... | ||
1; | ||
And then in a script or something: | ||
my $col = $rs->id | ||
# dies with a helpful error! | ||
=head1 DESCRIPTION | ||
Users new to C<DBIx::Class> often make the mistake of treating ResultSets like | ||
Results. This helper ameliorates the situation by giving a helpful error when | ||
the user calls methods for the result on the ResultSet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib 't/lib'; | ||
use Test::More; | ||
use Test::Fatal; | ||
|
||
use TestSchema; | ||
my $schema = TestSchema->deploy_or_connect(); | ||
|
||
like exception { | ||
$schema->resultset('Gnarly')->literature | ||
}, qr{^\QYou're trying to call a Result ("TestSchema::Result::Gnarly") method ("literature") on a ResultSet ("TestSchema::ResultSet::Gnarly") at t/ResultSet/Errors.t line 14.\E}, 'exceptional'; | ||
|
||
done_testing; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters