From: Peter Rabbitson Date: Sat, 30 May 2009 05:40:24 +0000 (+0000) Subject: Patch + test for more informative exceptions on load_namespace a non-rs class X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b488020e139c8b235ea2fef878965bcf102d002e;p=dbsrgits%2FDBIx-Class-Historic.git Patch + test for more informative exceptions on load_namespace a non-rs class --- diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 63bff46..a3c5bc1 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -173,8 +173,8 @@ sub _findallmod { } # returns a hash of $shortname => $fullname for every package -# found in the given namespaces ($shortname is with the $fullname's -# namespace stripped off) +# found in the given namespaces ($shortname is with the $fullname's +# namespace stripped off) sub _map_namespaces { my ($class, @namespaces) = @_; @@ -190,6 +190,22 @@ sub _map_namespaces { @results_hash; } +# returns the result_source_instance for the passed class/object, +# or dies with an informative message (used by load_namespaces) +sub _ns_get_rsrc_instance { + my $class = shift; + my $rs = ref ($_[0]) || $_[0]; + + if ($rs->can ('result_source_instance') ) { + return $rs->result_source_instance; + } + else { + $class->throw_exception ( + "Attempt to load_namespaces() class $rs failed - are you sure this is a real Result Class?" + ); + } +} + sub load_namespaces { my ($class, %args) = @_; @@ -231,7 +247,7 @@ sub load_namespaces { my $result_class = $results{$result}; my $rs_class = delete $resultsets{$result}; - my $rs_set = $result_class->resultset_class; + my $rs_set = $class->_ns_get_rsrc_instance ($result_class)->resultset_class; if($rs_set && $rs_set ne 'DBIx::Class::ResultSet') { if($rs_class && $rs_class ne $rs_set) { @@ -241,10 +257,10 @@ sub load_namespaces { } elsif($rs_class ||= $default_resultset_class) { $class->ensure_class_loaded($rs_class); - $result_class->resultset_class($rs_class); + $class->_ns_get_rsrc_instance ($result_class)->resultset_class($rs_class); } - my $source_name = $result_class->source_name || $result; + my $source_name = $class->_ns_get_rsrc_instance ($result_class)->source_name || $result; push(@to_register, [ $source_name, $result_class ]); } diff --git a/t/39load_namespaces_exception.t b/t/39load_namespaces_exception.t new file mode 100644 index 0000000..0606972 --- /dev/null +++ b/t/39load_namespaces_exception.t @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +use lib qw(t/lib); +use DBICTest; # do not remove even though it is not used + +plan tests => 1; + +eval { + package DBICNSTest; + use base qw/DBIx::Class::Schema/; + __PACKAGE__->load_namespaces( + result_namespace => 'Bogus', + resultset_namespace => 'RSet', + ); +}; + +like ($@, qr/are you sure this is a real Result Class/, 'Clear exception thrown'); diff --git a/t/lib/DBICNSTest/Bogus/A.pm b/t/lib/DBICNSTest/Bogus/A.pm new file mode 100644 index 0000000..b525df5 --- /dev/null +++ b/t/lib/DBICNSTest/Bogus/A.pm @@ -0,0 +1,6 @@ +package DBICNSTest::Bogus::A; +use base qw/DBIx::Class/; +__PACKAGE__->load_components(qw/PK::Auto Core/); +__PACKAGE__->table('a'); +__PACKAGE__->add_columns('a'); +1; diff --git a/t/lib/DBICNSTest/Bogus/Bigos.pm b/t/lib/DBICNSTest/Bogus/Bigos.pm new file mode 100644 index 0000000..0f49683 --- /dev/null +++ b/t/lib/DBICNSTest/Bogus/Bigos.pm @@ -0,0 +1,3 @@ +package DBICNSTest::Bogus::Bigos; + +1;