00ff4404f78d036d9f4bcb98d51d3ffa225bf353
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute / Trait / Application / ToRole.pm
1 package MooseX::ClassAttribute::Trait::Application::ToRole;
2
3 use strict;
4 use warnings;
5
6 our $VERSION   = '0.11';
7
8 use Moose::Util::MetaRole;
9 use MooseX::ClassAttribute::Trait::Application::ToClass;
10 use MooseX::ClassAttribute::Trait::Application::ToInstance;
11
12 use namespace::autoclean;
13 use Moose::Role;
14
15 with 'MooseX::ClassAttribute::Trait::Application';
16
17 sub _apply_class_attributes {
18     my $self  = shift;
19     my $role1 = shift;
20     my $role2 = shift;
21
22     $role2 = Moose::Util::MetaRole::apply_metaclass_roles(
23         for            => $role2,
24         role_metaroles => {
25             role => ['MooseX::ClassAttribute::Trait::Role'],
26             application_to_class =>
27                 ['MooseX::ClassAttribute::Trait::Application::ToClass'],
28             application_to_role =>
29                 ['MooseX::ClassAttribute::Trait::Application::ToRole'],
30             application_to_instance => [
31                 'MooseX::ClassAttribute::Trait::Application::ToInstance'
32             ],
33         },
34     );
35
36     foreach my $attribute_name ( $role1->get_class_attribute_list() ) {
37         if (   $role2->has_class_attribute($attribute_name)
38             && $role2->get_class_attribute($attribute_name)
39             != $role1->get_class_attribute($attribute_name) ) {
40
41             require Moose;
42             Moose->throw_error( "Role '"
43                     . $role1->name()
44                     . "' has encountered a class attribute conflict "
45                     . "during composition. This is fatal error and cannot be disambiguated."
46             );
47         }
48         else {
49             $role2->add_class_attribute(
50                 $role1->get_class_attribute($attribute_name)->clone() );
51         }
52     }
53 }
54
55 1;
56
57 __END__
58
59 =pod
60
61 =head1 NAME
62
63 MooseX::ClassAttribute::Trait::Application::ToRole - A trait that supports applying class attributes to roles
64
65 =head1 DESCRIPTION
66
67 This trait is used to allow the application of roles containing class
68 attributes to roles.
69
70 =head1 AUTHOR
71
72 Dave Rolsky, C<< <autarch@urth.org> >>
73
74 =head1 BUGS
75
76 See L<MooseX::ClassAttribute> for details.
77
78 =head1 COPYRIGHT & LICENSE
79
80 Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
81
82 This program is free software; you can redistribute it and/or modify
83 it under the same terms as Perl itself.
84
85 =cut