Skip to content

Instantly share code, notes, and snippets.

Created October 31, 2011 20:53
Show Gist options
  • Save anonymous/1328895 to your computer and use it in GitHub Desktop.
Save anonymous/1328895 to your computer and use it in GitHub Desktop.
package SuiteSetup::Web::ActionRole::PrePostHooking;
use Moose::Role;
use Data::Dumper;
=head2 pre_hook|post_hook
Name of the method to be called for (pre|post)-hooks on actions
=cut
has [qw( pre_hook post_hook )] => (
is => 'rw',
lazy => 1,
default => sub {}
);
#pre hooks
before execute => sub {
my @params = @_;
push @params, 'pre';
_handle_hook(@params)
};
#post hooks
after execute => sub {
my @params = @_;
push @params, 'post';
_handle_hook(@params)
};
# generic hook handling sub
sub _handle_hook {
my ( $self, $controller, $c, $prefix) = @_;
my $action_name = (split(/\//, $c->action))[-1];
my $hook_name = "_" . $prefix . "_" . $action_name;
if ( $controller->can($hook_name) && ($action_name eq $self->name ) ) {
$c->log->debug( $self->name . " CAN $hook_name");
$controller->$hook_name($c);
} else {
$c->log->debug( $self->name . " CAN'T $hook_name");
}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment