From: Justin Hunter Date: Mon, 20 Apr 2009 22:00:43 +0000 (+0000) Subject: rip load_optional_class from DBIx::Class::Componentised and put it in Class::C3:... X-Git-Tag: v1.001000~24 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=078742b1464002054e7a2bb48a9d262cdddb7b17;p=p5sagit%2FClass-C3-Componentised.git rip load_optional_class from DBIx::Class::Componentised and put it in Class::C3::Componentised --- diff --git a/Changes b/Changes index 29f8c5d..f3893b1 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Class-C3-Componentised +1.0005 19 Apr 2009 + Add load_optional_class method + 1.0004 18 Mar 2009 Port to use MRO::Compat (rafl) diff --git a/lib/Class/C3/Componentised.pm b/lib/Class/C3/Componentised.pm index 2865eaf..c216fe0 100644 --- a/lib/Class/C3/Componentised.pm +++ b/lib/Class/C3/Componentised.pm @@ -46,7 +46,7 @@ use MRO::Compat; use Class::Inspector; use Carp; -our $VERSION = 1.0004; +our $VERSION = 1.0005; =head2 load_components( @comps ) @@ -168,6 +168,25 @@ sub inject_base { mro::set_mro($target, 'c3'); } +=head2 load_optional_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 + +=cut + +sub load_optional_class { + my ($class, $f_class) = @_; + if ($class->ensure_class_found($f_class)) { + eval { $class->ensure_class_loaded($f_class) }; + croak "Failed to load $f_class: $@" if $@; + return 1; + } + return 0; +} + + =head1 AUTHOR Matt S. Trout and the DBIx::Class team diff --git a/t/01-basic.t b/t/01-basic.t index 8c3d5cb..fea4c38 100644 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -8,7 +8,7 @@ use Test::Exception; use lib "$FindBin::Bin/lib"; -plan tests => 3; +plan tests => 6; use_ok('MyModule'); @@ -23,3 +23,7 @@ throws_ok { MyModule->load_components('+ClassC3ComponentFooThatShouldntExist'); is(MyModule->new->message, "Foo MyModule", "it worked"); +is(MyModule->load_optional_class('ClassC3ComponentFooThatShouldntExist'), 0, "load_optional_class NonexistantClass returned false"); +is(MyModule->load_optional_class('MyModule::Plugin::Foo'), 1, "load_optional_class MyModule::Plugin::Foo (previously loaded module) returned true"); +is(MyModule->load_optional_class('MyModule::OwnComponent'), 1, "load_optional_class MyModule::OwnComponent (not previously loaded module) returned true"); +