11 package My::Attribute::Trait;
14 has 'alias_to' => (is => 'ro', isa => 'Str');
16 has foo => ( is => "ro", default => "blah" );
18 after 'install_accessors' => sub {
20 $self->associated_class->add_method(
22 $self->get_read_method_ref
26 package Moose::Meta::Attribute::Custom::Trait::Aliased;
27 sub register_implementation { 'My::Attribute::Trait' }
31 package My::Other::Attribute::Trait;
38 has the_other_attr => ( isa => "Str", is => "rw", default => "oink" );
40 after 'install_accessors' => sub {
42 $self->associated_class->add_method(
48 package Moose::Meta::Attribute::Custom::Trait::Other;
49 sub register_implementation { 'My::Other::Attribute::Trait' }
57 traits => [qw/Aliased/],
65 package My::Derived::Class;
71 traits => [qw/Other/],
75 my $c = My::Class->new(bar => 100);
76 isa_ok($c, 'My::Class');
78 is($c->bar, 100, '... got the right value for bar');
81 is($c->baz, 100, '... got the right value for baz');
83 my $bar_attr = $c->meta->get_attribute('bar');
84 does_ok($bar_attr, 'My::Attribute::Trait');
85 is($bar_attr->foo, "blah", "attr initialized");
87 ok(!$bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
88 ok($bar_attr->does('Aliased'), "attr->does uses aliases");
89 ok(!$bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
90 ok(!$bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
92 my $quux = My::Derived::Class->new(bar => 1000);
94 is($quux->bar, 1000, '... got the right value for bar');
97 is($quux->baz, 1000, '... got the right value for baz');
99 my $derived_bar_attr = $quux->meta->get_attribute("bar");
100 does_ok($derived_bar_attr, 'My::Attribute::Trait' );
102 is( $derived_bar_attr->foo, "blah", "attr initialized" );
104 does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' );
106 is($derived_bar_attr->the_other_attr, "oink", "attr initialized" );
108 ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
109 ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases");
110 ok(!$derived_bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
111 ok(!$derived_bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
113 can_ok($quux, 'additional_method');
114 is(eval { $quux->additional_method }, 42, '... got the right value for additional_method');