From: Peter Rabbitson Date: Tue, 21 Jul 2009 23:01:35 +0000 (+0000) Subject: Do not call component_base_class unless it is actually necessary X-Git-Tag: v1.001000~21 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b0dd4f7ba77d3889d34af444ac19b8760a877305;p=p5sagit%2FClass-C3-Componentised.git Do not call component_base_class unless it is actually necessary --- diff --git a/Changes b/Changes index bbc7f97..6e5a12d 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ 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 diff --git a/lib/Class/C3/Componentised.pm b/lib/Class/C3/Componentised.pm index 280dc06..5d8c73b 100644 --- a/lib/Class/C3/Componentised.pm +++ b/lib/Class/C3/Componentised.pm @@ -60,8 +60,12 @@ Calling this will call C. 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); } @@ -95,9 +99,12 @@ found. 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; diff --git a/t/02-explicit.t b/t/02-explicit.t new file mode 100644 index 0000000..45aed0e --- /dev/null +++ b/t/02-explicit.t @@ -0,0 +1,35 @@ +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', +); diff --git a/t/lib/MyModule.pm b/t/lib/MyModule.pm index 3aa04a3..f7dae37 100644 --- a/t/lib/MyModule.pm +++ b/t/lib/MyModule.pm @@ -8,7 +8,7 @@ sub component_base_class { "MyModule::Plugin" } sub message { my $msg = $_[0]->maybe::next::method() || ''; - return $msg . " MyModule"; + return $msg . ' ' . __PACKAGE__; } sub new { diff --git a/t/lib/MyModuleNoBase.pm b/t/lib/MyModuleNoBase.pm new file mode 100644 index 0000000..969f5bc --- /dev/null +++ b/t/lib/MyModuleNoBase.pm @@ -0,0 +1,16 @@ +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;