1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
# NAME
MooseX::Object::Pluggable - Make your classes pluggable
# VERSION
version 0.0013
# 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;
# DESCRIPTION
This module is meant to be loaded as a role from Moose-based classes.
It will add five methods and four 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.
# 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 `around`, `before`, `after`
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`.
Please note that when you load at runtime you lose the ability to wrap `BUILD`
and roles using `has` will not go through compile time checks like `required`
and `default`.
Even though `override` will work, I __STRONGLY__ discourage its use
and a warning will be thrown if you try to use it.
This is closely linked to the way multiple roles being applied is handled and is not
likely to change. `override` behavior 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.
# How plugins are loaded
When roles are applied at runtime an anonymous class will wrap your class and
`$self->blessed`, `ref $self` and `$self->meta->name`
will no longer return the name of your object;
they will instead return the name of the anonymous class created at runtime.
See `_original_class_name`.
# Usage
For a simple example see the tests included in this distribution.
# Attributes
## \_plugin\_ns
String. The prefix to use for plugin names provided. `MyApp::Plugin` is sensible.
## \_plugin\_app\_ns
An ArrayRef accessor that automatically dereferences into array on a read call.
By default it will be filled with the class name and its precedents. It is used
to determine which directories to look for plugins as well as which plugins
take precedence upon namespace collisions. This allows you to subclass a pluggable
class and still use its plugins while using yours first if they are available.
## \_plugin\_locator
An automatically built instance of [Module::Pluggable::Object](https://metacpan.org/pod/Module::Pluggable::Object) used to locate
available plugins.
## \_original\_class\_name
Because of the way roles apply, `$self->blessed`, `ref $self`
and `$self->meta->name` 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.
# Public Methods
## load\_plugins @plugins
## load\_plugin $plugin
Load the appropriate role for `$plugin`.
# Private Methods
There's nothing stopping you from using these, but if you are using them
for anything that's not really complicated you are probably doing
something wrong.
## \_role\_from\_plugin $plugin
Creates a role name from a plugin name. If the plugin name is prepended
with a `+` it will be treated as a full name returned as is. Otherwise
a string consisting of `$plugin` prepended with the `_plugin_ns`
and the first valid value from `_plugin_app_ns` will be returned. Example
#assuming appname MyApp and C<_plugin_ns> 'Plugin'
$self->_role_from_plugin("MyPlugin"); # MyApp::Plugin::MyPlugin
## \_load\_and\_apply\_role @roles
Require `$role` if it is not already loaded and apply it. This is
the meat of this module.
## \_build\_plugin\_app\_ns
Automatically builds the \_plugin\_app\_ns attribute with the classes in the
class precedence list that are not part of Moose.
## \_build\_plugin\_locator
Automatically creates a [Module::Pluggable::Object](https://metacpan.org/pod/Module::Pluggable::Object) instance with the correct
search\_path.
## meta
Keep tests happy. See [Moose](https://metacpan.org/pod/Moose)
# SEE ALSO
[Moose](https://metacpan.org/pod/Moose), [Moose::Role](https://metacpan.org/pod/Moose::Role), [Class::Inspector](https://metacpan.org/pod/Class::Inspector)
# BUGS
Holler?
Please report any bugs or feature requests to
`bug-MooseX-Object-Pluggable at rt.cpan.org`, or through the web interface at
[http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Object-Pluggable](http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Object-Pluggable).
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
# SUPPORT
You can find documentation for this module with the perldoc command.
perldoc MooseX-Object-Pluggable
You can also look for information at:
- AnnoCPAN: Annotated CPAN documentation
[http://annocpan.org/dist/MooseX-Object-Pluggable](http://annocpan.org/dist/MooseX-Object-Pluggable)
- CPAN Ratings
[http://cpanratings.perl.org/d/MooseX-Object-Pluggable](http://cpanratings.perl.org/d/MooseX-Object-Pluggable)
- RT: CPAN's request tracker
[http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Object-Pluggable](http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Object-Pluggable)
- Search CPAN
[http://search.cpan.org/dist/MooseX-Object-Pluggable](http://search.cpan.org/dist/MooseX-Object-Pluggable)
# ACKNOWLEDGEMENTS
- #Moose - Huge number of questions
- Matt S Trout <mst@shadowcatsystems.co.uk> - ideas / planning.
- Stevan Little - EVERYTHING. Without him this would have never happened.
- Shawn M Moore - bugfixes
# AUTHOR
Guillermo Roditi <groditi@cpan.org>
# COPYRIGHT AND LICENSE
This software is copyright (c) 2007 by Guillermo Roditi <groditi@cpan.org>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
# CONTRIBUTORS
- David Steinbrunner <dsteinbrunner@pobox.com>
- Karen Etheridge <ether@cpan.org>
- Robert Boone <robo4288@gmail.com>
- Shawn M Moore <sartak@gmail.com>
- Todd Hepler <thepler@employees.org>
- Yuval Kogman <nothingmuch@woobling.org>
|