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 | |
b6cca0d5 |
9 | our $VERSION = '1.14'; |
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 | |
51d4595e |
60 | my $role2_name = $role2->name; |
61 | |
70ea9161 |
62 | require Moose; |
51d4595e |
63 | Moose->throw_error( "Role '" |
64 | . $role1->name |
65 | . "' has encountered an attribute conflict" |
66 | . " while being composed into '$role2_name'." |
67 | . " This is a fatal error and cannot be disambiguated." |
68 | . " The conflicting attribute is named '$attribute_name'." ); |
1c9db35c |
69 | } |
70 | else { |
71 | $role2->add_attribute( |
f785aad8 |
72 | $role1->get_attribute($attribute_name)->clone |
1c9db35c |
73 | ); |
74 | } |
75 | } |
76 | } |
77 | |
78 | sub apply_methods { |
4a9e8ff6 |
79 | my ( $self, $role1, $role2 ) = @_; |
80 | foreach my $method ( $role1->_get_local_methods ) { |
81 | |
82 | my $method_name = $method->name; |
83 | |
0385d05e |
84 | next if $method_name eq 'meta'; |
db9476b1 |
85 | |
bd38046e |
86 | unless ( $self->is_method_excluded($method_name) ) { |
4a9e8ff6 |
87 | |
88 | my $role2_method = $role2->get_method($method_name); |
89 | if ( $role2_method |
90 | && $role2_method->body != $method->body ) { |
bd38046e |
91 | |
92 | # method conflicts between roles result in the method becoming |
93 | # a requirement |
94 | $role2->add_conflicting_method( |
95 | name => $method_name, |
96 | roles => [ $role1->name, $role2->name ], |
97 | ); |
98 | } |
99 | else { |
100 | $role2->add_method( |
101 | $method_name, |
4a9e8ff6 |
102 | $method, |
bd38046e |
103 | ); |
104 | } |
105 | } |
106 | |
4a9e8ff6 |
107 | next unless $self->is_method_aliased($method_name); |
094b50c0 |
108 | |
4a9e8ff6 |
109 | my $aliased_method_name = $self->get_method_aliases->{$method_name}; |
70ea9161 |
110 | |
4a9e8ff6 |
111 | my $role2_method = $role2->get_method($aliased_method_name); |
db9476b1 |
112 | |
4a9e8ff6 |
113 | if ( $role2_method |
114 | && $role2_method->body != $method->body ) { |
115 | |
116 | require Moose; |
117 | Moose->throw_error( |
118 | "Cannot create a method alias if a local method of the same name exists" |
db9476b1 |
119 | ); |
4a9e8ff6 |
120 | } |
db9476b1 |
121 | |
4a9e8ff6 |
122 | $role2->add_method( |
123 | $aliased_method_name, |
124 | $role1->get_method($method_name) |
125 | ); |
126 | |
127 | if ( !$role2->has_method($method_name) ) { |
128 | $role2->add_required_methods($method_name) |
129 | unless $self->is_method_excluded($method_name); |
1c9db35c |
130 | } |
131 | } |
132 | } |
133 | |
134 | sub apply_override_method_modifiers { |
135 | my ($self, $role1, $role2) = @_; |
136 | foreach my $method_name ($role1->get_method_modifier_list('override')) { |
137 | # it if it has one already then ... |
138 | if ($role2->has_method($method_name)) { |
139 | # if it is being composed into another role |
140 | # we have a conflict here, because you cannot |
6549b0d1 |
141 | # combine an overridden method with a locally |
1c9db35c |
142 | # defined one |
70ea9161 |
143 | require Moose; |
c245d69b |
144 | Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " . |
1c9db35c |
145 | "during composition (A local method of the same name as been found). This " . |
4c0b3599 |
146 | "is fatal error."); |
1c9db35c |
147 | } |
148 | else { |
149 | # if we are a role, we need to make sure |
150 | # we dont have a conflict with the role |
151 | # we are composing into |
152 | if ($role2->has_override_method_modifier($method_name) && |
153 | $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) { |
70ea9161 |
154 | |
155 | require Moose; |
c245d69b |
156 | Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " . |
1c9db35c |
157 | "during composition (Two 'override' methods of the same name encountered). " . |
4c0b3599 |
158 | "This is fatal error."); |
1c9db35c |
159 | } |
160 | else { |
161 | # if there is no conflict, |
162 | # just add it to the role |
163 | $role2->add_override_method_modifier( |
164 | $method_name, |
165 | $role1->get_override_method_modifier($method_name) |
166 | ); |
167 | } |
168 | } |
169 | } |
170 | } |
171 | |
172 | sub apply_method_modifiers { |
173 | my ($self, $modifier_type, $role1, $role2) = @_; |
174 | my $add = "add_${modifier_type}_method_modifier"; |
175 | my $get = "get_${modifier_type}_method_modifiers"; |
176 | foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) { |
177 | $role2->$add( |
178 | $method_name, |
179 | $_ |
180 | ) foreach $role1->$get($method_name); |
181 | } |
182 | } |
183 | |
1c9db35c |
184 | |
fb1e11d5 |
185 | 1; |
186 | |
187 | __END__ |
188 | |
189 | =pod |
190 | |
191 | =head1 NAME |
192 | |
ab76842e |
193 | Moose::Meta::Role::Application::ToRole - Compose a role into another role |
fb1e11d5 |
194 | |
195 | =head1 DESCRIPTION |
196 | |
197 | =head2 METHODS |
198 | |
199 | =over 4 |
200 | |
201 | =item B<new> |
202 | |
203 | =item B<meta> |
204 | |
1c9db35c |
205 | =item B<apply> |
206 | |
709c321c |
207 | =item B<check_role_exclusions> |
208 | |
1c9db35c |
209 | =item B<check_required_methods> |
210 | |
709c321c |
211 | =item B<check_required_attributes> |
1c9db35c |
212 | |
213 | =item B<apply_attributes> |
214 | |
215 | =item B<apply_methods> |
216 | |
217 | =item B<apply_method_modifiers> |
218 | |
1c9db35c |
219 | =item B<apply_override_method_modifiers> |
220 | |
fb1e11d5 |
221 | =back |
222 | |
223 | =head1 BUGS |
224 | |
d4048ef3 |
225 | See L<Moose/BUGS> for details on reporting bugs. |
fb1e11d5 |
226 | |
227 | =head1 AUTHOR |
228 | |
229 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
230 | |
231 | =head1 COPYRIGHT AND LICENSE |
232 | |
7e0492d3 |
233 | Copyright 2006-2010 by Infinity Interactive, Inc. |
fb1e11d5 |
234 | |
235 | L<http://www.iinteractive.com> |
236 | |
237 | This library is free software; you can redistribute it and/or modify |
238 | it under the same terms as Perl itself. |
239 | |
240 | =cut |
241 | |