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'; |
154a4aae |
8 | use Scalar::Util 'weaken', 'blessed'; |
fb1e11d5 |
9 | |
d874ffad |
10 | our $VERSION = '1.20'; |
e606ae5f |
11 | $VERSION = eval $VERSION; |
fb1e11d5 |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
13 | |
14 | use base 'Moose::Meta::Role::Application'; |
15 | |
ff9c4953 |
16 | __PACKAGE__->meta->add_attribute('role' => ( |
17 | reader => 'role', |
ff9c4953 |
18 | )); |
19 | |
20 | __PACKAGE__->meta->add_attribute('class' => ( |
21 | reader => 'class', |
ff9c4953 |
22 | )); |
23 | |
1c9db35c |
24 | sub apply { |
d03bd989 |
25 | my ($self, $role, $class) = @_; |
ff9c4953 |
26 | |
154a4aae |
27 | # We need weak_ref in CMOP :( |
28 | weaken($self->{role} = $role); |
29 | weaken($self->{class} = $class); |
ff9c4953 |
30 | |
1c9db35c |
31 | $self->SUPER::apply($role, $class); |
a9b63d79 |
32 | |
d03bd989 |
33 | $class->add_role($role); |
a9b63d79 |
34 | $class->add_role_application($self); |
1c9db35c |
35 | } |
36 | |
37 | sub check_role_exclusions { |
38 | my ($self, $role, $class) = @_; |
39 | if ($class->excludes_role($role->name)) { |
4c0b3599 |
40 | $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'"); |
1c9db35c |
41 | } |
42 | foreach my $excluded_role_name ($role->get_excluded_roles_list) { |
43 | if ($class->does_role($excluded_role_name)) { |
4c0b3599 |
44 | $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'"); |
1c9db35c |
45 | } |
46 | } |
47 | } |
48 | |
49 | sub check_required_methods { |
50 | my ($self, $role, $class) = @_; |
d939e016 |
51 | |
52 | my @missing; |
53 | my @is_attr; |
54 | |
1c9db35c |
55 | # NOTE: |
56 | # we might need to move this down below the |
57 | # the attributes so that we can require any |
58 | # attribute accessors. However I am thinking |
59 | # that maybe those are somehow exempt from |
60 | # the require methods stuff. |
92b7f727 |
61 | foreach my $required_method ($role->get_required_method_list) { |
62 | my $required_method_name = $required_method->name; |
1c9db35c |
63 | |
3e19778d |
64 | if (!$class->find_method_by_name($required_method_name)) { |
d03bd989 |
65 | |
3e19778d |
66 | next if $self->is_aliased_method($required_method_name); |
d939e016 |
67 | |
92b7f727 |
68 | push @missing, $required_method; |
1c9db35c |
69 | } |
1c9db35c |
70 | } |
d939e016 |
71 | |
f3e76c8f |
72 | return unless @missing; |
d939e016 |
73 | |
74 | my $error = ''; |
75 | |
bb365545 |
76 | @missing = sort { $a->name cmp $b->name } @missing; |
16d7dc8d |
77 | my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing; |
884837f3 |
78 | |
79 | if (@conflicts) { |
80 | my $conflict = $conflicts[0]; |
e893345e |
81 | my $roles = $conflict->roles_as_english_list; |
82 | |
83 | my @same_role_conflicts = grep { $_->roles_as_english_list eq $roles } @conflicts; |
84 | |
85 | if (@same_role_conflicts == 1) { |
86 | $error |
87 | .= "Due to a method name conflict in roles " |
88 | . $roles |
89 | . ", the method '" |
90 | . $conflict->name |
91 | . "' must be implemented or excluded by '" |
92 | . $class->name |
93 | . q{'}; |
94 | } |
95 | else { |
96 | my $methods |
97 | = Moose::Util::english_list( map { q{'} . $_->name . q{'} } @same_role_conflicts ); |
98 | |
99 | $error |
100 | .= "Due to method name conflicts in roles " |
101 | . $roles |
102 | . ", the methods " |
103 | . $methods |
104 | . " must be implemented or excluded by '" |
105 | . $class->name |
106 | . q{'}; |
107 | } |
884837f3 |
108 | } |
109 | elsif (@missing) { |
d939e016 |
110 | my $noun = @missing == 1 ? 'method' : 'methods'; |
111 | |
112 | my $list |
113 | = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing ); |
114 | |
115 | $error |
116 | .= q{'} |
117 | . $role->name |
118 | . "' requires the $noun $list " |
119 | . "to be implemented by '" |
120 | . $class->name . q{'}; |
121 | } |
122 | |
d939e016 |
123 | $class->throw_error($error); |
1c9db35c |
124 | } |
125 | |
709c321c |
126 | sub check_required_attributes { |
d03bd989 |
127 | |
709c321c |
128 | } |
129 | |
1c9db35c |
130 | sub apply_attributes { |
131 | my ($self, $role, $class) = @_; |
f785aad8 |
132 | my $attr_metaclass = $class->attribute_metaclass; |
133 | |
1c9db35c |
134 | foreach my $attribute_name ($role->get_attribute_list) { |
135 | # it if it has one already |
136 | if ($class->has_attribute($attribute_name) && |
137 | # make sure we haven't seen this one already too |
138 | $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) { |
139 | next; |
140 | } |
141 | else { |
d7d8a8c7 |
142 | $class->add_attribute( |
f785aad8 |
143 | $role->get_attribute($attribute_name)->attribute_for_class($attr_metaclass) |
d7d8a8c7 |
144 | ); |
1c9db35c |
145 | } |
146 | } |
147 | } |
148 | |
149 | sub apply_methods { |
4a9e8ff6 |
150 | my ( $self, $role, $class ) = @_; |
151 | |
152 | foreach my $method ( $role->_get_local_methods ) { |
153 | my $method_name = $method->name; |
154 | |
ba7d613d |
155 | next if $method->isa('Class::MOP::Method::Meta'); |
d03bd989 |
156 | |
4a9e8ff6 |
157 | unless ( $self->is_method_excluded($method_name) ) { |
158 | |
159 | my $class_method = $class->get_method($method_name); |
160 | |
161 | next if $class_method && $class_method->body != $method->body; |
d03bd989 |
162 | |
87e63626 |
163 | $class->add_method( |
4a9e8ff6 |
164 | $method_name, |
165 | $method, |
166 | ); |
167 | } |
168 | |
169 | next unless $self->is_method_aliased($method_name); |
170 | |
171 | my $aliased_method_name = $self->get_method_aliases->{$method_name}; |
172 | |
173 | my $class_method = $class->get_method($aliased_method_name); |
174 | |
175 | if ( $class_method && $class_method->body != $method->body ) { |
176 | $class->throw_error( |
177 | "Cannot create a method alias if a local method of the same name exists" |
d03bd989 |
178 | ); |
179 | } |
4a9e8ff6 |
180 | |
181 | $class->add_method( |
182 | $aliased_method_name, |
183 | $method, |
184 | ); |
1c9db35c |
185 | } |
4a9e8ff6 |
186 | |
1c9db35c |
187 | # we must reset the cache here since |
188 | # we are just aliasing methods, otherwise |
189 | # the modifiers go wonky. |
d03bd989 |
190 | $class->reset_package_cache_flag; |
1c9db35c |
191 | } |
192 | |
193 | sub apply_override_method_modifiers { |
194 | my ($self, $role, $class) = @_; |
195 | foreach my $method_name ($role->get_method_modifier_list('override')) { |
196 | # it if it has one already then ... |
197 | if ($class->has_method($method_name)) { |
198 | next; |
199 | } |
200 | else { |
201 | # if this is not a role, then we need to |
202 | # find the original package of the method |
203 | # so that we can tell the class were to |
204 | # find the right super() method |
205 | my $method = $role->get_override_method_modifier($method_name); |
206 | my ($package) = Class::MOP::get_code_info($method); |
207 | # if it is a class, we just add it |
208 | $class->add_override_method_modifier($method_name, $method, $package); |
209 | } |
210 | } |
211 | } |
212 | |
213 | sub apply_method_modifiers { |
214 | my ($self, $modifier_type, $role, $class) = @_; |
215 | my $add = "add_${modifier_type}_method_modifier"; |
216 | my $get = "get_${modifier_type}_method_modifiers"; |
217 | foreach my $method_name ($role->get_method_modifier_list($modifier_type)) { |
218 | $class->$add( |
219 | $method_name, |
220 | $_ |
221 | ) foreach $role->$get($method_name); |
222 | } |
223 | } |
224 | |
fb1e11d5 |
225 | 1; |
226 | |
227 | __END__ |
228 | |
229 | =pod |
230 | |
231 | =head1 NAME |
232 | |
ab76842e |
233 | Moose::Meta::Role::Application::ToClass - Compose a role into a class |
fb1e11d5 |
234 | |
235 | =head1 DESCRIPTION |
236 | |
237 | =head2 METHODS |
238 | |
239 | =over 4 |
240 | |
241 | =item B<new> |
242 | |
243 | =item B<meta> |
244 | |
1c9db35c |
245 | =item B<apply> |
246 | |
709c321c |
247 | =item B<check_role_exclusions> |
248 | |
1c9db35c |
249 | =item B<check_required_methods> |
250 | |
709c321c |
251 | =item B<check_required_attributes> |
1c9db35c |
252 | |
253 | =item B<apply_attributes> |
254 | |
255 | =item B<apply_methods> |
256 | |
257 | =item B<apply_method_modifiers> |
258 | |
1c9db35c |
259 | =item B<apply_override_method_modifiers> |
260 | |
fb1e11d5 |
261 | =back |
262 | |
263 | =head1 BUGS |
264 | |
d4048ef3 |
265 | See L<Moose/BUGS> for details on reporting bugs. |
fb1e11d5 |
266 | |
267 | =head1 AUTHOR |
268 | |
269 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
270 | |
271 | =head1 COPYRIGHT AND LICENSE |
272 | |
7e0492d3 |
273 | Copyright 2006-2010 by Infinity Interactive, Inc. |
fb1e11d5 |
274 | |
275 | L<http://www.iinteractive.com> |
276 | |
277 | This library is free software; you can redistribute it and/or modify |
278 | it under the same terms as Perl itself. |
279 | |
280 | =cut |
281 | |