package MooseX::Role::Parameterized;
use Moose (
extends => { -as => 'moose_extends' },
- qw/around confess/,
+ around => { -as => 'moose_around' },
+ 'confess',
);
use Carp 'croak';
__PACKAGE__->setup_import_methods(
with_caller => ['parameter', 'role', 'method'],
- as_is => ['has', 'with', 'extends', 'requires', 'excludes', 'augment', 'inner'],
+ as_is => ['has', 'with', 'extends', 'requires', 'excludes', 'augment', 'inner', 'before', 'after', 'around'],
);
sub parameter {
}
# give role a (&) prototype
-around _make_wrapper => sub {
+moose_around _make_wrapper => sub {
my $orig = shift;
my ($self, $caller, $sub, $fq_name) = @_;
$CURRENT_METACLASS->add_method($name => $method);
}
+sub before {
+ confess "before must be called within the role { ... } block."
+ unless $CURRENT_METACLASS;
+
+ my $code = pop @_;
+
+ for (@_) {
+ croak "Roles do not currently support "
+ . ref($_)
+ . " references for before method modifiers"
+ if ref $_;
+ $CURRENT_METACLASS->add_before_method_modifier($_, $code);
+ }
+}
+
+sub after {
+ confess "after must be called within the role { ... } block."
+ unless $CURRENT_METACLASS;
+
+ my $code = pop @_;
+
+ for (@_) {
+ croak "Roles do not currently support "
+ . ref($_)
+ . " references for after method modifiers"
+ if ref $_;
+ $CURRENT_METACLASS->add_after_method_modifier($_, $code);
+ }
+}
+
+sub around {
+ confess "around must be called within the role { ... } block."
+ unless $CURRENT_METACLASS;
+
+ my $code = pop @_;
+
+ for (@_) {
+ croak "Roles do not currently support "
+ . ref($_)
+ . " references for around method modifiers"
+ if ref $_;
+ $CURRENT_METACLASS->add_around_method_modifier($_, $code);
+ }
+}
+
sub with {
confess "with must be called within the role { ... } block."
unless $CURRENT_METACLASS;
--- /dev/null
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+my @calls;
+
+do {
+ package MyRole::LogMethod;
+ use MooseX::Role::Parameterized;
+
+ parameter method => (
+ is => 'ro',
+ isa => 'Str',
+ required => 1,
+ );
+
+ role {
+ my $p = shift;
+
+ before $p->method => sub {
+ push @calls, "calling " . $p->method
+ };
+
+ after $p->method => sub {
+ push @calls, "called " . $p->method
+ };
+
+ around $p->method => sub {
+ my $orig = shift;
+ my $start = 0; # time
+ $orig->(@_);
+ my $end = 0; # time
+
+ push @calls, "took " . ($end - $start) . " seconds";
+ };
+ };
+};
+
+do {
+ package MyClass;
+ use Moose;
+ with 'MyRole::LogMethod' => {
+ method => 'new',
+ };
+};
+
+is_deeply([splice @calls], [], "no calls yet");
+MyClass->new;
+is_deeply([splice @calls], ["calling new", "took 0 seconds", "called new"], "instrumented new");
+