added code and tests for Componentized::ensure_class_found and load_optional_class
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Componentised.pm
index 412958f..109ad36 100644 (file)
@@ -58,6 +58,10 @@ sub _load_components {
   $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 {
@@ -65,8 +69,29 @@ 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;