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