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