From: Guillermo Roditi Date: Tue, 16 Jan 2007 20:20:11 +0000 (+0000) Subject: first checkin of MooseX-Object-Pluggable X-Git-Tag: 0.0002^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=421e9f8d52cf41796d09266414e4164a488dfb2d;p=gitmo%2FMooseX-Object-Pluggable.git first checkin of MooseX-Object-Pluggable --- 421e9f8d52cf41796d09266414e4164a488dfb2d diff --git a/Changes b/Changes new file mode 100644 index 0000000..2ae672f --- /dev/null +++ b/Changes @@ -0,0 +1,6 @@ +Revision history for MooseX-Object-Pluggable +0.0002 Jan 16, 2007 + Forgot Class::Inspector dep on Makefile +0.0001 Jan 16, 2007 + Initial Release! + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..7538120 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,31 @@ +Changes +inc/Module/AutoInstall.pm +inc/Module/Install.pm +inc/Module/Install/AutoInstall.pm +inc/Module/Install/Base.pm +inc/Module/Install/Can.pm +inc/Module/Install/Fetch.pm +inc/Module/Install/Include.pm +inc/Module/Install/Makefile.pm +inc/Module/Install/Metadata.pm +inc/Module/Install/Win32.pm +inc/Module/Install/WriteAll.pm +lib/MooseX/Object/Pluggable.pm +Makefile.PL +MANIFEST This list of files +META.yml +README +t/00-load.t +t/01-basic.t +t/boilerplate.t +t/lib/TestApp.pm +t/lib/TestApp/Plugin/Bar.pm +t/lib/TestApp/Plugin/Baz.pm +t/lib/TestApp/Plugin/Baz/ExtensionFor/Bar.pm +t/lib/TestApp/Plugin/Bor.pm +t/lib/TestApp/Plugin/Bor/ExtensionFor/Foo.pm +t/lib/TestApp/Plugin/Foo.pm +t/lib/TestApp/Plugin/Foo/ExtensionFor/Bar.pm +t/lib/TestApp/Plugin/Foo/ExtensionFor/Baz.pm +t/pod-coverage.t +t/pod.t diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..2ead1f5 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,20 @@ +#! /usr/bin/perl -w + +# Load the Module::Install bundled in ./inc/ +use inc::Module::Install; + +# Define metadata +name 'MooseX-Object-Pluggable'; +abstract 'Add plugin support to your Moose classes via roles.'; +all_from 'lib/MooseX/Object/Pluggable.pm'; + +# Specific dependencies +requires 'Moose' => 0.17; +requires 'Class::Inspector' => 1.04; + +build_requires 'Test::More' => 0; + +#no_index directory => 'dist'; + +auto_install; +WriteAll; diff --git a/README b/README new file mode 100644 index 0000000..01d1704 --- /dev/null +++ b/README @@ -0,0 +1,38 @@ +MooseX-Object-Pluggable + +INSTALLATION + +To install this module, run the following commands: + + perl Makefile.PL + make + make test + make install + + +SUPPORT AND DOCUMENTATION + +After installing, you can find documentation for this module with the perldoc command. + + perldoc MooseX::Object::Pluggable + +You can also look for information at: + + Search CPAN + http://search.cpan.org/dist/MooseX-Object-Pluggable + + CPAN Request Tracker: + http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Object-Pluggable + + AnnoCPAN, annotated CPAN documentation: + http://annocpan.org/dist/MooseX-Object-Pluggable + + CPAN Ratings: + http://cpanratings.perl.org/d/MooseX-Object-Pluggable + +COPYRIGHT AND LICENCE + +Copyright (C) 2006 Guillermo Roditi + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. diff --git a/lib/MooseX/Object/Pluggable.pm b/lib/MooseX/Object/Pluggable.pm new file mode 100644 index 0000000..6df5a5a --- /dev/null +++ b/lib/MooseX/Object/Pluggable.pm @@ -0,0 +1,345 @@ +package MooseX::Object::Pluggable; + +use Carp; +use strict; +use warnings; +use Moose::Role; +use Class::Inspector; + + +our $VERSION = '0.0002'; + +=head1 NAME + + MooseX::Object::Pluggable - Make your classes pluggable + +=head1 SYNOPSIS + + package MyApp; + use Moose; + + with 'MooseX::Object::Pluggable'; + + ... + + package MyApp::Plugin::Pretty; + use Moose::Role; + + sub pretty{ print "I am pretty" } + + 1; + + # + use MyApp; + my $app = MyApp->new; + $app->load_plugin('Pretty'); + $app->pretty; + +=head1 DESCRIPTION + +This module is meant to be loaded as a role from Moose-based classes +it will add five methods and five attributes to assist you with the loading +and handling of plugins and extensions for plugins. I understand that this may +pollute your namespace, however I took great care in using the least ambiguous +names possible. + +=head1 How plugins Work + +Plugins and extensions are just Roles by a fancy name. They are loaded at runtime +on demand and are instance, not class based. This means that if you have more than +one instance of a class they can all have different plugins loaded. This is a feature. + +Plugin methods are allowed to C, C, C +their consuming classes, so it is important to watch for load order as plugins can +and will overload each other. You may also add attributes through has. + +Even thouch C will work in basic cases, I STRONGLY discourage it's use +and a warning will be thrown if you try to use it. +This is closely linked to the way multiple roles being applies is handles and is not +likely to change. C bevavior is closely linked to inheritance and thus will +likely not work as you expect it in multiple inheritance situations. Point being, +save yourself the headache. + +=head1 How plugins are loaded + +You don't really need to understand anything except for the first paragraph. + +The first time you load a plugin a new anonymous L will be +created. This class will inherit from your pluggable object and then your object +will be reblessed to an instance of this anonymous class. This means that +C<$self-Eblessed> and C will no longer return the name of your object, +they will instead return the name of the anonymous class created at runtime. Your +original class name can be located at C<($self-Emeta-Esuperclasses)[0]> + +Once the anonymous subclass exists all plugin roles will be Ced to this class +directly. This "subclass" though is in fact now C<$self> and it C. + If this is confusing.. it should be, thats why you let me handle it. Just know that it +has to be done this way in order for plugins to override core functionality. + +=head1 + +For a simple example see the tests for this distribution. + +=head1 Attributes + +=head2 _plugin_ns + +String. The prefix to use for plugin names provided. MyApp::Plugin is sensible. + +=head2 _plugin_ext + +Boolean. Indicates whether we should attempt to load plugin extensions. +Defaults to true; + +=head2 _plugin_ext_ns + +String. The namespace plugin extensions have. Defaults to 'ExtensionFor'. + +This means that is _plugin_ns is "MyApp::Plugin" and _plugin_ext_ns is +"ExtensionFor" loading plugin "Bar" would search for extensions in +"MyApp::Plugin::Bar::ExtensionFor::*". + +=head2 _plugin_loaded + +HashRef. Keeps an inventory of what plugins are loaded and what the actual +module name is to avoid multiple loading. + +=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. + +=cut + +#--------#---------#---------#---------#---------#---------#---------#---------# + +has _plugin_ns => (is => 'rw', required => 1, isa => 'Str', + default => 'Plugin'); + +has _plugin_ext => (is => 'rw', required => 1, isa => 'Bool', + default => 1); +has _plugin_ext_ns => (is => 'rw', required => 1, isa => 'Str', + default => 'ExtensionFor'); +has _plugin_loaded => (is => 'rw', required => 1, isa => 'HashRef', + default => sub{ {} }); + +has __plugin_subclass => ( is => 'rw', required => 0, isa => 'Object', ); + +#--------#---------#---------#---------#---------#---------#---------#---------# + +=head1 Public Methods + +=head2 load_plugin $plugin + +This is the only method you should be using. +Load the apropriate role for C<$plugin> as well as any +extensions it provides if extensions are enabled. + +=cut + +sub load_plugin{ + my ($self, $plugin) = @_; + die("You must provide a plugin name") unless $plugin; + + my $loaded = $self->_plugin_loaded; + return 1 if exists $loaded->{$plugin}; + + my $role = $self->_role_from_plugin($plugin); + + $loaded->{$plugin} = $role if $self->_load_and_apply_role($role); + $self->load_plugin_ext($plugin) if $self->_plugin_ext; + + return exists $loaded->{$plugin}; +} + + +=head2 _load_plugin_ext + +Will load any extensions for a particular plugin. This should be called +automatically by C so you don't need to worry about it. +It basically attempts to load any extension that exists for a plugin +that is already loaded. The only reason for using this is if you want to +keep _plugin_ext as false and only load extensions manually, which I don't +recommend. + +=cut + +sub load_plugin_ext{ + my ($self, $plugin) = @_; + die("You must provide a plugin name") unless $plugin; + my $role = $self->_role_from_plugin($plugin); + + # $p for plugin, $r for role + while( my($p,$r) = each %{ $self->_plugin_loaded }){ + my $ext = join "::", $role, $self->_plugin_ext_ns, $p; + + $self->_load_and_apply_role( $ext ) + if Class::Inspector->installed($ext); + + #go back to prev loaded modules and load extensions for current module? + #my $ext2 = join "::", $r, $self->_plugin_ext_ns, $plugin; + #$self->_load_and_apply_role( $ext2 ) + # if Class::Inspector->installed($ext2); + } +} + +=head1 Private Methods + +There's nothing stopping you from using these, but if you are using them +you are probably doing something wrong. + +=head2 _plugin_subclass + +Creates, if needed and returns the anonymous instance of the consuming objects +subclass to which roles will be applied to. + +=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; + } + + return $anon_class; +} + +=head2 _role_from_plugin $plugin + +Creates a role name from a plugin name. If the plugin name is prepended +with a C<+> it will be treated as a full name returned as is. Otherwise +a string consisting of C<$plugin> prepended with the application name +and C<_plugin_ns> will be returned. Example + + #assuming appname MyApp and C<_plugin_ns> 'Plugin' + $self->_role_from_plugin("MyPlugin"); # MyApp::Plugin::MyPlugin + +=cut + +sub _role_from_plugin{ + my ($self, $plugin) = @_; + + my $name = $self->meta->is_anon_class ? + ($self->meta->superclasses)[0] : $self->blessed; + + $plugin =~ /^\+(.*)/ ? $1 : join '::', $name, $self->_plugin_ns, $plugin; +} + +=head2 _load_and_apply_role $role + +Require C<$role> if it is not already loaded and apply it to +C<_plugin_subclass>. This is the meat of this module. + +=cut + +sub _load_and_apply_role{ + my ($self, $role) = @_; + die("You must provide a role name") unless $role; + + #Throw exception if plugin is not installed + die("$role is not available on this system") + unless Class::Inspector->installed($role); + + #don't re-require... + unless( Class::Inspector->loaded($role) ){ + eval "require $role" || die("Failed to load role: $role"); + } + + carp("Using 'override' is strongly discouraged and may not behave ". + "as you expect it to. Please use 'around'") + if scalar keys %{ $role->meta->get_override_method_modifiers_map }; + + #apply the plugin to the anon subclass + die("Failed to apply plugin: $role") + unless $role->meta->apply( $self->_plugin_subclass ); + + return 1; +} + + +1; + +__END__; + +=head1 SEE ALSO + +L, L + +=head1 AUTHOR + +Guillermo Roditi, + +=head1 BUGS + +Holler? + +Please report any bugs or feature requests to +C, or through the web interface at +L. +I will be notified, and then you'll automatically be notified of progress on +your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc MooseX-Object-Pluggable + +You can also look for information at: + +=over 4 + +=item * AnnoCPAN: Annotated CPAN documentation + +L + +=item * CPAN Ratings + +L + +=item * RT: CPAN's request tracker + +L + +=item * Search CPAN + +L + +=back + +=head1 ACKNOWLEDGEMENTS + +=over 4 + +=item #Moose - Huge number of questions + +=item Matt S Trout - ideas / planning. + +=item Stevan Little - EVERYTHING. Without him this would have never happened. + +=back + +=head1 COPYRIGHT + +Copyright 2007 Guillermo Roditi. All Rights Reserved. This is +free software; you may redistribute it and/or modify it under the same +terms as Perl itself. + +=cut diff --git a/t/00-load.t b/t/00-load.t new file mode 100644 index 0000000..ed305e9 --- /dev/null +++ b/t/00-load.t @@ -0,0 +1,9 @@ +#!perl + +use Test::More tests => 1; + +BEGIN { + use_ok( 'MooseX::Object::Pluggable' ); +} + +diag( "Testing MooseX::Object::Pluggable $MooseX::Object::Pluggable::VERSION, Perl $], $^X" ); diff --git a/t/01-basic.t b/t/01-basic.t new file mode 100644 index 0000000..77aa5f1 --- /dev/null +++ b/t/01-basic.t @@ -0,0 +1,32 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use Test::More; +use lib 't/lib'; + +plan tests => 16; + +use_ok('TestApp'); + +my $app = TestApp->new; + +is( $app->foo, "original foo", 'original foo value'); +is( $app->bar, "original bar", 'original bar value'); +is( $app->bor, "original bor", 'original bor value'); + +ok($app->load_plugin('Bar'), "Loaded Bar"); +is( $app->bar, "override bar", 'overridden bar via plugin'); + +ok($app->load_plugin('Baz'), "Loaded Baz"); +is( $app->baz, "plugin baz", 'added baz via plugin'); +is( $app->bar, "baz'd bar override bar", 'baz extension for bar using around'); + +ok($app->load_plugin('Foo'), "Loaded Foo"); +is( $app->foo, "around foo", 'around foo via plugin'); +is( $app->bar, "foo'd bar baz'd bar override bar", 'foo extension around baz extension for bar'); +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'); diff --git a/t/boilerplate.t b/t/boilerplate.t new file mode 100644 index 0000000..dccf5b9 --- /dev/null +++ b/t/boilerplate.t @@ -0,0 +1,48 @@ +#!perl + +use strict; +use warnings; +use Test::More tests => 3; + +sub not_in_file_ok { + my ($filename, %regex) = @_; + open my $fh, "<", $filename + or die "couldn't open $filename for reading: $!"; + + my %violated; + + while (my $line = <$fh>) { + while (my ($desc, $regex) = each %regex) { + if ($line =~ $regex) { + push @{$violated{$desc}||=[]}, $.; + } + } + } + + if (%violated) { + fail("$filename contains boilerplate text"); + diag "$_ appears on lines @{$violated{$_}}" for keys %violated; + } else { + pass("$filename contains no boilerplate text"); + } +} + +not_in_file_ok(README => + "The README is used..." => qr/The README is used/, + "'version information here'" => qr/to provide version information/, +); + +not_in_file_ok(Changes => + "placeholder date/time" => qr(Date/time) +); + +sub module_boilerplate_ok { + my ($module) = @_; + not_in_file_ok($module => + 'the great new $MODULENAME' => qr/ - The great new /, + 'boilerplate description' => qr/Quick summary of what the module/, + 'stub function definition' => qr/function[12]/, + ); +} + +module_boilerplate_ok('lib/MooseX/Object/Pluggable.pm'); diff --git a/t/lib/TestApp.pm b/t/lib/TestApp.pm new file mode 100644 index 0000000..d108a13 --- /dev/null +++ b/t/lib/TestApp.pm @@ -0,0 +1,17 @@ +package TestApp; + +use strict; +use warnings; +use Moose; + +with 'MooseX::Object::Pluggable'; + +has bee => (is => 'rw', isa => 'Int', required => 1, default => '100'); + +sub foo{ 'original foo' } + +sub bar{ 'original bar' } + +sub bor{ 'original bor' } + +1; diff --git a/t/lib/TestApp/Plugin/Bar.pm b/t/lib/TestApp/Plugin/Bar.pm new file mode 100644 index 0000000..6520ff6 --- /dev/null +++ b/t/lib/TestApp/Plugin/Bar.pm @@ -0,0 +1,9 @@ +package TestApp::Plugin::Bar; + +use strict; +use warnings; +use Moose::Role; + +around bar => sub{ 'override bar' }; + +1; diff --git a/t/lib/TestApp/Plugin/Baz.pm b/t/lib/TestApp/Plugin/Baz.pm new file mode 100644 index 0000000..f0f8dea --- /dev/null +++ b/t/lib/TestApp/Plugin/Baz.pm @@ -0,0 +1,9 @@ +package TestApp::Plugin::Baz; + +use strict; +use warnings; +use Moose::Role; + +sub baz { 'plugin baz' } + +1; diff --git a/t/lib/TestApp/Plugin/Baz/ExtensionFor/Bar.pm b/t/lib/TestApp/Plugin/Baz/ExtensionFor/Bar.pm new file mode 100644 index 0000000..eb236ff --- /dev/null +++ b/t/lib/TestApp/Plugin/Baz/ExtensionFor/Bar.pm @@ -0,0 +1,12 @@ +package TestApp::Plugin::Baz::ExtensionFor::Bar; + +use strict; +use warnings; +use Moose::Role; + +around bar => sub{ + my ($super, $self) = @_; + "baz'd bar " . $super->($self); +}; + +1; diff --git a/t/lib/TestApp/Plugin/Bor.pm b/t/lib/TestApp/Plugin/Bor.pm new file mode 100644 index 0000000..905a940 --- /dev/null +++ b/t/lib/TestApp/Plugin/Bor.pm @@ -0,0 +1,9 @@ +package TestApp::Plugin::Bor; + +use strict; +use warnings; +use Moose::Role; + +around bor => sub{ 'plugin bor' }; + +1; diff --git a/t/lib/TestApp/Plugin/Bor/ExtensionFor/Foo.pm b/t/lib/TestApp/Plugin/Bor/ExtensionFor/Foo.pm new file mode 100644 index 0000000..15f3379 --- /dev/null +++ b/t/lib/TestApp/Plugin/Bor/ExtensionFor/Foo.pm @@ -0,0 +1,13 @@ +package TestApp::Plugin::Bor::ExtensionFor::Foo; + +use strict; +use warnings; +use Moose::Role; + +around foo => sub{ + my $super = shift; + my $self = shift; + "bor'd foo " . $super->($self); +}; + +1; diff --git a/t/lib/TestApp/Plugin/Foo.pm b/t/lib/TestApp/Plugin/Foo.pm new file mode 100644 index 0000000..c3ab157 --- /dev/null +++ b/t/lib/TestApp/Plugin/Foo.pm @@ -0,0 +1,9 @@ +package TestApp::Plugin::Foo; + +use strict; +use warnings; +use Moose::Role; + +around foo => sub{ 'around foo' }; + +1; diff --git a/t/lib/TestApp/Plugin/Foo/ExtensionFor/Bar.pm b/t/lib/TestApp/Plugin/Foo/ExtensionFor/Bar.pm new file mode 100644 index 0000000..c521441 --- /dev/null +++ b/t/lib/TestApp/Plugin/Foo/ExtensionFor/Bar.pm @@ -0,0 +1,12 @@ +package TestApp::Plugin::Foo::ExtensionFor::Bar; + +use strict; +use warnings; +use Moose::Role; + +around bar => sub { + my ($super, $self) = @_; + "foo'd bar " . $super->($self); +}; + +1; diff --git a/t/lib/TestApp/Plugin/Foo/ExtensionFor/Baz.pm b/t/lib/TestApp/Plugin/Foo/ExtensionFor/Baz.pm new file mode 100644 index 0000000..f83b96e --- /dev/null +++ b/t/lib/TestApp/Plugin/Foo/ExtensionFor/Baz.pm @@ -0,0 +1,13 @@ +package TestApp::Plugin::Foo::ExtensionFor::Baz; + +use strict; +use warnings; +use Moose::Role; + +around baz => sub{ + my $super = shift; + my $self = shift; + "foo'd baz " . $super->($self); +}; + +1; diff --git a/t/pod-coverage.t b/t/pod-coverage.t new file mode 100644 index 0000000..c3fb12a --- /dev/null +++ b/t/pod-coverage.t @@ -0,0 +1,6 @@ +#!perl + +use Test::More; +eval "use Test::Pod::Coverage"; +plan skip_all => "Test::Pod::Coverage required for testing POD coverage" if $@; +all_pod_coverage_ok(); diff --git a/t/pod.t b/t/pod.t new file mode 100644 index 0000000..4909b92 --- /dev/null +++ b/t/pod.t @@ -0,0 +1,6 @@ +#!perl + +use Test::More; +eval "use Test::Pod 1.14"; +plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; +all_pod_files_ok();