update Class::Inspector prereq
[p5sagit/Class-C3-Componentised.git] / lib / Class / C3 / Componentised.pm
index 8d3058a..2007b27 100644 (file)
@@ -2,11 +2,7 @@ package Class::C3::Componentised;
 
 =head1 NAME
 
-Class::C3::Componentised
-
-=head1 DESCRIPTION
-
-Load mix-ins or components to your C3-based class.
+Class::C3::Componentised - Load mix-ins or components to your C3-based class
 
 =head1 SYNOPSIS
 
@@ -21,7 +17,7 @@ Load mix-ins or components to your C3-based class.
 
   package main;
 
-  MyModule->load_components( qw/Foo Bar/ ); 
+  MyModule->load_components( qw/Foo Bar/ );
   # Will load MyModule::Component::Foo and MyModule::Component::Bar
 
 =head1 DESCRIPTION
@@ -29,7 +25,7 @@ Load mix-ins or components to your C3-based class.
 This will inject base classes to your module using the L<Class::C3> method
 resolution order.
 
-Please note: these are not plugins that can take precedence over methods 
+Please note: these are not plugins that can take precedence over methods
 declared in MyModule. If you want something like that, consider
 L<MooseX::Object::Pluggable>.
 
@@ -47,15 +43,17 @@ use warnings;
 # Therefore leaving it in indefinitely.
 use MRO::Compat;
 
-use Carp;
+use Carp ();
+use List::Util ();
 
-our $VERSION = 1.0008;
+our $VERSION = '1.001000';
+$VERSION =~ tr/_//d;
 
 my $invalid_class = qr/(?: \b:\b | \:{3,} | \:\:$ )/x;
 
 =head2 load_components( @comps )
 
-Loads the given components into the current module. If a module begins with a 
+Loads the given components into the current module. If a module begins with a
 C<+> character, it is taken to be a fully qualified class name, otherwise
 C<< $class->component_base_class >> is prepended to it.
 
@@ -95,7 +93,7 @@ sub _load_components {
 
 =head2 load_optional_components
 
-As L<load_components>, but will silently ignore any components that cannot be 
+As L<load_components>, but will silently ignore any components that cannot be
 found.
 
 =cut
@@ -142,16 +140,16 @@ sub ensure_class_loaded {
     return if ( *{"${f_class}::$_"}{CODE} );
   }
 
-
   # require always returns true on success
-  eval { require($file) } or do {
+  # ill-behaved modules might very well obliterate $_
+  eval { local $_; require($file) } or do {
 
     $@ = "Invalid class name '$f_class'" if $f_class =~ $invalid_class;
 
     if ($class->can('throw_exception')) {
       $class->throw_exception($@);
     } else {
-      croak $@;
+      Carp::croak $@;
     }
   };
 
@@ -163,11 +161,6 @@ sub ensure_class_loaded {
 Returns true if the specified class is installed or already loaded, false
 otherwise.
 
-Note that the underlying mechanism (Class::Inspector->installed()) used by this
-sub will not, at the time of writing, correctly function when @INC includes
-coderefs. Since PAR relies upon coderefs in @INC, this function should be
-avoided in modules that are likely to be included within a PAR.
-
 =cut
 
 sub ensure_class_found {
@@ -188,13 +181,24 @@ sub inject_base {
   my $class = shift;
   my $target = shift;
 
-  for (reverse @_) {
-    no strict 'refs';
-    unshift ( @{"${target}::ISA"}, $_ )
-      unless ($target eq $_ || $target->isa($_));
-  }
-
   mro::set_mro($target, 'c3');
+
+  for my $comp (reverse @_) {
+    my $apply = do {
+      no strict 'refs';
+      sub { unshift ( @{"${target}::ISA"}, $comp ) };
+    };
+    unless ($target eq $comp || $target->isa($comp)) {
+      our %APPLICATOR_FOR;
+      if (my $apply_class
+            = List::Util::first { $APPLICATOR_FOR{$_} } @{mro::get_linear_isa($comp)}
+      ) {
+        $APPLICATOR_FOR{$apply_class}->_apply_component_to_class($comp,$target,$apply);
+      } else {
+        $apply->();
+      }
+    }
+  }
 }
 
 =head2 load_optional_class
@@ -232,11 +236,19 @@ sub load_optional_class {
   }
 }
 
-=head1 AUTHOR
+=head1 AUTHORS
+
+Matt S. Trout and the L<DBIx::Class team|DBIx::Class/CONTRIBUTORS>
+
+Pulled out into separate module by Ash Berlin C<< <ash@cpan.org> >>
+
+Optimizations and overall bolt-tightening by Peter "ribasushi" Rabbitson
+C<< <ribasushi@cpan.org> >>
 
-Matt S. Trout and the DBIx::Class team
+=head1 COPYRIGHT
 
-Pulled out into seperate module by Ash Berlin C<< <ash@cpan.org> >>
+Copyright (c) 2006 - 2011 the Class::C3::Componentised L</AUTHORS> as listed
+above.
 
 =head1 LICENSE