File Coverage

blib/lib/DBIx/Class/Helper/ResultSet/Errors.pm
Criterion Covered Total %
statement 16 17 94.1
branch 2 4 50.0
condition 1 3 33.3
subroutine 4 4 100.0
pod n/a
total 23 28 82.1


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::ResultSet::Errors;
2             $DBIx::Class::Helper::ResultSet::Errors::VERSION = '2.034002';
3             # ABSTRACT: add exceptions to help when calling Result methods on an ResultSets
4              
5 55     55   21291 use strict;
  55         140  
  55         1351  
6 55     55   232 use warnings;
  55         97  
  55         1190  
7              
8 55     55   247 use parent 'DBIx::Class::ResultSet';
  55         85  
  55         235  
9              
10             my $std_err = qq{Can't locate object method "%s" via package "%s" } .
11             qq{at %s line %d.\n};
12              
13             my $cust_err = qq{You're trying to call a Result ("%s") method ("%s") } .
14             qq{on a ResultSet ("%s") at %s line %d.\n};
15              
16             sub AUTOLOAD {
17 1     1   608 my $self = shift;
18              
19 1   33     5 my($class) = ref $self || $self;
20              
21 1         9 my($meth) = $DBIx::Class::Helper::ResultSet::Errors::AUTOLOAD
22             =~ m/::([^:]+)$/;
23              
24 1 50       4 return if $meth eq 'DESTROY';
25              
26 1         3 my($callpack, $callfile, $callline) = caller;
27              
28 1         9 my $rclass = $self->result_source->result_class;
29              
30 1 50       266 die sprintf $cust_err, $rclass, $meth, $class, $callfile, $callline
31             if $rclass->can($meth);
32              
33 0           die sprintf $std_err, $meth, $class, $callfile, $callline;
34             }
35              
36             1;
37              
38             __END__
39              
40             =pod
41              
42             =head1 NAME
43              
44             DBIx::Class::Helper::ResultSet::Errors - add exceptions to help when calling Result methods on an ResultSets
45              
46             =head1 SYNOPSIS
47              
48             package MyApp::Schema::ResultSet::Foo;
49              
50             __PACKAGE__->load_components(qw{Helper::ResultSet::Errors});
51              
52             ...
53              
54             1;
55              
56             And then in a script or something:
57              
58             my $col = $rs->id
59              
60             # dies with a helpful error!
61              
62             =head1 DESCRIPTION
63              
64             Users new to C<DBIx::Class> often make the mistake of treating ResultSets like
65             Results. This helper ameliorates the situation by giving a helpful error when
66             the user calls methods for the result on the ResultSet. See
67             L<DBIx::Class::Helper::ResultSet/NOTE> for a nice way to apply it to your entire
68             schema.
69              
70             =head1 AUTHOR
71              
72             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
73              
74             =head1 COPYRIGHT AND LICENSE
75              
76             This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
77              
78             This is free software; you can redistribute it and/or modify it under
79             the same terms as the Perl 5 programming language system itself.
80              
81             =cut