From: Stevan Little Date: Thu, 3 Jan 2008 07:08:08 +0000 (+0000) Subject: aliasing stuff X-Git-Tag: 0_35~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=43a41edeb57c090ba7f7f2ad44e7d31c09edbefc;p=gitmo%2FMoose.git aliasing stuff --- diff --git a/lib/Moose/Meta/Role/Application/ToClass.pm b/lib/Moose/Meta/Role/Application/ToClass.pm index f283ad7..51fb16a 100644 --- a/lib/Moose/Meta/Role/Application/ToClass.pm +++ b/lib/Moose/Meta/Role/Application/ToClass.pm @@ -114,25 +114,34 @@ sub apply_methods { next if $self->is_method_excluded($method_name); - my $orig_method_name = $method_name; - - if ($self->is_method_aliased($method_name)) { - $method_name = $self->get_method_aliases->{$method_name}; - } - # it if it has one already if ($class->has_method($method_name) && # and if they are not the same thing ... $class->get_method($method_name)->body != $role->get_method($method_name)->body) { next; } - else { + else { + # add it, although it could be overriden $class->alias_method( $method_name, - $role->get_method($orig_method_name) - ); + $role->get_method($method_name) + ); } + + if ($self->is_method_aliased($method_name)) { + my $aliased_method_name = $self->get_method_aliases->{$method_name}; + # it if it has one already + if ($class->has_method($aliased_method_name) && + # and if they are not the same thing ... + $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) { + confess "Cannot create a method alias if a local method of the same name exists"; + } + $class->alias_method( + $aliased_method_name, + $role->get_method($method_name) + ); + } } # we must reset the cache here since # we are just aliasing methods, otherwise diff --git a/lib/Moose/Meta/Role/Application/ToRole.pm b/lib/Moose/Meta/Role/Application/ToRole.pm index afd1a44..4f7ebdc 100644 --- a/lib/Moose/Meta/Role/Application/ToRole.pm +++ b/lib/Moose/Meta/Role/Application/ToRole.pm @@ -69,13 +69,7 @@ sub apply_methods { my ($self, $role1, $role2) = @_; foreach my $method_name ($role1->get_method_list) { - next if $self->is_method_excluded($method_name); - - my $orig_method_name = $method_name; - - if ($self->is_method_aliased($method_name)) { - $method_name = $self->get_method_aliases->{$method_name}; - } + next if $self->is_method_excluded($method_name); # it if it has one already if ($role2->has_method($method_name) && @@ -89,9 +83,24 @@ sub apply_methods { # add it, although it could be overriden $role2->alias_method( $method_name, - $role1->get_method($orig_method_name) + $role1->get_method($method_name) ); + } + + if ($self->is_method_aliased($method_name)) { + my $aliased_method_name = $self->get_method_aliases->{$method_name}; + # it if it has one already + if ($role2->has_method($aliased_method_name) && + # and if they are not the same thing ... + $role2->get_method($aliased_method_name)->body != $role1->get_method($method_name)->body) { + confess "Cannot create a method alias if a local method of the same name exists"; + } + $role2->alias_method( + $aliased_method_name, + $role1->get_method($method_name) + ); + } } } diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index 691337c..02bb03d 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -5,8 +5,8 @@ use strict; use warnings; use metaclass; -use overload '""' => sub { shift->name }, # stringify to tc name - fallback => 1; +#use overload '""' => sub { shift->name }, # stringify to tc name +# fallback => 1; use Sub::Name 'subname'; use Carp 'confess'; diff --git a/t/030_roles/013_method_aliasing_during_composition.t b/t/030_roles/013_method_aliasing_during_composition.t index 59130e6..c0587fe 100644 --- a/t/030_roles/013_method_aliasing_during_composition.t +++ b/t/030_roles/013_method_aliasing_during_composition.t @@ -26,10 +26,18 @@ BEGIN { ::lives_ok { with 'My::Role' => { alias => { bar => 'role_bar' } }; } '... this succeeds'; + + package My::Class::Failure; + use Moose; + + ::throws_ok { + with 'My::Role' => { alias => { bar => 'role_bar' } }; + } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds'; + + sub role_bar { 'FAIL' } } -ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar); -ok(!My::Class->meta->has_method('bar'), '... but we dont get bar'); +ok(My::Class->meta->has_method($_), "we have a $_ method") for qw(foo baz bar role_bar); { package My::OtherRole; @@ -40,10 +48,19 @@ ok(!My::Class->meta->has_method('bar'), '... but we dont get bar'); } '... this succeeds'; sub bar { 'My::OtherRole::bar' } + + package My::OtherRole::Failure; + use Moose::Role; + + ::throws_ok { + with 'My::Role' => { alias => { bar => 'role_bar' } }; + } qr/Cannot create a method alias if a local method of the same name exists/, '... this succeeds'; + + sub role_bar { 'FAIL' } } -ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo bar baz role_bar); -ok(!My::OtherRole->meta->requires_method('bar'), '... and the &bar method is not required'); +ok(My::OtherRole->meta->has_method($_), "we have a $_ method") for qw(foo baz role_bar); +ok(My::OtherRole->meta->requires_method('bar'), '... and the &bar method is required'); ok(!My::OtherRole->meta->requires_method('role_bar'), '... and the &role_bar method is not required'); diff --git a/t/040_type_constraints/001_util_type_constraints.t b/t/040_type_constraints/001_util_type_constraints.t index 353a344..72cfc3c 100644 --- a/t/040_type_constraints/001_util_type_constraints.t +++ b/t/040_type_constraints/001_util_type_constraints.t @@ -32,7 +32,7 @@ ok(Number(5), '... this is a Num'); ok(!defined(Number('Foo')), '... this is not a Num'); { my $number_tc = Moose::Util::TypeConstraints::find_type_constraint('Number'); - is("$number_tc", 'Number', '... type constraint stringifies to name'); + is($number_tc->name, 'Number', '... type constraint stringifies to name'); } ok(String('Foo'), '... this is a Str');