From: Shawn M Moore Date: Tue, 24 Jun 2008 06:03:53 +0000 (+0000) Subject: Add support for meta_attr->does("ShortAlias") X-Git-Tag: 0_55~99 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0db4f1d7369cb5767cbd44e43708afcd3da0931d;p=gitmo%2FMoose.git Add support for meta_attr->does("ShortAlias") --- diff --git a/Changes b/Changes index 570637a..82b997f 100644 --- a/Changes +++ b/Changes @@ -2,7 +2,9 @@ Revision history for Perl extension Moose 0.51 * Moose::Role - - add unimport so "no Moose::Role" actually does something + - add unimport so "no Moose::Role" actually does + something (sartak) + * Moose::Meta::Role::Application::ToRole - when RoleA did RoleB, and RoleA aliased a method from RoleB in order to provide its own implementation, that method still got @@ -12,6 +14,7 @@ Revision history for Perl extension Moose provide its own implementation. See Recipe 11 for an example of all this. (thanks Dave Rolsky) - added tests for this + * Moose::Meta::Method::Constructor - when a single argument that wasn't a hashref was provided to an immutabilized constructor, the error message was very @@ -19,6 +22,13 @@ Revision history for Perl extension Moose dew. (Thanks to Dave Rolsky) - added test for this (also Dave Rolsky) + * Moose::Meta::Attribute + - added support for meta_attr->does("ShortAlias") (sartak) + - added tests for this (sartak) + + * Moose::Object + - fix a typo in ->does (sartak) + 0.50 Thurs. Jun 11, 2008 - Fixed a version number issue by bumping all modules to 0.50. diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index cadc9d7..a3eef3c 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -51,13 +51,18 @@ __PACKAGE__->meta->add_attribute('traits' => ( predicate => 'has_applied_traits', )); -# NOTE: # we need to have a ->does method in here to # more easily support traits, and the introspection -# of those traits. So in order to do this we -# just alias Moose::Object's version of it. -# - SL -*does = \&Moose::Object::does; +# of those traits. We extend the does check to look +# for metatrait aliases. +sub does { + my ($self, $role_name) = @_; + my $name = eval { + Moose::Util::resolve_metatrait_alias(Attribute => $role_name) + }; + return 0 if !defined($name); # failed to load class + return Moose::Object::does($self, $name); +} sub new { my ($class, $name, %options) = @_; diff --git a/lib/Moose/Object.pm b/lib/Moose/Object.pm index f3dce58..d0a1555 100644 --- a/lib/Moose/Object.pm +++ b/lib/Moose/Object.pm @@ -71,7 +71,7 @@ sub DESTROY { sub does { my ($self, $role_name) = @_; (defined $role_name) - || confess "You much supply a role name to does()"; + || confess "You must supply a role name to does()"; my $meta = $self->meta; foreach my $class ($meta->class_precedence_list) { my $m = $meta->initialize($class); diff --git a/t/020_attributes/016_attribute_traits_registered.t b/t/020_attributes/016_attribute_traits_registered.t index 726ed04..01b9536 100644 --- a/t/020_attributes/016_attribute_traits_registered.t +++ b/t/020_attributes/016_attribute_traits_registered.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 16; +use Test::More tests => 24; use Test::Exception; use Test::Moose; @@ -88,6 +88,11 @@ my $bar_attr = $c->meta->get_attribute('bar'); does_ok($bar_attr, 'My::Attribute::Trait'); is($bar_attr->foo, "blah", "attr initialized"); +ok(!$bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity"); +ok($bar_attr->does('Aliased'), "attr->does uses aliases"); +ok(!$bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles"); +ok(!$bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles"); + my $quux = My::Derived::Class->new(bar => 1000); is($quux->bar, 1000, '... got the right value for bar'); @@ -104,6 +109,11 @@ does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' ); is($derived_bar_attr->the_other_attr, "oink", "attr initialized" ); +ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity"); +ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases"); +ok(!$derived_bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles"); +ok(!$derived_bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles"); + can_ok($quux, 'additional_method'); is(eval { $quux->additional_method }, 42, '... got the right value for additional_method');