From: Jesse Luehrs Date: Mon, 27 Sep 2010 08:44:28 +0000 (-0500) Subject: actually, why not make the meta method renameable X-Git-Tag: 1.15~30 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2937ed180021b3276193c20c76dc2fe069ae1bfe;p=gitmo%2FMoose.git actually, why not make the meta method renameable --- diff --git a/lib/Moose.pm b/lib/Moose.pm index a916f6c..5790a1f 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -153,6 +153,7 @@ sub init_meta { or Moose->throw_error("Cannot call init_meta without specifying a for_class"); my $base_class = $args{base_class} || 'Moose::Object'; my $metaclass = $args{metaclass} || 'Moose::Meta::Class'; + my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta'; Moose->throw_error("The Metaclass $metaclass must be a subclass of Moose::Meta::Class.") unless $metaclass->isa('Moose::Meta::Class'); @@ -198,17 +199,19 @@ sub init_meta { $meta = $metaclass->initialize($class); } - unless ($args{no_meta}) { + if (defined $meta_name) { # also check for inherited non moose 'meta' method? - my $existing = $meta->get_method('meta'); + my $existing = $meta->get_method($meta_name); if ($existing && !$existing->isa('Class::MOP::Method::Meta')) { Carp::cluck "Moose is overwriting an existing method named " - . "'meta' with its own version, in class $class. If " - . "this is actually what you want, you should remove " - . "the existing method, otherwise, you should pass " - . "the '-no_meta => 1' option to 'use Moose'."; + . "$meta_name in class $class with a method " + . "which returns the class's metaclass. If this is " + . "actually what you want, you should remove the " + . "existing method, otherwise, you should rename or " + . "disable this generated method using the " + . "'-meta_name' option to 'use Moose'."; } - $meta->_add_meta_method; + $meta->_add_meta_method($meta_name); } # make sure they inherit from Moose::Object diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index 1064050..57d5a55 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -357,8 +357,8 @@ sub _make_import_sub { = Moose::Util::resolve_metaclass_alias( 'Class' => $metaclass ) if defined $metaclass && length $metaclass; - my $no_meta; - ( $no_meta, @_ ) = _strip_no_meta(@_); + my $meta_name; + ( $meta_name, @_ ) = _strip_meta_name(@_); # Normally we could look at $_[0], but in some weird cases # (involving goto &Moose::import), $_[0] ends as something @@ -386,7 +386,7 @@ sub _make_import_sub { $c->init_meta( for_class => $CALLER, metaclass => $metaclass, - no_meta => $no_meta, + meta_name => $meta_name, ); $did_init_meta = 1; } @@ -451,16 +451,16 @@ sub _strip_metaclass { return ( $metaclass, @_ ); } -sub _strip_no_meta { - my $idx = first_index { $_ eq '-no_meta' } @_; +sub _strip_meta_name { + my $idx = first_index { $_ eq '-meta_name' } @_; - return ( undef, @_ ) unless $idx >= 0 && $#_ >= $idx + 1; + return ( 'meta', @_ ) unless $idx >= 0 && $#_ >= $idx + 1; - my $no_meta = $_[ $idx + 1 ]; + my $meta_name = $_[ $idx + 1 ]; splice @_, $idx, 2; - return ( $no_meta, @_ ); + return ( $meta_name, @_ ); } sub _apply_meta_traits { diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 2dff118..4cfff23 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -479,12 +479,15 @@ sub create { || confess "You must pass a HASH ref of methods" if exists $options{methods}; + $options{meta_name} = 'meta' + unless exists $options{meta_name}; + my (%initialize_options) = %options; delete @initialize_options{qw( package attributes methods - no_meta + meta_name version authority )}; @@ -493,7 +496,8 @@ sub create { $meta->_instantiate_module( $options{version}, $options{authority} ); - $meta->_add_meta_method if !$options{no_meta}; + $meta->_add_meta_method($options{meta_name}) + if defined $options{meta_name}; if (exists $options{attributes}) { foreach my $attribute_name (keys %{$options{attributes}}) { diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index a058222..95c02bb 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -111,6 +111,7 @@ sub init_meta { } my $metaclass = $args{metaclass} || "Moose::Meta::Role"; + my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta'; Moose->throw_error("The Metaclass $metaclass must be a subclass of Moose::Meta::Role.") unless $metaclass->isa('Moose::Meta::Role'); @@ -133,17 +134,19 @@ sub init_meta { $meta = $metaclass->initialize($role); } - unless ($args{no_meta}) { + if (defined $meta_name) { # also check for inherited non moose 'meta' method? - my $existing = $meta->get_method('meta'); + my $existing = $meta->get_method($meta_name); if ($existing && !$existing->isa('Class::MOP::Method::Meta')) { Carp::cluck "Moose::Role is overwriting an existing method named " - . "'meta' with its own version, in role $role. If " - . "this is actually what you want, you should remove " - . "the existing method, otherwise, you should pass " - . "the '-no_meta => 1' option to 'use Moose::Role'."; + . "$meta_name in role $role with a method " + . "which returns the class's metaclass. If this is " + . "actually what you want, you should remove the " + . "existing method, otherwise, you should rename or " + . "disable this generated method using the " + . "'-meta_name' option to 'use Moose::Role'."; } - $meta->_add_meta_method; + $meta->_add_meta_method($meta_name); } return $meta;