From: Shawn M Moore Date: Wed, 30 Jul 2008 15:30:57 +0000 (+0000) Subject: Refactor regular class method modifier addition to go through the metaclass X-Git-Tag: 0.19~225 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=50dc6ee5748409751a8e0ef57a0e7c93e2c48cb4;p=gitmo%2FMouse.git Refactor regular class method modifier addition to go through the metaclass --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index 8d531aa..71640fc 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -9,7 +9,6 @@ use 5.006; use Sub::Exporter; use Carp 'confess'; use Scalar::Util 'blessed'; -use Class::Method::Modifiers (); use Mouse::Meta::Attribute; use Mouse::Meta::Class; @@ -61,15 +60,42 @@ do { }, before => sub { - return \&Class::Method::Modifiers::before; + my $caller = $CALLER; + + return sub { + my $code = pop; + my $class = $caller->meta; + + for (@_) { + $class->add_before_method_modifier($_ => $code); + } + }; }, after => sub { - return \&Class::Method::Modifiers::after; + my $caller = $CALLER; + + return sub { + my $code = pop; + my $class = $caller->meta; + + for (@_) { + $class->add_after_method_modifier($_ => $code); + } + }; }, around => sub { - return \&Class::Method::Modifiers::around; + my $caller = $CALLER; + + return sub { + my $code = pop; + my $class = $caller->meta; + + for (@_) { + $class->add_around_method_modifier($_ => $code); + } + }; }, with => sub { diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 9b32fc3..796872a 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -7,6 +7,7 @@ use Scalar::Util 'blessed'; use Carp 'confess'; use MRO::Compat; +use Class::Method::Modifiers (); do { my %METACLASS_CACHE; @@ -130,6 +131,36 @@ sub is_immutable { 0 } sub attribute_metaclass { "Mouse::Meta::Class" } +sub add_before_method_modifier { + my ($self, $name, $code) = @_; + Class::Method::Modifiers::_install_modifier( + $self->name, + 'before', + $name, + $code, + ); +} + +sub add_around_method_modifier { + my ($self, $name, $code) = @_; + Class::Method::Modifiers::_install_modifier( + $self->name, + 'around', + $name, + $code, + ); +} + +sub add_after_method_modifier { + my ($self, $name, $code) = @_; + Class::Method::Modifiers::_install_modifier( + $self->name, + 'after', + $name, + $code, + ); +} + 1; __END__