From: Guillermo Roditi Date: Mon, 27 Apr 2009 19:05:16 +0000 (-0400) Subject: update _original_class_name X-Git-Tag: v0.0010^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=acf6ced22d63f04899f245b126720354c5394c83;p=gitmo%2FMooseX-Object-Pluggable.git update _original_class_name --- diff --git a/Changes b/Changes index a4fb554..62a03cd 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,7 @@ Revision history for MooseX-Object-Pluggable +0.0010 Apr 27, 2009 + Change how _original_class_name works so it sucks less. + 0.0009 Nov 25, 2008 Avoid throwing an error when load_plugins is called on already-loaded plugins diff --git a/lib/MooseX/Object/Pluggable.pm b/lib/MooseX/Object/Pluggable.pm index 51b5fdd..857239a 100644 --- a/lib/MooseX/Object/Pluggable.pm +++ b/lib/MooseX/Object/Pluggable.pm @@ -3,9 +3,10 @@ package MooseX::Object::Pluggable; use Carp; use Moose::Role; use Class::MOP; +use Scalar::Util 'blessed'; use Module::Pluggable::Object; -our $VERSION = '0.0009'; +our $VERSION = '0.00010'; =head1 NAME @@ -87,54 +88,62 @@ to determine which directories to look for plugins as well as which plugins take presedence upon namespace collitions. This allows you to subclass a pluggable class and still use it's plugins while using yours first if they are available. -=cut - =head2 _plugin_locator An automatically built instance of L used to locate available plugins. +=head2 _original_class_name + +Because of the way roles apply C<$self-Eblessed> and C will +no longer return what you expect. Instead, upon instantiation, the name of the +class instantiated will be stored in this attribute if you need to access the +name the class held before any runtime roles were applied. + =cut #--------#---------#---------#---------#---------#---------#---------#---------# -has _plugin_ns => - ( - is => 'rw', - required => 1, - isa => 'Str', - default => sub{ 'Plugin' }, - ); - -has _plugin_loaded => - ( - is => 'rw', - required => 1, - isa => 'HashRef', - default => sub{ {} } - ); - -has _plugin_app_ns => - ( - is => 'rw', - required => 1, - isa => 'ArrayRef', - lazy => 1, - auto_deref => 1, - builder => '_build_plugin_app_ns', - trigger => sub{ $_[0]->_clear_plugin_locator if $_[0]->_has_plugin_locator; }, - ); - -has _plugin_locator => - ( - is => 'rw', - required => 1, - lazy => 1, - isa => 'Module::Pluggable::Object', - clearer => '_clear_plugin_locator', - predicate => '_has_plugin_locator', - builder => '_build_plugin_locator' - ); +has _plugin_ns => ( + is => 'rw', + required => 1, + isa => 'Str', + default => sub{ 'Plugin' }, +); + +has _original_class_name => ( + is => 'ro', + required => 1, + isa => 'Str', + default => sub{ blessed($_[0]) }, +); + +has _plugin_loaded => ( + is => 'rw', + required => 1, + isa => 'HashRef', + default => sub{ {} } +); + +has _plugin_app_ns => ( + is => 'rw', + required => 1, + isa => 'ArrayRef', + lazy => 1, + auto_deref => 1, + builder => '_build_plugin_app_ns', + trigger => sub{ $_[0]->_clear_plugin_locator if $_[0]->_has_plugin_locator; }, +); + +has _plugin_locator => ( + is => 'rw', + required => 1, + lazy => 1, + isa => 'Module::Pluggable::Object', + clearer => '_clear_plugin_locator', + predicate => '_has_plugin_locator', + builder => '_build_plugin_locator' +); #--------#---------#---------#---------#---------#---------#---------#---------# @@ -172,20 +181,6 @@ sub load_plugin { $self->load_plugins(@_); } -=head2 _original_class_name - -Because of the way roles apply C<$self-Eblessed> and C will -no longer return what you expect. Instead use this class to get your original -class name. - -=cut - -sub _original_class_name{ - my $self = shift; - return (grep {$_ !~ /^Moose::/} $self->meta->class_precedence_list)[0]; -} - - =head1 Private Methods There's nothing stopping you from using these, but if you are using them diff --git a/t/01-basic.t b/t/01-basic.t index 7025db0..9bdc083 100644 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -5,7 +5,7 @@ use warnings; use Test::More; use lib 't/lib'; -plan tests => 15; +plan tests => 16; use_ok('TestApp'); @@ -17,8 +17,6 @@ is($app->_role_from_plugin('+'.$_), $_) is($app->_role_from_plugin($_), 'TestApp::Plugin::'.$_) for(qw/Foo/); - - is( $app->foo, "original foo", 'original foo value'); is( $app->bar, "original bar", 'original bar value'); is( $app->bor, "original bor", 'original bor value'); @@ -26,6 +24,8 @@ is( $app->bor, "original bor", 'original bor value'); ok($app->load_plugin('Bar'), "Loaded Bar"); is( $app->bar, "override bar", 'overridden bar via plugin'); +is( $app->_original_class_name, 'TestApp', '_original_class_name works'); + ok($app->load_plugin('Baz'), "Loaded Baz"); is( $app->baz, "plugin baz", 'added baz via plugin'); @@ -36,3 +36,5 @@ ok($app->load_plugin('+TestApp::Plugin::Bor'), "Loaded Bor"); is( $app->bor, "plugin bor", 'override bor via plugin'); #print $app->meta->dump(3); + +