From: Dave Rolsky Date: Tue, 30 Mar 2010 20:11:38 +0000 (-0500) Subject: Quick experiment and moving add_method_modifier from Util to a new Mixin::HasMethods. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fabandoned%2Fmixin-hasmethods;p=gitmo%2FMoose.git Quick experiment and moving add_method_modifier from Util to a new Mixin::HasMethods. Lots of tests fail. Not sure if this is worth doing. --- diff --git a/lib/Moose.pm b/lib/Moose.pm index b486feb..07e0f3f 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -70,15 +70,15 @@ sub has { } sub before { - Moose::Util::add_method_modifier(shift, 'before', \@_); + shift->add_method_modifier('before', \@_); } sub after { - Moose::Util::add_method_modifier(shift, 'after', \@_); + shift->add_method_modifier('after', \@_); } sub around { - Moose::Util::add_method_modifier(shift, 'around', \@_); + shift->add_method_modifier('around', \@_); } our $SUPER_PACKAGE; diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index f163043..ac15a64 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -22,7 +22,7 @@ use Moose::Meta::Class::Immutable::Trait; use Moose::Meta::Method::Constructor; use Moose::Meta::Method::Destructor; -use base 'Class::MOP::Class'; +use base 'Class::MOP::Class', 'Moose::Meta::Mixin::HasMethods'; __PACKAGE__->meta->add_attribute('roles' => ( reader => 'roles', diff --git a/lib/Moose/Meta/Mixin/HasMethods.pm b/lib/Moose/Meta/Mixin/HasMethods.pm new file mode 100644 index 0000000..ccf40fe --- /dev/null +++ b/lib/Moose/Meta/Mixin/HasMethods.pm @@ -0,0 +1,43 @@ +package Moose::Meta::Mixin::HasMethods; + +use strict; +use warnings; + +our $VERSION = '0.99'; +our $AUTHORITY = 'cpan:STEVAN'; + +use base 'Class::MOP::Mixin::HasMethods'; + +sub add_method_modifier { + my ( $self, $modifier_name, $args ) = @_; + + my $code = pop @{$args}; + my $add_modifier_method = 'add_' . $modifier_name . '_method_modifier'; + + if ( my $method_modifier_type = ref( @{$args}[0] ) ) { + if ( $method_modifier_type eq 'Regexp' ) { + my @all_methods = $self->get_all_methods; + my @matched_methods + = grep { $_->name =~ @{$args}[0] } @all_methods; + $self->$add_modifier_method( $_->name, $code ) + for @matched_methods; + } + elsif ( $method_modifier_type eq 'ARRAY' ) { + $self->$add_modifier_method( $_, $code ) for @{ $args->[0] }; + } + else { + $self->throw_error( + sprintf( + "Methods passed to %s must be provided as a list, arrayref or regex, not %s", + $modifier_name, + $method_modifier_type, + ) + ); + } + } + else { + $self->$add_modifier_method( $_, $code ) for @{$args}; + } +} + +1; diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 29d10fc..0883891 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -20,7 +20,7 @@ use Moose::Meta::Role::Method::Required; use Moose::Meta::Role::Method::Conflicting; use Moose::Util qw( ensure_all_roles ); -use base 'Class::MOP::Module', 'Class::MOP::Mixin::HasAttributes'; +use base 'Class::MOP::Module', 'Class::MOP::Mixin::HasAttributes', 'Moose::Meta::Mixin::HasMethods'; ## ------------------------------------------------------------------ ## NOTE: diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index e1277da..ad3120b 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -57,8 +57,7 @@ sub _add_method_modifier { . ref($_) . " references for $type method modifiers" if ref $_; - my $add_method = "add_${type}_method_modifier"; - $meta->$add_method( $_, $code ); + $meta->add_method_modifier( $type, $_, $code ); } } diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index 36e1d2e..b5a862a 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -22,7 +22,6 @@ my @exports = qw[ get_all_attribute_values resolve_metatrait_alias resolve_metaclass_alias - add_method_modifier english_list meta_attribute_alias meta_class_alias @@ -196,40 +195,6 @@ sub _build_alias_package_name { } } -sub add_method_modifier { - my ( $class_or_obj, $modifier_name, $args ) = @_; - my $meta - = $class_or_obj->can('add_before_method_modifier') - ? $class_or_obj - : find_meta($class_or_obj); - my $code = pop @{$args}; - my $add_modifier_method = 'add_' . $modifier_name . '_method_modifier'; - if ( my $method_modifier_type = ref( @{$args}[0] ) ) { - if ( $method_modifier_type eq 'Regexp' ) { - my @all_methods = $meta->get_all_methods; - my @matched_methods - = grep { $_->name =~ @{$args}[0] } @all_methods; - $meta->$add_modifier_method( $_->name, $code ) - for @matched_methods; - } - elsif ($method_modifier_type eq 'ARRAY') { - $meta->$add_modifier_method( $_, $code ) for @{$args->[0]}; - } - else { - $meta->throw_error( - sprintf( - "Methods passed to %s must be provided as a list, arrayref or regex, not %s", - $modifier_name, - $method_modifier_type, - ) - ); - } - } - else { - $meta->$add_modifier_method( $_, $code ) for @{$args}; - } -} - sub english_list { my @items = sort @_;