From: Jesse Luehrs Date: Fri, 31 Jul 2009 03:44:06 +0000 (-0500) Subject: add alias resolution for "use Moose -metaclass" X-Git-Tag: 0.89~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8a8856dedc0c13a38701fff0d9b8c1c2bef2a4a8;p=gitmo%2FMoose.git add alias resolution for "use Moose -metaclass" --- diff --git a/Changes b/Changes index 07144a9..0d8a3e7 100644 --- a/Changes +++ b/Changes @@ -14,6 +14,10 @@ next version * Moose::Meta::TypeConstraint - Add assert_valid method to use a TypeConstraint for assertion (rjbs) + * Moose::Exporter + - Make "use Moose -metaclass => 'Foo'" do alias resolution, like -traits + does. (doy) + 0.88 Fri Jul 24, 2009 * Moose::Manual::Contributing - Re-write the Moose::Manual::Contributing document to reflect diff --git a/lib/Moose.pm b/lib/Moose.pm index 1c77f78..44dc9e9 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -585,13 +585,8 @@ capabilities of the I keyword: they are the simplest way to extend the MOP, but they are still a fairly advanced topic and too much to cover here, see L for more information. -The default behavior here is to just load C<$metaclass_name>; however, we also -have a way to alias to a shorter name. This will first look to see if -B exists. If it does, Moose -will then check to see if that has the method C, which -should return the actual name of the custom attribute metaclass. If there is no -C method, it will fall back to using -B as the metaclass name. +See L for details on how a metaclass name +is resolved to a class name. =item I [ @role_names ]> @@ -599,8 +594,8 @@ This tells Moose to take the list of C<@role_names> and apply them to the attribute meta-object. This is very similar to the I option, but allows you to use more than one extension at a time. -See L for details on how a trait name is -resolved to a class name. +See L for details on how a trait name is +resolved to a role name. Also see L for a metaclass trait example. @@ -831,9 +826,10 @@ You can also specify traits which will be applied to your metaclass: This is very similar to the attribute traits feature. When you do this, your class's C object will have the specified traits -applied to it. See L for more details. +applied to it. See L for more +details. -=head2 Trait Name Resolution +=head2 Metaclass and Trait Name Resolution By default, when given a trait name, Moose simply tries to load a class of the same name. If such a class does not exist, it then looks @@ -848,6 +844,9 @@ return the I class name of the trait. If there is no C method, it will fall back to using B as the trait name. +The lookup method for metaclasses is the same, except that it looks +for a class matching B. + If all this is confusing, take a look at L, which demonstrates how to create an attribute trait. diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index cb46603..6ca7ca6 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -369,6 +369,9 @@ sub _make_import_sub { my $metaclass; ( $metaclass, @_ ) = _strip_metaclass(@_); + $metaclass = Moose::Util::resolve_metaclass_alias( + 'Class' => $metaclass + ) if defined $metaclass && length $metaclass; # Normally we could look at $_[0], but in some weird cases # (involving goto &Moose::import), $_[0] ends as something diff --git a/lib/Moose/Manual/Delta.pod b/lib/Moose/Manual/Delta.pod index a548c0a..cf98390 100644 --- a/lib/Moose/Manual/Delta.pod +++ b/lib/Moose/Manual/Delta.pod @@ -16,6 +16,11 @@ feature. If you encounter a problem and have a solution but don't see it documented here, or think we missed an important feature, please send us a patch. +=head1 Version 0.89 + +C<< use Moose -metaclass => 'Foo' >> now does alias resolution, just like +C<-traits> (and the C and C options to C). + =head1 Version 0.84 When an attribute generates I accessors, we now warn. This is to help diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index 01837ce..ed4c136 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -228,7 +228,8 @@ You can also specify traits which will be applied to your role metaclass: This is very similar to the attribute traits feature. When you do this, your class's C object will have the specified traits -applied to it. See L for more details. +applied to it. See L for more +details. =head1 CAVEATS diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index 079ad56..33b22b8 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -309,8 +309,8 @@ when specifying the C or C option for an attribute: metaclass => "Bar", ); -The name resolution mechanism is covered in L. +The name resolution mechanism is covered in +L. =item B diff --git a/t/050_metaclasses/022_new_metaclass.t b/t/050_metaclasses/022_new_metaclass.t index 1eca249..5d98c50 100644 --- a/t/050_metaclasses/022_new_metaclass.t +++ b/t/050_metaclasses/022_new_metaclass.t @@ -1,12 +1,15 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 2; do { package My::Meta::Class; use Moose; BEGIN { extends 'Moose::Meta::Class' }; + + package Moose::Meta::Class::Custom::MyMetaClass; + sub register_implementation { 'My::Meta::Class' } }; do { @@ -14,5 +17,11 @@ do { use Moose -metaclass => 'My::Meta::Class'; }; +do { + package My::Class::Aliased; + use Moose -metaclass => 'MyMetaClass'; +}; + is(My::Class->meta->meta->name, 'My::Meta::Class'); +is(My::Class::Aliased->meta->meta->name, 'My::Meta::Class');