Make sure roles are applied to right metaclass
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute / Trait / Application / ToRole.pm
CommitLineData
63fcc508 1package MooseX::ClassAttribute::Trait::Application::ToRole;
88b7f2c8 2
3use strict;
4use warnings;
5
6use Moose::Util::MetaRole;
63fcc508 7use MooseX::ClassAttribute::Trait::Application::ToClass;
88b7f2c8 8
9use namespace::autoclean;
10use Moose::Role;
11
63fcc508 12with 'MooseX::ClassAttribute::Trait::Application';
88b7f2c8 13
fb949f72 14around apply => sub {
15 my $orig = shift;
88b7f2c8 16 my $self = shift;
17 my $role1 = shift;
18 my $role2 = shift;
19
dbf3ee23 20 $role2 = Moose::Util::MetaRole::apply_metaroles(
88b7f2c8 21 for => $role2,
22 role_metaroles => {
63fcc508 23 role => ['MooseX::ClassAttribute::Trait::Role'],
88b7f2c8 24 application_to_class =>
63fcc508 25 ['MooseX::ClassAttribute::Trait::Application::ToClass'],
88b7f2c8 26 application_to_role =>
63fcc508 27 ['MooseX::ClassAttribute::Trait::Application::ToRole'],
88b7f2c8 28 },
29 );
30
fb949f72 31 $self->$orig( $role1, $role2 );
32};
33
34sub _apply_class_attributes {
35 my $self = shift;
36 my $role1 = shift;
37 my $role2 = shift;
38
88b7f2c8 39 foreach my $attribute_name ( $role1->get_class_attribute_list() ) {
40 if ( $role2->has_class_attribute($attribute_name)
41 && $role2->get_class_attribute($attribute_name)
42 != $role1->get_class_attribute($attribute_name) ) {
43
44 require Moose;
45 Moose->throw_error( "Role '"
46 . $role1->name()
47 . "' has encountered a class attribute conflict "
48 . "during composition. This is fatal error and cannot be disambiguated."
49 );
50 }
51 else {
52 $role2->add_class_attribute(
53 $role1->get_class_attribute($attribute_name)->clone() );
54 }
55 }
56}
57
581;
04b89789 59
0d0bf8c3 60# ABSTRACT: A trait that supports applying class attributes to roles
61
04b89789 62__END__
63
64=pod
65
04b89789 66=head1 DESCRIPTION
67
68This trait is used to allow the application of roles containing class
69attributes to roles.
70
04b89789 71=head1 BUGS
72
73See L<MooseX::ClassAttribute> for details.
74
04b89789 75=cut