From: Guillermo Roditi Date: Fri, 19 Jan 2007 20:17:48 +0000 (+0000) Subject: branching out and maybe relasing X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fusing-build;p=gitmo%2FMooseX-Object-Pluggable.git branching out and maybe relasing --- diff --git a/TODO b/TODO new file mode 100644 index 0000000..ae246c3 --- /dev/null +++ b/TODO @@ -0,0 +1,4 @@ + +---- This works great unless the consuming class lacks + a build. Then it just kinda pukes all over itself + because I am using around. What to do? \ No newline at end of file diff --git a/lib/MooseX/Object/Pluggable.pm b/lib/MooseX/Object/Pluggable.pm index 6df5a5a..2b7cd41 100644 --- a/lib/MooseX/Object/Pluggable.pm +++ b/lib/MooseX/Object/Pluggable.pm @@ -104,13 +104,13 @@ This means that is _plugin_ns is "MyApp::Plugin" and _plugin_ext_ns is HashRef. Keeps an inventory of what plugins are loaded and what the actual module name is to avoid multiple loading. -=head2 __plugin_subclass +=head2 _plugin_subclass Object. This holds the subclass of our pluggable object in the form of an anonymous L instance. All roles are actually applied to this instance instead of the original class instance in order to not lose the original object name as roles are applied. The anonymous class will be -automatically generated upon first use. +automatically generated. =cut @@ -126,7 +126,7 @@ has _plugin_ext_ns => (is => 'rw', required => 1, isa => 'Str', has _plugin_loaded => (is => 'rw', required => 1, isa => 'HashRef', default => sub{ {} }); -has __plugin_subclass => ( is => 'rw', required => 0, isa => 'Object', ); +has _plugin_subclass => ( is => 'rw', required => 0, isa => 'Object', ); #--------#---------#---------#---------#---------#---------#---------#---------# @@ -191,35 +191,29 @@ sub load_plugin_ext{ There's nothing stopping you from using these, but if you are using them you are probably doing something wrong. -=head2 _plugin_subclass +=head2 BUILD -Creates, if needed and returns the anonymous instance of the consuming objects -subclass to which roles will be applied to. +Extend BUILD to create a suitable C. =cut -sub _plugin_subclass{ - my $self = shift; - my $anon_class = $self->__plugin_subclass; - - #initialize if we havnt been initialized already. - unless(ref $anon_class && $self->meta->is_anon_class){ - - #create an anon class that inherits from $self that plugins can be - #applied to safely and store it within the $self instance. - $anon_class = Moose::Meta::Class-> - create_anon_class(superclasses => [$self->meta->name]); - $self->__plugin_subclass( $anon_class ); - - #rebless $self as the anon class which now inherits from ourselves - #this allows the anon class to override methods in the consuming - #class while keeping a stable name and set of superclasses - bless $self => $anon_class->name - unless $self->meta->name eq $anon_class->name; - } +around BUILD => sub { + my ($super,$self) = @_; - return $anon_class; -} + #create an anon class that inherits from $self that plugins can be + #applied to safely and store it within the $self instance. + my $anon_class = Moose::Meta::Class-> + create_anon_class(superclasses => [$self->meta->name]); + $self->_plugin_subclass( $anon_class ); + + #rebless $self as the anon class which now inherits from ourselves + #this allows the anon class to override methods in the consuming + #class while keeping a stable name and set of superclasses + bless $self => $anon_class->name + unless $self->meta->name eq $anon_class->name; + + $super->($self); +}; =head2 _role_from_plugin $plugin @@ -242,6 +236,13 @@ sub _role_from_plugin{ $plugin =~ /^\+(.*)/ ? $1 : join '::', $name, $self->_plugin_ns, $plugin; } +#sub original_class_name{ +# my $self = shift; +# +# $self->meta->is_anon_class ? +# ($self->meta->superclasses)[0] : $self->blessed; +#} + =head2 _load_and_apply_role $role Require C<$role> if it is not already loaded and apply it to diff --git a/t/01-basic.t b/t/01-basic.t index 77aa5f1..8bd713e 100644 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -30,3 +30,6 @@ is( $app->baz, "foo'd baz plugin baz", 'foo extension override for baz'); ok($app->load_plugin('Bor'), "Loaded Bor"); is( $app->foo, "bor'd foo around foo", 'bor extension override for foo'); is( $app->bor, "plugin bor", 'override bor via plugin'); + + +print $app->bar_value("XXX");; diff --git a/t/lib/TestApp.pm b/t/lib/TestApp.pm index d108a13..943fab4 100644 --- a/t/lib/TestApp.pm +++ b/t/lib/TestApp.pm @@ -8,6 +8,8 @@ with 'MooseX::Object::Pluggable'; has bee => (is => 'rw', isa => 'Int', required => 1, default => '100'); +sub BUILD{ 'foome' } + sub foo{ 'original foo' } sub bar{ 'original bar' } diff --git a/t/lib/TestApp/Plugin/Bar.pm b/t/lib/TestApp/Plugin/Bar.pm index 6520ff6..f63473d 100644 --- a/t/lib/TestApp/Plugin/Bar.pm +++ b/t/lib/TestApp/Plugin/Bar.pm @@ -4,6 +4,10 @@ use strict; use warnings; use Moose::Role; +has 'bar_value' => (is => 'rw', required => 1, isa => 'Str', + default => 'Fucker' + ); + around bar => sub{ 'override bar' }; 1;