From: Shawn M Moore Date: Wed, 30 Jul 2008 15:30:31 +0000 (+0000) Subject: Add support for adding method modifiers to a role metaclass X-Git-Tag: 0.19~227 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fc0e0bbda277a99354170a4b8de6eb1275920555;p=gitmo%2FMouse.git Add support for adding method modifiers to a role metaclass --- diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index 0c04b74..79f3b7f 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -58,6 +58,13 @@ sub apply { for my $modifier_type (qw/before after around/) { no strict 'refs'; + *{ __PACKAGE__ . '::' . "add_${modifier_type}_method_modifier" } = sub { + my ($self, $method_name, $method) = @_; + + push @{ $self->{"${modifier_type}_method_modifiers"}->{$method_name} }, + $method; + }; + *{ __PACKAGE__ . '::' . "get_${modifier_type}_method_modifiers" } = sub { my ($self, $method_name, $method) = @_; @{ $self->{"${modifier_type}_method_modifiers"}->{$method_name} || [] } diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index 4a9a08b..e538247 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -23,13 +23,31 @@ do { } }, before => sub { - return sub { } + my $caller = $CALLER; + return sub { + my $code = pop; + for (@_) { + $caller->meta->add_before_method_modifier($_ => $code); + } + } }, after => sub { - return sub { } + my $caller = $CALLER; + return sub { + my $code = pop; + for (@_) { + $caller->meta->add_before_method_modifier($_ => $code); + } + } }, around => sub { - return sub { } + my $caller = $CALLER; + return sub { + my $code = pop; + for (@_) { + $caller->meta->add_before_method_modifier($_ => $code); + } + } }, has => sub { my $caller = $CALLER;