21091d62e295e801b87f18476fcbb3a8f1dda261
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToRole.pm
1 package Moose::Meta::Role::Application::ToRole;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Scalar::Util    'blessed';
8
9 our $AUTHORITY = 'cpan:STEVAN';
10
11 use base 'Moose::Meta::Role::Application';
12
13 sub apply {
14     my ($self, $role1, $role2) = @_;
15     $self->SUPER::apply($role1, $role2);
16     $role2->add_role($role1);
17 }
18
19 sub check_role_exclusions {
20     my ($self, $role1, $role2) = @_;
21     if ( $role2->excludes_role($role1->name) ) {
22         require Moose;
23         Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'");
24     }
25     foreach my $excluded_role_name ($role1->get_excluded_roles_list) {
26         if ( $role2->does_role($excluded_role_name) ) {
27             require Moose;
28             Moose->throw_error("The class " . $role2->name . " does the excluded role '$excluded_role_name'");
29         }
30         $role2->add_excluded_roles($excluded_role_name);
31     }
32 }
33
34 sub check_required_methods {
35     my ($self, $role1, $role2) = @_;
36     foreach my $required_method ($role1->get_required_method_list) {
37         my $required_method_name = $required_method->name;
38
39         next if $self->is_aliased_method($required_method_name);
40
41         $role2->add_required_methods($required_method)
42             unless $role2->find_method_by_name($required_method_name);
43     }
44 }
45
46 sub check_required_attributes {
47
48 }
49
50 sub apply_attributes {
51     my ($self, $role1, $role2) = @_;
52     foreach my $attribute_name ($role1->get_attribute_list) {
53         # it if it has one already
54         if ($role2->has_attribute($attribute_name) &&
55             # make sure we haven't seen this one already too
56             $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) {
57
58             my $role2_name = $role2->name;
59
60             require Moose;
61             Moose->throw_error( "Role '"
62                     . $role1->name
63                     . "' has encountered an attribute conflict"
64                     . " while being composed into '$role2_name'."
65                     . " This is a fatal error and cannot be disambiguated."
66                     . " The conflicting attribute is named '$attribute_name'." );
67         }
68         else {
69             $role2->add_attribute(
70                 $role1->get_attribute($attribute_name)->clone
71             );
72         }
73     }
74 }
75
76 sub apply_methods {
77     my ( $self, $role1, $role2 ) = @_;
78     foreach my $method ( $role1->_get_local_methods ) {
79
80         my $method_name = $method->name;
81
82         next if $method->isa('Class::MOP::Method::Meta');
83
84         unless ( $self->is_method_excluded($method_name) ) {
85
86             my $role2_method = $role2->get_method($method_name);
87             if (   $role2_method
88                 && $role2_method->body != $method->body ) {
89
90                 # method conflicts between roles result in the method becoming
91                 # a requirement
92                 $role2->add_conflicting_method(
93                     name  => $method_name,
94                     roles => [ $role1->name, $role2->name ],
95                 );
96             }
97             else {
98                 $role2->add_method(
99                     $method_name,
100                     $method,
101                 );
102             }
103         }
104
105         next unless $self->is_method_aliased($method_name);
106
107         my $aliased_method_name = $self->get_method_aliases->{$method_name};
108
109         my $role2_method = $role2->get_method($aliased_method_name);
110
111         if (   $role2_method
112             && $role2_method->body != $method->body ) {
113
114             require Moose;
115             Moose->throw_error(
116                 "Cannot create a method alias if a local method of the same name exists"
117             );
118         }
119
120         $role2->add_method(
121             $aliased_method_name,
122             $role1->get_method($method_name)
123         );
124
125         if ( !$role2->has_method($method_name) ) {
126             $role2->add_required_methods($method_name)
127                 unless $self->is_method_excluded($method_name);
128         }
129     }
130 }
131
132 sub apply_override_method_modifiers {
133     my ($self, $role1, $role2) = @_;
134     foreach my $method_name ($role1->get_method_modifier_list('override')) {
135         # it if it has one already then ...
136         if ($role2->has_method($method_name)) {
137             # if it is being composed into another role
138             # we have a conflict here, because you cannot
139             # combine an overridden method with a locally
140             # defined one
141             require Moose;
142             Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
143                     "during composition (A local method of the same name as been found). This " .
144                     "is fatal error.");
145         }
146         else {
147             # if we are a role, we need to make sure
148             # we dont have a conflict with the role
149             # we are composing into
150             if ($role2->has_override_method_modifier($method_name) &&
151                 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
152
153                 require Moose;
154                 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
155                         "during composition (Two 'override' methods of the same name encountered). " .
156                         "This is fatal error.");
157             }
158             else {
159                 # if there is no conflict,
160                 # just add it to the role
161                 $role2->add_override_method_modifier(
162                     $method_name,
163                     $role1->get_override_method_modifier($method_name)
164                 );
165             }
166         }
167     }
168 }
169
170 sub apply_method_modifiers {
171     my ($self, $modifier_type, $role1, $role2) = @_;
172     my $add = "add_${modifier_type}_method_modifier";
173     my $get = "get_${modifier_type}_method_modifiers";
174     foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
175         $role2->$add(
176             $method_name,
177             $_
178         ) foreach $role1->$get($method_name);
179     }
180 }
181
182
183 1;
184
185 # ABSTRACT: Compose a role into another role
186
187 __END__
188
189 =pod
190
191 =head1 DESCRIPTION
192
193 =head2 METHODS
194
195 =over 4
196
197 =item B<new>
198
199 =item B<meta>
200
201 =item B<apply>
202
203 =item B<check_role_exclusions>
204
205 =item B<check_required_methods>
206
207 =item B<check_required_attributes>
208
209 =item B<apply_attributes>
210
211 =item B<apply_methods>
212
213 =item B<apply_method_modifiers>
214
215 =item B<apply_override_method_modifiers>
216
217 =back
218
219 =head1 BUGS
220
221 See L<Moose/BUGS> for details on reporting bugs.
222
223 =cut
224