-
-
Save dhoss/1328897 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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