Make sure the component_class autoloader works across module reloads
Peter Rabbitson [Tue, 8 Mar 2011 12:58:38 +0000 (13:58 +0100)]
(works around corner cases from db29433c)

lib/DBIx/Class/AccessorGroup.pm
lib/DBIx/Class/Optional/Dependencies.pm
t/storage/cursor.t

index bd245e3..2b0462b 100644 (file)
@@ -4,15 +4,24 @@ use strict;
 use warnings;
 
 use base qw/Class::Accessor::Grouped/;
+use Scalar::Util qw/weaken/;
+use namespace::clean;
 
-our %successfully_loaded_components;
+my $successfully_loaded_components;
 
 sub get_component_class {
   my $class = $_[0]->get_inherited($_[1]);
-  if (defined $class and ! $successfully_loaded_components{$class}) {
+
+  if (defined $class and ! $successfully_loaded_components->{$class} ) {
     $_[0]->ensure_class_loaded($class);
-    $successfully_loaded_components{$class}++; # only increment if the load succeeded
+
+    no strict 'refs';
+    $successfully_loaded_components->{$class}
+      = ${"${class}::__LOADED__BY__DBIC__CAG__COMPONENT_CLASS__"}
+        = do { \(my $anon = 'loaded') };
+    weaken($successfully_loaded_components->{$class});
   }
+
   $class;
 };
 
index d946fc1..6f7c10d 100644 (file)
@@ -132,6 +132,12 @@ my $reqs = {
     req => $id_shortener,
   },
 
+  test_component_accessor => {
+    req => {
+      'Class::Unload'             => '0.07',
+    },
+  },
+
   test_pod => {
     req => {
       'Test::Pod'                 => '1.41',
index fb5f0cc..e6c0ba7 100644 (file)
@@ -13,4 +13,15 @@ lives_ok {
   is($schema->resultset("Artist")->search(), 3, "Three artists returned");
 } 'Custom cursor autoloaded';
 
+SKIP: {
+  eval { require Class::Unload }
+    or skip 'component_class reentrancy test requires Class::Unload', 1;
+
+  Class::Unload->unload('DBICTest::Cursor');
+
+  lives_ok {
+    is($schema->resultset("Artist")->search(), 3, "Three artists still returned");
+  } 'Custom cursor auto re-loaded';
+}
+
 done_testing;