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 | |
56d7c745 |
9 | our $VERSION = '0.73'; |
e606ae5f |
10 | $VERSION = eval $VERSION; |
fb1e11d5 |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
13 | use base 'Moose::Meta::Role::Application'; |
14 | |
1c9db35c |
15 | sub apply { |
c4538447 |
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) = @_; |
38 | foreach my $required_method_name ($role1->get_required_method_list) { |
3e19778d |
39 | |
40 | next if $self->is_aliased_method($required_method_name); |
41 | |
1c9db35c |
42 | $role2->add_required_methods($required_method_name) |
43 | unless $role2->find_method_by_name($required_method_name); |
44 | } |
45 | } |
46 | |
709c321c |
47 | sub check_required_attributes { |
48 | |
49 | } |
50 | |
1c9db35c |
51 | sub apply_attributes { |
52 | my ($self, $role1, $role2) = @_; |
53 | foreach my $attribute_name ($role1->get_attribute_list) { |
54 | # it if it has one already |
55 | if ($role2->has_attribute($attribute_name) && |
56 | # make sure we haven't seen this one already too |
57 | $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) { |
70ea9161 |
58 | |
59 | require Moose; |
c245d69b |
60 | Moose->throw_error("Role '" . $role1->name . "' has encountered an attribute conflict " . |
4c0b3599 |
61 | "during composition. This is fatal error and cannot be disambiguated."); |
1c9db35c |
62 | } |
63 | else { |
64 | $role2->add_attribute( |
65 | $attribute_name, |
66 | $role1->get_attribute($attribute_name) |
67 | ); |
68 | } |
69 | } |
70 | } |
71 | |
72 | sub apply_methods { |
73 | my ($self, $role1, $role2) = @_; |
74 | foreach my $method_name ($role1->get_method_list) { |
db9476b1 |
75 | |
76 | if ($self->is_method_aliased($method_name)) { |
77 | my $aliased_method_name = $self->get_method_aliases->{$method_name}; |
78 | # it if it has one already |
79 | if ($role2->has_method($aliased_method_name) && |
80 | # and if they are not the same thing ... |
81 | $role2->get_method($aliased_method_name)->body != $role1->get_method($method_name)->body) { |
70ea9161 |
82 | |
83 | require Moose; |
c245d69b |
84 | Moose->throw_error("Cannot create a method alias if a local method of the same name exists"); |
db9476b1 |
85 | } |
86 | |
87e63626 |
87 | $role2->add_method( |
db9476b1 |
88 | $aliased_method_name, |
89 | $role1->get_method($method_name) |
90 | ); |
91 | |
92 | if (!$role2->has_method($method_name)) { |
f5b6d42e |
93 | $role2->add_required_methods($method_name) |
94 | unless $self->is_method_excluded($method_name); |
db9476b1 |
95 | } |
96 | |
97 | next; |
f5b6d42e |
98 | } |
99 | |
100 | next if $self->is_method_excluded($method_name); |
3e19778d |
101 | |
1c9db35c |
102 | # it if it has one already |
103 | if ($role2->has_method($method_name) && |
104 | # and if they are not the same thing ... |
105 | $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) { |
106 | # method conflicts between roles result |
107 | # in the method becoming a requirement |
108 | $role2->add_required_methods($method_name); |
109 | } |
110 | else { |
6549b0d1 |
111 | # add it, although it could be overridden |
87e63626 |
112 | $role2->add_method( |
1c9db35c |
113 | $method_name, |
43a41ede |
114 | $role1->get_method($method_name) |
1c9db35c |
115 | ); |
43a41ede |
116 | |
1c9db35c |
117 | } |
43a41ede |
118 | |
1c9db35c |
119 | } |
120 | } |
121 | |
122 | sub apply_override_method_modifiers { |
123 | my ($self, $role1, $role2) = @_; |
124 | foreach my $method_name ($role1->get_method_modifier_list('override')) { |
125 | # it if it has one already then ... |
126 | if ($role2->has_method($method_name)) { |
127 | # if it is being composed into another role |
128 | # we have a conflict here, because you cannot |
6549b0d1 |
129 | # combine an overridden method with a locally |
1c9db35c |
130 | # defined one |
70ea9161 |
131 | require Moose; |
c245d69b |
132 | Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " . |
1c9db35c |
133 | "during composition (A local method of the same name as been found). This " . |
4c0b3599 |
134 | "is fatal error."); |
1c9db35c |
135 | } |
136 | else { |
137 | # if we are a role, we need to make sure |
138 | # we dont have a conflict with the role |
139 | # we are composing into |
140 | if ($role2->has_override_method_modifier($method_name) && |
141 | $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) { |
70ea9161 |
142 | |
143 | require Moose; |
c245d69b |
144 | Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " . |
1c9db35c |
145 | "during composition (Two 'override' methods of the same name encountered). " . |
4c0b3599 |
146 | "This is fatal error."); |
1c9db35c |
147 | } |
148 | else { |
149 | # if there is no conflict, |
150 | # just add it to the role |
151 | $role2->add_override_method_modifier( |
152 | $method_name, |
153 | $role1->get_override_method_modifier($method_name) |
154 | ); |
155 | } |
156 | } |
157 | } |
158 | } |
159 | |
160 | sub apply_method_modifiers { |
161 | my ($self, $modifier_type, $role1, $role2) = @_; |
162 | my $add = "add_${modifier_type}_method_modifier"; |
163 | my $get = "get_${modifier_type}_method_modifiers"; |
164 | foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) { |
165 | $role2->$add( |
166 | $method_name, |
167 | $_ |
168 | ) foreach $role1->$get($method_name); |
169 | } |
170 | } |
171 | |
1c9db35c |
172 | |
fb1e11d5 |
173 | 1; |
174 | |
175 | __END__ |
176 | |
177 | =pod |
178 | |
179 | =head1 NAME |
180 | |
ab76842e |
181 | Moose::Meta::Role::Application::ToRole - Compose a role into another role |
fb1e11d5 |
182 | |
183 | =head1 DESCRIPTION |
184 | |
185 | =head2 METHODS |
186 | |
187 | =over 4 |
188 | |
189 | =item B<new> |
190 | |
191 | =item B<meta> |
192 | |
1c9db35c |
193 | =item B<apply> |
194 | |
709c321c |
195 | =item B<check_role_exclusions> |
196 | |
1c9db35c |
197 | =item B<check_required_methods> |
198 | |
709c321c |
199 | =item B<check_required_attributes> |
1c9db35c |
200 | |
201 | =item B<apply_attributes> |
202 | |
203 | =item B<apply_methods> |
204 | |
205 | =item B<apply_method_modifiers> |
206 | |
1c9db35c |
207 | =item B<apply_override_method_modifiers> |
208 | |
fb1e11d5 |
209 | =back |
210 | |
211 | =head1 BUGS |
212 | |
213 | All complex software has bugs lurking in it, and this module is no |
214 | exception. If you find a bug please either email me, or add the bug |
215 | to cpan-RT. |
216 | |
217 | =head1 AUTHOR |
218 | |
219 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
220 | |
221 | =head1 COPYRIGHT AND LICENSE |
222 | |
2840a3b2 |
223 | Copyright 2006-2009 by Infinity Interactive, Inc. |
fb1e11d5 |
224 | |
225 | L<http://www.iinteractive.com> |
226 | |
227 | This library is free software; you can redistribute it and/or modify |
228 | it under the same terms as Perl itself. |
229 | |
230 | =cut |
231 | |