From: Shawn M Moore Date: Sun, 10 May 2009 16:41:00 +0000 (-0400) Subject: Factor out Role's before/after/around into a helper function X-Git-Tag: 0.78~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2e3c7aa0534b1f6b916253028d2b498e0db789e8;p=gitmo%2FMoose.git Factor out Role's before/after/around into a helper function I would use Moose::Util::add_method_modifier but that supports regular expressions for the method name, which roles do not. --- diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index 0941da6..0100667 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -46,43 +46,26 @@ sub has { $meta->add_attribute( $_, %options ) for @$attrs; } -sub before { +sub _add_method_modifier { + my $type = shift; my $meta = Moose::Meta::Role->initialize(shift); my $code = pop @_; for (@_) { croak "Roles do not currently support " . ref($_) - . " references for before method modifiers" + . " references for $type method modifiers" if ref $_; - $meta->add_before_method_modifier( $_, $code ); + my $add_method = "add_${type}_method_modifier"; + $meta->$add_method( $_, $code ); } } -sub after { - my $meta = Moose::Meta::Role->initialize(shift); +sub before { _add_method_modifier('before', @_) } - my $code = pop @_; - for (@_) { - croak "Roles do not currently support " - . ref($_) - . " references for after method modifiers" - if ref $_; - $meta->add_after_method_modifier( $_, $code ); - } -} +sub after { _add_method_modifier('after', @_) } -sub around { - my $meta = Moose::Meta::Role->initialize(shift); - my $code = pop @_; - for (@_) { - croak "Roles do not currently support " - . ref($_) - . " references for around method modifiers" - if ref $_; - $meta->add_around_method_modifier( $_, $code ); - } -} +sub around { _add_method_modifier('around', @_) } # see Moose.pm for discussion sub super {