Commit | Line | Data |
fb1e11d5 |
1 | package Moose::Meta::Role::Application::ToClass; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
d939e016 |
7 | use Moose::Util 'english_list'; |
8 | use Scalar::Util 'blessed'; |
fb1e11d5 |
9 | |
74397c13 |
10 | our $VERSION = '0.75_01'; |
e606ae5f |
11 | $VERSION = eval $VERSION; |
fb1e11d5 |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
13 | |
14 | use base 'Moose::Meta::Role::Application'; |
15 | |
1c9db35c |
16 | sub apply { |
c4538447 |
17 | my ($self, $role, $class) = @_; |
1c9db35c |
18 | $self->SUPER::apply($role, $class); |
c4538447 |
19 | $class->add_role($role); |
1c9db35c |
20 | } |
21 | |
22 | sub check_role_exclusions { |
23 | my ($self, $role, $class) = @_; |
24 | if ($class->excludes_role($role->name)) { |
4c0b3599 |
25 | $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'"); |
1c9db35c |
26 | } |
27 | foreach my $excluded_role_name ($role->get_excluded_roles_list) { |
28 | if ($class->does_role($excluded_role_name)) { |
4c0b3599 |
29 | $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'"); |
1c9db35c |
30 | } |
31 | } |
32 | } |
33 | |
34 | sub check_required_methods { |
35 | my ($self, $role, $class) = @_; |
d939e016 |
36 | |
37 | my @missing; |
38 | my @is_attr; |
39 | |
1c9db35c |
40 | # NOTE: |
41 | # we might need to move this down below the |
42 | # the attributes so that we can require any |
43 | # attribute accessors. However I am thinking |
44 | # that maybe those are somehow exempt from |
45 | # the require methods stuff. |
46 | foreach my $required_method_name ($role->get_required_method_list) { |
47 | |
3e19778d |
48 | if (!$class->find_method_by_name($required_method_name)) { |
49 | |
50 | next if $self->is_aliased_method($required_method_name); |
d939e016 |
51 | |
52 | push @missing, $required_method_name; |
1c9db35c |
53 | } |
1c9db35c |
54 | } |
d939e016 |
55 | |
f3e76c8f |
56 | return unless @missing; |
d939e016 |
57 | |
58 | my $error = ''; |
59 | |
60 | if (@missing) { |
61 | my $noun = @missing == 1 ? 'method' : 'methods'; |
62 | |
63 | my $list |
64 | = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing ); |
65 | |
66 | $error |
67 | .= q{'} |
68 | . $role->name |
69 | . "' requires the $noun $list " |
70 | . "to be implemented by '" |
71 | . $class->name . q{'}; |
72 | } |
73 | |
d939e016 |
74 | $class->throw_error($error); |
1c9db35c |
75 | } |
76 | |
709c321c |
77 | sub check_required_attributes { |
78 | |
79 | } |
80 | |
1c9db35c |
81 | sub apply_attributes { |
82 | my ($self, $role, $class) = @_; |
83 | foreach my $attribute_name ($role->get_attribute_list) { |
84 | # it if it has one already |
85 | if ($class->has_attribute($attribute_name) && |
86 | # make sure we haven't seen this one already too |
87 | $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) { |
88 | next; |
89 | } |
90 | else { |
d7d8a8c7 |
91 | $class->add_attribute( |
92 | $attribute_name, |
93 | $role->get_attribute($attribute_name) |
94 | ); |
1c9db35c |
95 | } |
96 | } |
97 | } |
98 | |
99 | sub apply_methods { |
100 | my ($self, $role, $class) = @_; |
d443cad0 |
101 | my @implicitly_overridden; |
102 | |
1c9db35c |
103 | foreach my $method_name ($role->get_method_list) { |
f5b6d42e |
104 | unless ($self->is_method_excluded($method_name)) { |
105 | # it if it has one already |
106 | if ($class->has_method($method_name) && |
107 | # and if they are not the same thing ... |
108 | $class->get_method($method_name)->body != $role->get_method($method_name)->body) { |
d443cad0 |
109 | push @implicitly_overridden, $method_name; |
f5b6d42e |
110 | next; |
111 | } |
112 | else { |
6549b0d1 |
113 | # add it, although it could be overridden |
f5b6d42e |
114 | $class->add_method( |
115 | $method_name, |
116 | $role->get_method($method_name) |
117 | ); |
118 | } |
1c9db35c |
119 | } |
43a41ede |
120 | |
121 | if ($self->is_method_aliased($method_name)) { |
122 | my $aliased_method_name = $self->get_method_aliases->{$method_name}; |
123 | # it if it has one already |
124 | if ($class->has_method($aliased_method_name) && |
125 | # and if they are not the same thing ... |
126 | $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) { |
4c0b3599 |
127 | $class->throw_error("Cannot create a method alias if a local method of the same name exists"); |
43a41ede |
128 | } |
87e63626 |
129 | $class->add_method( |
43a41ede |
130 | $aliased_method_name, |
131 | $role->get_method($method_name) |
132 | ); |
133 | } |
1c9db35c |
134 | } |
d443cad0 |
135 | |
136 | if (@implicitly_overridden) { |
137 | my $plural = @implicitly_overridden > 1 ? "s" : ""; |
138 | # we use \n because we have no hope of guessing the right stack frame, |
139 | # it's almost certainly never going to be the one above us |
140 | warn "The " . $class->name . " class has implicitly overridden the method$plural (" . join(', ', @implicitly_overridden) . ") from role " . $role->name . ". If this is intentional, please exclude the method$plural from composition to silence this warning (see Moose::Cookbook::Roles::Recipe2)\n"; |
141 | } |
142 | |
1c9db35c |
143 | # we must reset the cache here since |
144 | # we are just aliasing methods, otherwise |
145 | # the modifiers go wonky. |
146 | $class->reset_package_cache_flag; |
147 | } |
148 | |
149 | sub apply_override_method_modifiers { |
150 | my ($self, $role, $class) = @_; |
151 | foreach my $method_name ($role->get_method_modifier_list('override')) { |
152 | # it if it has one already then ... |
153 | if ($class->has_method($method_name)) { |
154 | next; |
155 | } |
156 | else { |
157 | # if this is not a role, then we need to |
158 | # find the original package of the method |
159 | # so that we can tell the class were to |
160 | # find the right super() method |
161 | my $method = $role->get_override_method_modifier($method_name); |
162 | my ($package) = Class::MOP::get_code_info($method); |
163 | # if it is a class, we just add it |
164 | $class->add_override_method_modifier($method_name, $method, $package); |
165 | } |
166 | } |
167 | } |
168 | |
169 | sub apply_method_modifiers { |
170 | my ($self, $modifier_type, $role, $class) = @_; |
171 | my $add = "add_${modifier_type}_method_modifier"; |
172 | my $get = "get_${modifier_type}_method_modifiers"; |
173 | foreach my $method_name ($role->get_method_modifier_list($modifier_type)) { |
174 | $class->$add( |
175 | $method_name, |
176 | $_ |
177 | ) foreach $role->$get($method_name); |
178 | } |
179 | } |
180 | |
fb1e11d5 |
181 | 1; |
182 | |
183 | __END__ |
184 | |
185 | =pod |
186 | |
187 | =head1 NAME |
188 | |
ab76842e |
189 | Moose::Meta::Role::Application::ToClass - Compose a role into a class |
fb1e11d5 |
190 | |
191 | =head1 DESCRIPTION |
192 | |
193 | =head2 METHODS |
194 | |
195 | =over 4 |
196 | |
197 | =item B<new> |
198 | |
199 | =item B<meta> |
200 | |
1c9db35c |
201 | =item B<apply> |
202 | |
709c321c |
203 | =item B<check_role_exclusions> |
204 | |
1c9db35c |
205 | =item B<check_required_methods> |
206 | |
709c321c |
207 | =item B<check_required_attributes> |
1c9db35c |
208 | |
209 | =item B<apply_attributes> |
210 | |
211 | =item B<apply_methods> |
212 | |
213 | =item B<apply_method_modifiers> |
214 | |
1c9db35c |
215 | =item B<apply_override_method_modifiers> |
216 | |
fb1e11d5 |
217 | =back |
218 | |
219 | =head1 BUGS |
220 | |
221 | All complex software has bugs lurking in it, and this module is no |
222 | exception. If you find a bug please either email me, or add the bug |
223 | to cpan-RT. |
224 | |
225 | =head1 AUTHOR |
226 | |
227 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
228 | |
229 | =head1 COPYRIGHT AND LICENSE |
230 | |
2840a3b2 |
231 | Copyright 2006-2009 by Infinity Interactive, Inc. |
fb1e11d5 |
232 | |
233 | L<http://www.iinteractive.com> |
234 | |
235 | This library is free software; you can redistribute it and/or modify |
236 | it under the same terms as Perl itself. |
237 | |
238 | =cut |
239 | |