sub apply_attributes {
my ($self, $role, $class) = @_;
- my $attr_metaclass = $role->applied_attribute_metaclass;
foreach my $attribute_name ($role->get_attribute_list) {
# it if it has one already
}
else {
$class->add_attribute(
- $role->get_attribute($attribute_name)->attribute_for_class($attr_metaclass)
+ $role->get_attribute($attribute_name)->attribute_for_class
);
}
}
);
__PACKAGE__->meta->add_attribute(
+ '_original_role' => (
+ reader => '_original_role',
+ )
+);
+
+__PACKAGE__->meta->add_attribute(
'is' => (
reader => 'is',
)
(defined $name)
|| confess "You must provide a name for the attribute";
+ my $role = delete $options{_original_role};
+
return bless {
name => $name,
original_options => \%options,
+ _original_role => $role,
%options,
}, $class;
}
weaken( $self->{'associated_role'} = $role );
}
+sub original_role {
+ my $self = shift;
+
+ return $self->_original_role || $self->associated_role;
+}
+
sub attribute_for_class {
- my $self = shift;
- my $metaclass = shift;
+ my $self = shift;
+
+ my $metaclass = $self->original_role->applied_attribute_metaclass;
return $metaclass->interpolate_class_and_new(
$self->name => %{ $self->original_options } );
sub clone {
my $self = shift;
- return ( ref $self )->new( $self->name, %{ $self->original_options } );
+ my $role = $self->original_role;
+
+ return ( ref $self )->new(
+ $self->name,
+ %{ $self->original_options },
+ _original_role => $role,
+ );
}
sub is_same_as {
Returns the L<Moose::Meta::Role> to which this attribute belongs, if any.
+=item B<< $attr->original_role >>
+
+Returns the L<Moose::Meta::Role> in which this attribute was first
+defined. This may not be the same as the value C<associated_role()> in the
+case of composite role, or the case where one role consumes other roles.
+
=item B<< $attr->original_options >>
Returns a hash reference of options passed to the constructor. This is used
my $baz = Quux->meta->get_attribute('baz');
ok(! does_role($baz, 'Quux::Meta::Role::Attribute'),
"applied_attribute traits do not end up applying to attributes from other roles during composition");
-}
-{
- local $TODO = "applied_attribute metaroles are lost in role composition";
my $bar = Quux->meta->get_attribute('bar');
does_ok($bar, 'Quux::Meta::Role::Attribute',
"attribute metarole applied correctly");