Dynamically load necessary table classes
Peter Rabbitson [Wed, 5 Aug 2009 13:41:28 +0000 (13:41 +0000)]
lib/DBIx/Class/Manual/Cookbook.pod
lib/DBIx/Class/ResultSource.pm
lib/DBIx/Class/ResultSource/View.pm
lib/DBIx/Class/ResultSourceProxy/Table.pm
t/lib/DBICTest/Schema/Year1999CDs.pm
t/lib/DBICTest/Schema/Year2000CDs.pm

index d1e559c..8728eb5 100644 (file)
@@ -111,8 +111,6 @@ L<ResultSource::View|DBIx::Class::ResultSource::View> for your query.
   use warnings;
   use base qw/DBIx::Class/;
 
-  use DBIx::Class::ResultSource::View;
-
   __PACKAGE__->load_components('Core');
   __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
 
index c5f157f..1b93f4e 100644 (file)
@@ -40,8 +40,6 @@ DBIx::Class::ResultSource - Result source object
   # Create a query (view) based result source, in a result class
   package MyDB::Schema::Result::Year2000CDs;
 
-  use DBIx::Class::ResultSource::View;
-
   __PACKAGE__->load_components('Core');
   __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
 
index ea37971..d992c71 100644 (file)
@@ -20,7 +20,6 @@ DBIx::Class::ResultSource::View - ResultSource object representing a view
   package MyDB::Schema::Result::Year2000CDs;
 
   use base qw/DBIx::Class/;
-  use DBIx::Class::ResultSource::View;
 
   __PACKAGE__->load_components('Core');
   __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
index a983ce1..929c300 100644 (file)
@@ -5,8 +5,6 @@ use warnings;
 
 use base qw/DBIx::Class::ResultSourceProxy/;
 
-use DBIx::Class::ResultSource::Table;
-
 __PACKAGE__->mk_classdata(table_class => 'DBIx::Class::ResultSource::Table');
 
 __PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do
@@ -22,8 +20,11 @@ sub _init_result_source_instance {
     my $class_has_table_instance = ($table and $table->result_class eq $class);
     return $table if $class_has_table_instance;
 
+    my $table_class = $class->table_class;
+    $class->ensure_class_loaded($table_class);
+
     if( $table ) {
-        $table = $class->table_class->new({
+        $table = $table_class->new({
             %$table,
             result_class => $class,
             source_name => undef,
@@ -31,7 +32,7 @@ sub _init_result_source_instance {
         });
     }
     else {
-        $table = $class->table_class->new({
+        $table = $table_class->new({
             name            => undef,
             result_class    => $class,
             source_name     => undef,
@@ -76,7 +77,11 @@ sub table {
   my ($class, $table) = @_;
   return $class->result_source_instance->name unless $table;
   unless (ref $table) {
-    $table = $class->table_class->new({
+
+    my $table_class = $class->table_class;
+    $class->ensure_class_loaded($table_class);
+
+    $table = $table_class->new({
         $class->can('result_source_instance') ?
           %{$class->result_source_instance||{}} : (),
         name => $table,
index 4aea122..ee1c9ec 100644 (file)
@@ -3,7 +3,6 @@ package # hide from PAUSE
 ## Used in 104view.t
 
 use base qw/DBICTest::BaseResult/;
-use DBIx::Class::ResultSource::View;
 
 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
 
index ebc4395..7f75f5f 100644 (file)
@@ -3,7 +3,6 @@ package # hide from PAUSE
 ## Used in 104view.t
 
 use base qw/DBICTest::BaseResult/;
-use DBIx::Class::ResultSource::View;
 
 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');