| MooseX-Plaggerize documentation | Contained in the MooseX-Plaggerize distribution. |
MooseX::Plaggerize - plagger like plugin feature for Moose
# in main
my $c = Your::Context->new;
$c->load_plugin('HTMLFilter::StickyTime');
$c->load_plugin({module => 'HTMLFilter::DocRoot', config => { root => '/mobirc/' }});
$c->run();
package Your::Context;
use Moose;
with 'MooseX::Plaggerize';
sub run {
my $self = shift;
$self->run_hook('response_filter' => $args);
}
package Your::Plugin::HTMLFilter::DocRoot;
use strict;
use MooseX::Plaggerize::Plugin;
has root => (
is => 'ro',
isa => 'Str',
required => 1,
);
hook 'response_filter' => sub {
my ($self, $context, $args) = @_;
};
THIS MODULE IS IN ITS BETA QUALITY. API MAY CHANGE IN THE FUTURE.
MooseX::Plaggerize is Plagger like plugin system for Moose.
MooseX::Plaggerize is a Moose::Role.You can use this module with 'with'.
if you write:
my $app = MyApp->new;
$app->load_plugin({ module => 'Foo', config => {hoge => 'fuga'})
above code executes follow code:
my $app = MyApp->new;
my $plugin = MyApp::Plugin::Foo->new({hoge => 'fuga'});
$plugin->register( $app );
register code to hook point.$plugin is instance of plugin.
run hook.
use case: mostly ;-)
run hook.
if your hook code returns true value, stop the hook loop(this feature likes OK/DECLINED of mod_perl handler).
(please look source code :)
use case: handler like mod_perl
run hook.
(please look source code :)
use case: html filter
get the codes.
use case: write tricky code :-(
no plan :-)
Tokuhiro Matsuno <tokuhirom@gmail.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| MooseX-Plaggerize documentation | Contained in the MooseX-Plaggerize distribution. |
package MooseX::Plaggerize; use strict; use Moose::Role; use 5.00800; our $VERSION = '0.06'; use Scalar::Util qw/blessed/; use Carp; has __moosex_plaggerize_hooks => ( is => 'ro', isa => 'HashRef', default => sub { {} }, ); sub load_plugin { my ($self, $args) = @_; $args = {module => $args} unless ref $args; my $module = $args->{module}; $module = $self->resolve_plugin($module); Class::MOP::load_class($module); my $plugin = $module->new($args->{config} || {}); $plugin->register( $self ); } sub resolve_plugin { my ($self, $module) = @_; my $base = blessed $self or croak "this is instance method"; return ($module =~ /^\+(.*)$/) ? $1 : "${base}::Plugin::$module"; } sub register_hook { my ($self, @hooks) = @_; while (my ($hook, $plugin, $code) = splice @hooks, 0, 3) { $self->__moosex_plaggerize_hooks->{$hook} ||= []; push @{ $self->__moosex_plaggerize_hooks->{$hook} }, +{ plugin => $plugin, code => $code, }; } } sub run_hook { my ($self, $hook, @args) = @_; return unless my $hooks = $self->__moosex_plaggerize_hooks->{$hook}; my @ret; for my $hook (@$hooks) { my ($code, $plugin) = ($hook->{code}, $hook->{plugin}); my $ret = $code->( $plugin, $self, @args ); push @ret, $ret; } \@ret; } sub run_hook_first { my ( $self, $point, @args ) = @_; croak 'missing hook point' unless $point; for my $hook ( @{ $self->__moosex_plaggerize_hooks->{$point} } ) { if ( my $res = $hook->{code}->( $hook->{plugin}, $self, @args ) ) { return $res; } } return; } sub run_hook_filter { my ( $self, $point, @args ) = @_; for my $hook ( @{ $self->__moosex_plaggerize_hooks->{$point} } ) { @args = $hook->{code}->( $hook->{plugin}, $self, @args ); } return @args; } sub get_hook { my ($self, $hook) = @_; return $self->__moosex_plaggerize_hooks->{$hook}; } 1; __END__