$class->inject_base($class => @comp);
}
+# Given a class name, tests to see if it is already loaded or otherwise
+# defined. If it is not yet loaded, the package is require'd, and an exception
+# is thrown if the class is still not loaded.
+#
# TODO: handle ->has_many('rel', 'Class'...) instead of
# ->has_many('rel', 'Some::Schema::Class'...)
sub ensure_class_loaded {
eval "require $f_class";
my $err = $@;
Class::Inspector->loaded($f_class)
- or die $err || "require $f_class was successful but the package".
- "is not defined";
+ or $class->throw_exception($err || "`require $f_class' was successful".
+ "but the package is not defined");
+}
+
+# Returns true if the specified class is installed or already loaded, false
+# otherwise
+sub ensure_class_found {
+ my ($class, $f_class) = @_;
+ return Class::Inspector->loaded($f_class) ||
+ Class::Inspector->installed($f_class);
+}
+
+# Returns a true value if the specified class is installed and loaded
+# successfully, throws an exception if the class is found but not loaded
+# successfully, and false if the class is not installed
+sub load_optional_class {
+ my ($class, $f_class) = @_;
+ if ($class->ensure_class_found($f_class)) {
+ $class->ensure_class_loaded($f_class);
+ return 1;
+ } else {
+ return 0;
+ }
}
1;
my $schema = DBICTest->init_schema();
-plan tests => 6;
-
-ok(Class::Inspector->loaded('TestPackage::A'),
- 'anon. package exists');
-eval {
- $schema->ensure_class_loaded('TestPackage::A');
-};
-
-ok(!$@, 'ensure_class_loaded detected an anon. class');
-
-eval {
- $schema->ensure_class_loaded('FakePackage::B');
-};
-
-like($@, qr/Can't locate/,
- 'ensure_class_loaded threw exception for nonexistent class');
-
-ok(!Class::Inspector->loaded('DBICTest::FakeComponent'),
- 'DBICTest::FakeComponent not loaded yet');
-
-eval {
- $schema->ensure_class_loaded('DBICTest::FakeComponent');
-};
-
-ok(!$@, 'ensure_class_loaded detected an existing but non-loaded class');
-ok(Class::Inspector->loaded('DBICTest::FakeComponent'),
- 'DBICTest::FakeComponent now loaded');
+plan tests => 17;
+
+# Test ensure_class_found
+ok( $schema->ensure_class_found('DBIx::Class::Schema'),
+ 'loaded package DBIx::Class::Schema was found' );
+ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
+ 'DBICTest::FakeComponent not loaded yet' );
+ok( $schema->ensure_class_found('DBICTest::FakeComponent'),
+ 'package DBICTest::FakeComponent was found' );
+ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
+ 'DBICTest::FakeComponent not loaded by ensure_class_found()' );
+ok( $schema->ensure_class_found('TestPackage::A'),
+ 'anonymous package TestPackage::A found' );
+ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'),
+ 'fake package not found' );
+
+# Test load_optional_class
+my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') };
+ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
+ok( !$retval, 'nonexistent package not loaded' );
+$retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
+ok( !$@, 'load_optional_class on an existing class did not throw' );
+ok( $retval, 'DBICTest::OptionalComponent loaded' );
+eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
+like( $@, qr/did not return a true value/, 'DBICTest::ErrorComponent threw ok' );
+
+# Test ensure_class_loaded
+ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' );
+eval { $schema->ensure_class_loaded('TestPackage::A'); };
+ok( !$@, 'ensure_class_loaded detected an anon. class' );
+eval { $schema->ensure_class_loaded('FakePackage::B'); };
+like( $@, qr/Can't locate/,
+ 'ensure_class_loaded threw exception for nonexistent class' );
+ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
+ 'DBICTest::FakeComponent not loaded yet' );
+eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
+ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
+ok( Class::Inspector->loaded('DBICTest::FakeComponent'),
+ 'DBICTest::FakeComponent now loaded' );
1;