From: Stevan Little Date: Mon, 21 Jan 2008 19:52:06 +0000 (+0000) Subject: cleaning up the traits things X-Git-Tag: 0_35~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3bb22459b80cac90a821e3cbf1eaf95727445e05;p=gitmo%2FMoose.git cleaning up the traits things --- diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 265f037..a784547 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -316,7 +316,22 @@ sub _process_attribute { superclasses => [ $attr_metaclass_name ] ); $ANON_CLASSES{$anon_role_key} = $class; - Moose::Util::apply_all_roles($class, @{$options{traits}}); + + my @traits; + foreach my $trait (@{$options{traits}}) { + eval { + my $possible_full_name = 'Moose::Meta::Attribute::Custom::Trait::' . $trait; + Class::MOP::load_class($possible_full_name); + push @traits => $possible_full_name->can('register_implementation') + ? $possible_full_name->register_implementation + : $possible_full_name; + }; + if ($@) { + push @traits => $trait; + } + } + + Moose::Util::apply_all_roles($class, @traits); } $attr_metaclass_name = $class->name; diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index cad5539..7b18a3f 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -147,7 +147,9 @@ Returns first class in precedence list that consumed C<$role_name>. Given an C<$applicant> (which can somehow be turned into either a metaclass or a metarole) and a list of C<@roles> this will do the right thing to apply the C<@roles> to the C<$applicant>. This is -actually used internally by both L and L. +actually used internally by both L and L, and the +C<@roles> will be pre-processed through L +to allow for the additional arguments to be passed. =back diff --git a/t/020_attributes/015_attribute_traits.t b/t/020_attributes/015_attribute_traits.t index 4683e3d..8c985fa 100644 --- a/t/020_attributes/015_attribute_traits.t +++ b/t/020_attributes/015_attribute_traits.t @@ -3,8 +3,9 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 6; use Test::Exception; +use Test::Moose; BEGIN { use_ok('Moose'); @@ -44,3 +45,9 @@ is($c->bar, 100, '... got the right value for bar'); can_ok($c, 'baz'); is($c->baz, 100, '... got the right value for baz'); + +does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait'); + + + + diff --git a/t/020_attributes/016_attribute_traits_registered.t b/t/020_attributes/016_attribute_traits_registered.t new file mode 100644 index 0000000..2060f02 --- /dev/null +++ b/t/020_attributes/016_attribute_traits_registered.t @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 6; +use Test::Exception; +use Test::Moose; + +BEGIN { + use_ok('Moose'); +} + +{ + package My::Attribute::Trait; + use Moose::Role; + + has 'alias_to' => (is => 'ro', isa => 'Str'); + + after 'install_accessors' => sub { + my $self = shift; + $self->associated_class->add_method( + $self->alias_to, + $self->get_read_method_ref + ); + }; + + package Moose::Meta::Attribute::Custom::Trait::Aliased; + sub register_implementation { 'My::Attribute::Trait' } +} + +{ + package My::Class; + use Moose; + + has 'bar' => ( + traits => [qw/Aliased/], + is => 'ro', + isa => 'Int', + alias_to => 'baz', + ); +} + +my $c = My::Class->new(bar => 100); +isa_ok($c, 'My::Class'); + +is($c->bar, 100, '... got the right value for bar'); + +can_ok($c, 'baz'); +is($c->baz, 100, '... got the right value for baz'); + +does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait');