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;
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<Moose> and L<Moose::Role>.
+actually used internally by both L<Moose> and L<Moose::Role>, and the
+C<@roles> will be pre-processed through L<Data::OptList::mkopt>
+to allow for the additional arguments to be passed.
=back
use strict;
use warnings;
-use Test::More tests => 5;
+use Test::More tests => 6;
use Test::Exception;
+use Test::Moose;
BEGIN {
use_ok('Moose');
can_ok($c, 'baz');
is($c->baz, 100, '... got the right value for baz');
+
+does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait');
+
+
+
+
--- /dev/null
+#!/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');