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