Commit | Line | Data |
3f4cecde |
1 | package MooseX::ClassAttribute::Trait::Role::Composite; |
8a44b0a6 |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Moose::Util::MetaRole; |
7 | use Moose::Util qw(does_role); |
8 | |
9 | use namespace::autoclean; |
10 | use Moose::Role; |
8dcc7909 |
11 | |
8a44b0a6 |
12 | with 'MooseX::ClassAttribute::Trait::Role'; |
13 | |
14 | sub _merge_class_attributes { |
15 | my $self = shift; |
16 | |
17 | my @all_attributes; |
3f4cecde |
18 | foreach my $role ( @{ $self->get_roles() } ) { |
535052a9 |
19 | if ( does_role( $role, 'MooseX::ClassAttribute::Trait::Role' ) ) { |
8a44b0a6 |
20 | push @all_attributes, |
21 | map { $role->get_class_attribute($_) } |
3f4cecde |
22 | $role->get_class_attribute_list(); |
8a44b0a6 |
23 | } |
24 | } |
25 | |
26 | my %seen; |
27 | |
28 | foreach my $attribute (@all_attributes) { |
3f4cecde |
29 | my $name = $attribute->name(); |
30 | |
535052a9 |
31 | if ( exists $seen{$name} ) { |
8a44b0a6 |
32 | next if $seen{$name} == $attribute; |
33 | |
34 | require Moose; |
35 | Moose->throw_error( "Role '" |
36 | . $self->name() |
37 | . "' has encountered a class attribute conflict " |
8863e904 |
38 | . "during composition. This is a fatal error and cannot be disambiguated." |
8a44b0a6 |
39 | ); |
40 | } |
3f4cecde |
41 | |
8a44b0a6 |
42 | $seen{$name} = $attribute; |
43 | } |
3f4cecde |
44 | |
8a44b0a6 |
45 | foreach my $attribute (@all_attributes) { |
46 | $self->add_class_attribute( $attribute->clone() ); |
47 | } |
48 | |
49 | return keys %seen; |
50 | } |
51 | |
52 | around apply_params => sub { |
3f4cecde |
53 | my $orig = shift; |
54 | my $self = shift; |
8a44b0a6 |
55 | |
2626f89a |
56 | $self->$orig(@_); |
57 | |
58 | $self = Moose::Util::MetaRole::apply_metaroles( |
59 | for => $self, |
8a44b0a6 |
60 | role_metaroles => { |
61 | application_to_class => |
62 | ['MooseX::ClassAttribute::Trait::Application::ToClass'], |
63 | application_to_role => |
64 | ['MooseX::ClassAttribute::Trait::Application::ToRole'], |
65 | }, |
66 | ); |
3f4cecde |
67 | |
2626f89a |
68 | $self->_merge_class_attributes(); |
3f4cecde |
69 | |
2626f89a |
70 | return $self; |
8a44b0a6 |
71 | }; |
72 | |
73 | 1; |
74 | |
75 | # ABSTRACT: A trait that supports applying multiple roles at once |
76 | |
77 | __END__ |
78 | |
79 | =pod |
80 | |
81 | =head1 DESCRIPTION |
82 | |
83 | This trait is used to allow the application of multiple roles (one |
84 | or more of which contain class attributes) to a class or role. |
85 | |
86 | =head1 BUGS |
87 | |
88 | See L<MooseX::ClassAttribute> for details. |
89 | |
90 | =cut |