Revision history for Class-C3-Componentised
+ Allow derived classes without component_base_class(), given all
+ arguments to load_components are absolute class names (+Classname)
+
1.0005 22 Apr 2009
Add load_optional_class method
sub load_components {
my $class = shift;
- my $base = $class->component_base_class;
- my @comp = map { /^\+(.*)$/ ? $1 : "${base}::$_" } grep { $_ !~ /^#/ } @_;
+ my @comp = map {
+ /^\+(.*)$/
+ ? $1
+ : join ('::', $class->component_base_class, $_)
+ }
+ grep { $_ !~ /^#/ } @_;
$class->_load_components(@comp);
}
sub load_optional_components {
my $class = shift;
- my $base = $class->component_base_class;
my @comp = grep { $class->load_optional_class( $_ ) }
- map { /^\+(.*)$/ ? $1 : "${base}::$_" }
+ map {
+ /^\+(.*)$/
+ ? $1
+ : join ('::', $class->component_base_class, $_)
+ }
grep { $_ !~ /^#/ } @_;
$class->_load_components( @comp ) if scalar @comp;
--- /dev/null
+use strict;
+use warnings;
+
+use FindBin;
+use Test::More;
+use Test::Exception;
+
+use lib "$FindBin::Bin/lib";
+
+plan tests => 7;
+
+use_ok('MyModuleNoBase');
+is(MyModuleNoBase->new->message, " MyModuleNoBase", "initial message matches");
+lives_ok (
+ sub { MyModuleNoBase->load_components('+MyModule::Plugin::Foo') },
+ 'explicit load_components does not throw',
+);
+is(MyModuleNoBase->new->message, "Foo MyModuleNoBase", "component works");
+
+throws_ok (
+ sub { MyModuleNoBase->load_components('ClassC3ComponentFooThatShouldntExist') },
+ qr/Can't locate object method "component_base_class"/,
+ 'non-explicit component specification fails without component_base_class()',
+);
+
+throws_ok (
+ sub { MyModuleNoBase->load_optional_components('ClassC3ComponentFooThatShouldntExist') },
+ qr/Can't locate object method "component_base_class"/,
+ 'non-explicit component specification fails without component_base_class()',
+);
+
+lives_ok (
+ sub { MyModuleNoBase->load_optional_components('+ClassC3ComponentFooThatShouldntExist') },
+ 'explicit optional component specification does not throw',
+);
sub message {
my $msg = $_[0]->maybe::next::method() || '';
- return $msg . " MyModule";
+ return $msg . ' ' . __PACKAGE__;
}
sub new {
--- /dev/null
+package # hide from pause
+ MyModuleNoBase;
+
+use base 'Class::C3::Componentised';
+
+sub message {
+ my $msg = $_[0]->maybe::next::method() || '';
+
+ return $msg . ' ' . __PACKAGE__;
+}
+
+sub new {
+ return bless {}, shift;
+}
+
+1;