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