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