Commit | Line | Data |
c0e30cf5 |
1 | |
2 | package Moose::Meta::Class; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
0addec44 |
7 | use Class::MOP; |
648e79ae |
8 | |
11c86f15 |
9 | use Carp (); |
2e7f6cf4 |
10 | use Data::OptList; |
f8b6827f |
11 | use List::Util qw( first ); |
349cda54 |
12 | use List::MoreUtils qw( any all uniq first_index ); |
21f1e231 |
13 | use Scalar::Util 'weaken', 'blessed'; |
a15dff8d |
14 | |
19a330e5 |
15 | our $VERSION = '1.03'; |
e606ae5f |
16 | $VERSION = eval $VERSION; |
d44714be |
17 | our $AUTHORITY = 'cpan:STEVAN'; |
bc1e29b5 |
18 | |
74862722 |
19 | use Moose::Meta::Method::Overridden; |
3f9e4b0a |
20 | use Moose::Meta::Method::Augmented; |
77f14411 |
21 | use Moose::Error::Default; |
0fa70d03 |
22 | use Moose::Meta::Class::Immutable::Trait; |
23 | use Moose::Meta::Method::Constructor; |
24 | use Moose::Meta::Method::Destructor; |
8ee73eeb |
25 | |
c0e30cf5 |
26 | use base 'Class::MOP::Class'; |
27 | |
598340d5 |
28 | __PACKAGE__->meta->add_attribute('roles' => ( |
ef333f17 |
29 | reader => 'roles', |
30 | default => sub { [] } |
31 | )); |
32 | |
a9b63d79 |
33 | __PACKAGE__->meta->add_attribute('role_applications' => ( |
639f9a1a |
34 | reader => '_get_role_applications', |
a9b63d79 |
35 | default => sub { [] } |
36 | )); |
37 | |
0fa70d03 |
38 | __PACKAGE__->meta->add_attribute( |
39 | Class::MOP::Attribute->new('immutable_trait' => ( |
40 | accessor => "immutable_trait", |
41 | default => 'Moose::Meta::Class::Immutable::Trait', |
42 | )) |
43 | ); |
44 | |
e606ae5f |
45 | __PACKAGE__->meta->add_attribute('constructor_class' => ( |
46 | accessor => 'constructor_class', |
e0001338 |
47 | default => 'Moose::Meta::Method::Constructor', |
e606ae5f |
48 | )); |
49 | |
50 | __PACKAGE__->meta->add_attribute('destructor_class' => ( |
51 | accessor => 'destructor_class', |
e0001338 |
52 | default => 'Moose::Meta::Method::Destructor', |
e606ae5f |
53 | )); |
54 | |
11c86f15 |
55 | __PACKAGE__->meta->add_attribute('error_class' => ( |
bf6fa6b3 |
56 | accessor => 'error_class', |
57 | default => 'Moose::Error::Default', |
11c86f15 |
58 | )); |
59 | |
590868a3 |
60 | sub initialize { |
61 | my $class = shift; |
62 | my $pkg = shift; |
d03bd989 |
63 | return Class::MOP::get_metaclass_by_name($pkg) |
685f7e44 |
64 | || $class->SUPER::initialize($pkg, |
65 | 'attribute_metaclass' => 'Moose::Meta::Attribute', |
66 | 'method_metaclass' => 'Moose::Meta::Method', |
67 | 'instance_metaclass' => 'Moose::Meta::Instance', |
68 | @_ |
d03bd989 |
69 | ); |
ac2dc464 |
70 | } |
590868a3 |
71 | |
0fa70d03 |
72 | sub _immutable_options { |
73 | my ( $self, @args ) = @_; |
74 | |
75 | $self->SUPER::_immutable_options( |
76 | inline_destructor => 1, |
77 | |
78 | # Moose always does this when an attribute is created |
79 | inline_accessors => 0, |
80 | |
81 | @args, |
82 | ); |
83 | } |
84 | |
61bdd94f |
85 | sub create { |
7d4035ae |
86 | my ($class, $package_name, %options) = @_; |
d03bd989 |
87 | |
61bdd94f |
88 | (ref $options{roles} eq 'ARRAY') |
7d4035ae |
89 | || $class->throw_error("You must pass an ARRAY ref of roles", data => $options{roles}) |
61bdd94f |
90 | if exists $options{roles}; |
310ba883 |
91 | my $roles = delete $options{roles}; |
dd37a5be |
92 | |
7d4035ae |
93 | my $new_meta = $class->SUPER::create($package_name, %options); |
dd37a5be |
94 | |
310ba883 |
95 | if ($roles) { |
7d4035ae |
96 | Moose::Util::apply_all_roles( $new_meta, @$roles ); |
61bdd94f |
97 | } |
d03bd989 |
98 | |
7d4035ae |
99 | return $new_meta; |
61bdd94f |
100 | } |
101 | |
6a4a7c31 |
102 | sub _check_metaclass_compatibility { |
2b72f3b4 |
103 | my $self = shift; |
104 | |
105 | if ( my @supers = $self->superclasses ) { |
106 | $self->_fix_metaclass_incompatibility(@supers); |
107 | } |
108 | |
6a4a7c31 |
109 | $self->SUPER::_check_metaclass_compatibility(@_); |
2b72f3b4 |
110 | } |
111 | |
17594769 |
112 | my %ANON_CLASSES; |
113 | |
114 | sub create_anon_class { |
115 | my ($self, %options) = @_; |
116 | |
117 | my $cache_ok = delete $options{cache}; |
d03bd989 |
118 | |
cf600c83 |
119 | my $cache_key |
120 | = _anon_cache_key( $options{superclasses}, $options{roles} ); |
d03bd989 |
121 | |
6d5cbd2b |
122 | if ($cache_ok && defined $ANON_CLASSES{$cache_key}) { |
17594769 |
123 | return $ANON_CLASSES{$cache_key}; |
124 | } |
d03bd989 |
125 | |
17594769 |
126 | my $new_class = $self->SUPER::create_anon_class(%options); |
127 | |
6d5cbd2b |
128 | $ANON_CLASSES{$cache_key} = $new_class |
129 | if $cache_ok; |
17594769 |
130 | |
131 | return $new_class; |
132 | } |
133 | |
cf600c83 |
134 | sub _anon_cache_key { |
135 | # Makes something like Super::Class|Super::Class::2=Role|Role::1 |
136 | return join '=' => ( |
137 | join( '|', @{ $_[0] || [] } ), |
138 | join( '|', sort @{ $_[1] || [] } ), |
139 | ); |
140 | } |
141 | |
142 | sub reinitialize { |
143 | my $self = shift; |
144 | my $pkg = shift; |
145 | |
146 | my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg); |
147 | |
148 | my $cache_key; |
149 | |
150 | my %existing_classes; |
151 | if ($meta) { |
152 | %existing_classes = map { $_ => $meta->$_() } qw( |
153 | attribute_metaclass |
154 | method_metaclass |
155 | wrapped_method_metaclass |
156 | instance_metaclass |
157 | constructor_class |
158 | destructor_class |
159 | error_class |
160 | ); |
161 | |
162 | $cache_key = _anon_cache_key( |
163 | [ $meta->superclasses ], |
164 | [ map { $_->name } @{ $meta->roles } ], |
165 | ) if $meta->is_anon_class; |
166 | } |
167 | |
168 | my $new_meta = $self->SUPER::reinitialize( |
169 | $pkg, |
170 | %existing_classes, |
171 | @_, |
172 | ); |
173 | |
174 | return $new_meta unless defined $cache_key; |
175 | |
176 | my $new_cache_key = _anon_cache_key( |
177 | [ $meta->superclasses ], |
178 | [ map { $_->name } @{ $meta->roles } ], |
179 | ); |
180 | |
181 | delete $ANON_CLASSES{$cache_key}; |
182 | $ANON_CLASSES{$new_cache_key} = $new_meta; |
183 | |
184 | return $new_meta; |
185 | } |
186 | |
ef333f17 |
187 | sub add_role { |
188 | my ($self, $role) = @_; |
189 | (blessed($role) && $role->isa('Moose::Meta::Role')) |
11c86f15 |
190 | || $self->throw_error("Roles must be instances of Moose::Meta::Role", data => $role); |
ef333f17 |
191 | push @{$self->roles} => $role; |
192 | } |
193 | |
639f9a1a |
194 | sub role_applications { |
195 | my ($self) = @_; |
196 | |
197 | return @{$self->_get_role_applications}; |
198 | } |
199 | |
a9b63d79 |
200 | sub add_role_application { |
201 | my ($self, $application) = @_; |
202 | (blessed($application) && $application->isa('Moose::Meta::Role::Application::ToClass')) |
203 | || $self->throw_error("Role applications must be instances of Moose::Meta::Role::Application::ToClass", data => $application); |
639f9a1a |
204 | push @{$self->_get_role_applications} => $application; |
a9b63d79 |
205 | } |
206 | |
b8aeb4dc |
207 | sub calculate_all_roles { |
208 | my $self = shift; |
209 | my %seen; |
210 | grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles }; |
211 | } |
212 | |
ef333f17 |
213 | sub does_role { |
214 | my ($self, $role_name) = @_; |
322abb07 |
215 | |
ef333f17 |
216 | (defined $role_name) |
11c86f15 |
217 | || $self->throw_error("You must supply a role name to look for"); |
322abb07 |
218 | |
9c429218 |
219 | foreach my $class ($self->class_precedence_list) { |
322abb07 |
220 | my $meta = Class::MOP::class_of($class); |
3d0f5a27 |
221 | # when a Moose metaclass is itself extended with a role, |
222 | # this check needs to be done since some items in the |
223 | # class_precedence_list might in fact be Class::MOP |
224 | # based still. |
322abb07 |
225 | next unless $meta && $meta->can('roles'); |
226 | foreach my $role (@{$meta->roles}) { |
9c429218 |
227 | return 1 if $role->does_role($role_name); |
228 | } |
ef333f17 |
229 | } |
230 | return 0; |
231 | } |
232 | |
d79e62fd |
233 | sub excludes_role { |
234 | my ($self, $role_name) = @_; |
ebfc4d0f |
235 | |
d79e62fd |
236 | (defined $role_name) |
11c86f15 |
237 | || $self->throw_error("You must supply a role name to look for"); |
ebfc4d0f |
238 | |
ac2dc464 |
239 | foreach my $class ($self->class_precedence_list) { |
ebfc4d0f |
240 | my $meta = Class::MOP::class_of($class); |
241 | # when a Moose metaclass is itself extended with a role, |
242 | # this check needs to be done since some items in the |
243 | # class_precedence_list might in fact be Class::MOP |
244 | # based still. |
245 | next unless $meta && $meta->can('roles'); |
246 | foreach my $role (@{$meta->roles}) { |
9c429218 |
247 | return 1 if $role->excludes_role($role_name); |
248 | } |
d79e62fd |
249 | } |
250 | return 0; |
251 | } |
252 | |
8c9d74e7 |
253 | sub new_object { |
7d4035ae |
254 | my $self = shift; |
e606ae5f |
255 | my $params = @_ == 1 ? $_[0] : {@_}; |
7d4035ae |
256 | my $object = $self->SUPER::new_object($params); |
1308deb4 |
257 | |
7d4035ae |
258 | foreach my $attr ( $self->get_all_attributes() ) { |
1308deb4 |
259 | |
260 | next unless $attr->can('has_trigger') && $attr->has_trigger; |
261 | |
262 | my $init_arg = $attr->init_arg; |
263 | |
264 | next unless defined $init_arg; |
265 | |
266 | next unless exists $params->{$init_arg}; |
267 | |
268 | $attr->trigger->( |
7d4035ae |
269 | $object, |
1308deb4 |
270 | ( |
271 | $attr->should_coerce |
7d4035ae |
272 | ? $attr->get_read_method_ref->($object) |
1308deb4 |
273 | : $params->{$init_arg} |
274 | ), |
1308deb4 |
275 | ); |
8c9d74e7 |
276 | } |
1308deb4 |
277 | |
7d4035ae |
278 | $object->BUILDALL($params) if $object->can('BUILDALL'); |
a19ae3d7 |
279 | |
7d4035ae |
280 | return $object; |
8c9d74e7 |
281 | } |
282 | |
e2eef3a5 |
283 | sub superclasses { |
284 | my $self = shift; |
2e7f6cf4 |
285 | my $supers = Data::OptList::mkopt(\@_); |
286 | foreach my $super (@{ $supers }) { |
287 | my ($name, $opts) = @{ $super }; |
288 | Class::MOP::load_class($name, $opts); |
289 | my $meta = Class::MOP::class_of($name); |
290 | $self->throw_error("You cannot inherit from a Moose Role ($name)") |
e2eef3a5 |
291 | if $meta && $meta->isa('Moose::Meta::Role') |
292 | } |
2e7f6cf4 |
293 | return $self->SUPER::superclasses(map { $_->[0] } @{ $supers }); |
e2eef3a5 |
294 | } |
295 | |
093b12c2 |
296 | ### --------------------------------------------- |
297 | |
a2eec5e7 |
298 | sub add_attribute { |
299 | my $self = shift; |
28af3424 |
300 | my $attr = |
e472c9a5 |
301 | (blessed $_[0] && $_[0]->isa('Class::MOP::Attribute') |
d03bd989 |
302 | ? $_[0] |
28af3424 |
303 | : $self->_process_attribute(@_)); |
304 | $self->SUPER::add_attribute($attr); |
305 | # it may be a Class::MOP::Attribute, theoretically, which doesn't have |
306 | # 'bare' and doesn't implement this method |
9340e346 |
307 | if ($attr->can('_check_associated_methods')) { |
308 | $attr->_check_associated_methods; |
28af3424 |
309 | } |
310 | return $attr; |
a2eec5e7 |
311 | } |
312 | |
78cd1d3b |
313 | sub add_override_method_modifier { |
314 | my ($self, $name, $method, $_super_package) = @_; |
18c2ec0e |
315 | |
d05cd563 |
316 | (!$self->has_method($name)) |
11c86f15 |
317 | || $self->throw_error("Cannot add an override method if a local method is already present"); |
18c2ec0e |
318 | |
74862722 |
319 | $self->add_method($name => Moose::Meta::Method::Overridden->new( |
3f9e4b0a |
320 | method => $method, |
321 | class => $self, |
322 | package => $_super_package, # need this for roles |
323 | name => $name, |
18c2ec0e |
324 | )); |
78cd1d3b |
325 | } |
326 | |
327 | sub add_augment_method_modifier { |
ac2dc464 |
328 | my ($self, $name, $method) = @_; |
d05cd563 |
329 | (!$self->has_method($name)) |
11c86f15 |
330 | || $self->throw_error("Cannot add an augment method if a local method is already present"); |
3f9e4b0a |
331 | |
332 | $self->add_method($name => Moose::Meta::Method::Augmented->new( |
333 | method => $method, |
334 | class => $self, |
335 | name => $name, |
336 | )); |
78cd1d3b |
337 | } |
338 | |
1341f10c |
339 | ## Private Utility methods ... |
340 | |
05d9eaf6 |
341 | sub _find_next_method_by_name_which_is_not_overridden { |
342 | my ($self, $name) = @_; |
68efb014 |
343 | foreach my $method ($self->find_all_methods_by_name($name)) { |
ac2dc464 |
344 | return $method->{code} |
74862722 |
345 | if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overridden'); |
05d9eaf6 |
346 | } |
347 | return undef; |
348 | } |
349 | |
41419b9e |
350 | sub _fix_metaclass_incompatibility { |
1341f10c |
351 | my ($self, @superclasses) = @_; |
e606ae5f |
352 | |
57a21623 |
353 | $self->_fix_one_incompatible_metaclass($_) |
354 | for map { Moose::Meta::Class->initialize($_) } @superclasses; |
349cda54 |
355 | } |
356 | |
357 | sub _fix_one_incompatible_metaclass { |
358 | my ($self, $meta) = @_; |
e606ae5f |
359 | |
349cda54 |
360 | return if $self->_superclass_meta_is_compatible($meta); |
361 | |
362 | unless ( $self->is_pristine ) { |
363 | $self->throw_error( |
364 | "Cannot attempt to reinitialize metaclass for " |
365 | . $self->name |
366 | . ", it isn't pristine" ); |
f8b6827f |
367 | } |
cb311121 |
368 | |
349cda54 |
369 | $self->_reconcile_with_superclass_meta($meta); |
f8b6827f |
370 | } |
371 | |
372 | sub _superclass_meta_is_compatible { |
349cda54 |
373 | my ($self, $super_meta) = @_; |
f8b6827f |
374 | |
375 | next unless $super_meta->isa("Class::MOP::Class"); |
376 | |
377 | my $super_meta_name |
378 | = $super_meta->is_immutable |
989263ad |
379 | ? $super_meta->_get_mutable_metaclass_name |
f8b6827f |
380 | : ref($super_meta); |
381 | |
382 | return 1 |
383 | if $self->isa($super_meta_name) |
384 | and |
385 | $self->instance_metaclass->isa( $super_meta->instance_metaclass ); |
386 | } |
387 | |
388 | # I don't want to have to type this >1 time |
389 | my @MetaClassTypes = |
3e334262 |
390 | qw( attribute_metaclass |
391 | method_metaclass |
392 | wrapped_method_metaclass |
393 | instance_metaclass |
394 | constructor_class |
395 | destructor_class |
396 | error_class ); |
f8b6827f |
397 | |
398 | sub _reconcile_with_superclass_meta { |
349cda54 |
399 | my ($self, $super_meta) = @_; |
f8b6827f |
400 | |
dd37a5be |
401 | my $super_meta_name |
f8b6827f |
402 | = $super_meta->is_immutable |
989263ad |
403 | ? $super_meta->_get_mutable_metaclass_name |
f8b6827f |
404 | : ref($super_meta); |
e606ae5f |
405 | |
f8b6827f |
406 | my $self_metaclass = ref $self; |
407 | |
408 | # If neither of these is true we have a more serious |
409 | # incompatibility that we just cannot fix (yet?). |
dd37a5be |
410 | if ( $super_meta_name->isa( ref $self ) |
f8b6827f |
411 | && all { $super_meta->$_->isa( $self->$_ ) } @MetaClassTypes ) { |
0635500e |
412 | $self->_reinitialize_with($super_meta); |
f8b6827f |
413 | } |
414 | elsif ( $self->_all_metaclasses_differ_by_roles_only($super_meta) ) { |
0635500e |
415 | $self->_reconcile_role_differences($super_meta); |
1341f10c |
416 | } |
1341f10c |
417 | } |
418 | |
f8b6827f |
419 | sub _reinitialize_with { |
420 | my ( $self, $new_meta ) = @_; |
421 | |
0635500e |
422 | my $new_self = $new_meta->reinitialize( |
f8b6827f |
423 | $self->name, |
424 | attribute_metaclass => $new_meta->attribute_metaclass, |
425 | method_metaclass => $new_meta->method_metaclass, |
426 | instance_metaclass => $new_meta->instance_metaclass, |
427 | ); |
428 | |
8b1d510f |
429 | $new_self->$_( $new_meta->$_ ) |
430 | for qw( constructor_class destructor_class error_class ); |
f8b6827f |
431 | |
0635500e |
432 | %$self = %$new_self; |
433 | |
434 | bless $self, ref $new_self; |
435 | |
4c5fcc12 |
436 | # We need to replace the cached metaclass instance or else when it |
437 | # goes out of scope Class::MOP::Class destroy's the namespace for |
438 | # the metaclass's class, causing much havoc. |
0635500e |
439 | Class::MOP::store_metaclass_by_name( $self->name, $self ); |
4c5fcc12 |
440 | Class::MOP::weaken_metaclass( $self->name ) if $self->is_anon_class; |
f8b6827f |
441 | } |
442 | |
443 | # In the more complex case, we share a common ancestor with our |
444 | # superclass's metaclass, but each metaclass (ours and the parent's) |
445 | # has a different set of roles applied. We reconcile this by first |
446 | # reinitializing into the parent class, and _then_ applying our own |
447 | # roles. |
448 | sub _all_metaclasses_differ_by_roles_only { |
449 | my ($self, $super_meta) = @_; |
450 | |
451 | for my $pair ( |
452 | [ ref $self, ref $super_meta ], |
453 | map { [ $self->$_, $super_meta->$_ ] } @MetaClassTypes |
454 | ) { |
455 | |
456 | next if $pair->[0] eq $pair->[1]; |
457 | |
458 | my $self_meta_meta = Class::MOP::Class->initialize( $pair->[0] ); |
459 | my $super_meta_meta = Class::MOP::Class->initialize( $pair->[1] ); |
460 | |
461 | my $common_ancestor |
462 | = _find_common_ancestor( $self_meta_meta, $super_meta_meta ); |
463 | |
464 | return unless $common_ancestor; |
465 | |
466 | return |
467 | unless _is_role_only_subclass_of( |
468 | $self_meta_meta, |
469 | $common_ancestor, |
470 | ) |
471 | && _is_role_only_subclass_of( |
472 | $super_meta_meta, |
473 | $common_ancestor, |
474 | ); |
475 | } |
476 | |
477 | return 1; |
478 | } |
479 | |
480 | # This, and some other functions, could be called as methods, but |
481 | # they're not for two reasons. One, we just end up ignoring the first |
482 | # argument, because we can't call these directly on one of the real |
483 | # arguments, because one of them could be a Class::MOP::Class object |
484 | # and not a Moose::Meta::Class. Second, only a completely insane |
485 | # person would attempt to subclass this stuff! |
486 | sub _find_common_ancestor { |
487 | my ($meta1, $meta2) = @_; |
488 | |
489 | # FIXME? This doesn't account for multiple inheritance (not sure |
490 | # if it needs to though). For example, is somewhere in $meta1's |
db9fda52 |
491 | # history it inherits from both ClassA and ClassB, and $meta2 |
f8b6827f |
492 | # inherits from ClassB & ClassA, does it matter? And what crazy |
493 | # fool would do that anyway? |
494 | |
495 | my %meta1_parents = map { $_ => 1 } $meta1->linearized_isa; |
496 | |
497 | return first { $meta1_parents{$_} } $meta2->linearized_isa; |
498 | } |
499 | |
500 | sub _is_role_only_subclass_of { |
501 | my ($meta, $ancestor) = @_; |
502 | |
503 | return 1 if $meta->name eq $ancestor; |
504 | |
505 | my @roles = _all_roles_until( $meta, $ancestor ); |
506 | |
507 | my %role_packages = map { $_->name => 1 } @roles; |
508 | |
509 | my $ancestor_meta = Class::MOP::Class->initialize($ancestor); |
510 | |
511 | my %shared_ancestors = map { $_ => 1 } $ancestor_meta->linearized_isa; |
512 | |
513 | for my $method ( $meta->get_all_methods() ) { |
514 | next if $method->name eq 'meta'; |
515 | next if $method->can('associated_attribute'); |
516 | |
517 | next |
518 | if $role_packages{ $method->original_package_name } |
519 | || $shared_ancestors{ $method->original_package_name }; |
520 | |
521 | return 0; |
522 | } |
523 | |
524 | # FIXME - this really isn't right. Just because an attribute is |
525 | # defined in a role doesn't mean it isn't _also_ defined in the |
526 | # subclass. |
527 | for my $attr ( $meta->get_all_attributes ) { |
528 | next if $shared_ancestors{ $attr->associated_class->name }; |
529 | |
530 | next if any { $_->has_attribute( $attr->name ) } @roles; |
531 | |
532 | return 0; |
533 | } |
534 | |
535 | return 1; |
536 | } |
537 | |
538 | sub _all_roles { |
539 | my $meta = shift; |
540 | |
541 | return _all_roles_until($meta); |
542 | } |
543 | |
544 | sub _all_roles_until { |
545 | my ($meta, $stop_at_class) = @_; |
546 | |
547 | return unless $meta->can('calculate_all_roles'); |
548 | |
549 | my @roles = $meta->calculate_all_roles; |
550 | |
551 | for my $class ( $meta->linearized_isa ) { |
552 | last if $stop_at_class && $stop_at_class eq $class; |
553 | |
554 | my $meta = Class::MOP::Class->initialize($class); |
555 | last unless $meta->can('calculate_all_roles'); |
556 | |
557 | push @roles, $meta->calculate_all_roles; |
558 | } |
559 | |
8b1d510f |
560 | return uniq @roles; |
f8b6827f |
561 | } |
562 | |
563 | sub _reconcile_role_differences { |
564 | my ($self, $super_meta) = @_; |
565 | |
3897e146 |
566 | my $self_meta = Class::MOP::class_of($self); |
f8b6827f |
567 | |
568 | my %roles; |
569 | |
570 | if ( my @roles = map { $_->name } _all_roles($self_meta) ) { |
571 | $roles{metaclass_roles} = \@roles; |
572 | } |
573 | |
574 | for my $thing (@MetaClassTypes) { |
575 | my $name = $self->$thing(); |
576 | |
577 | my $thing_meta = Class::MOP::Class->initialize($name); |
578 | |
579 | my @roles = map { $_->name } _all_roles($thing_meta) |
580 | or next; |
581 | |
582 | $roles{ $thing . '_roles' } = \@roles; |
583 | } |
584 | |
2b72f3b4 |
585 | $self->_reinitialize_with($super_meta); |
f8b6827f |
586 | |
587 | Moose::Util::MetaRole::apply_metaclass_roles( |
588 | for_class => $self->name, |
589 | %roles, |
590 | ); |
591 | |
592 | return $self; |
593 | } |
594 | |
1341f10c |
595 | sub _process_attribute { |
a3738e5b |
596 | my ( $self, $name, @args ) = @_; |
7e59b803 |
597 | |
598 | @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH'; |
d9bb6c63 |
599 | |
f9b5f5f8 |
600 | if (($name || '') =~ /^\+(.*)/) { |
7e59b803 |
601 | return $self->_process_inherited_attribute($1, @args); |
1341f10c |
602 | } |
603 | else { |
7e59b803 |
604 | return $self->_process_new_attribute($name, @args); |
605 | } |
606 | } |
607 | |
608 | sub _process_new_attribute { |
609 | my ( $self, $name, @args ) = @_; |
7e59b803 |
610 | |
d5c30e52 |
611 | $self->attribute_metaclass->interpolate_class_and_new($name, @args); |
1341f10c |
612 | } |
613 | |
614 | sub _process_inherited_attribute { |
615 | my ($self, $attr_name, %options) = @_; |
616 | my $inherited_attr = $self->find_attribute_by_name($attr_name); |
617 | (defined $inherited_attr) |
329c5dd4 |
618 | || $self->throw_error("Could not find an attribute by the name of '$attr_name' to inherit from in ${\$self->name}", data => $attr_name); |
1341f10c |
619 | if ($inherited_attr->isa('Moose::Meta::Attribute')) { |
d7d8a8c7 |
620 | return $inherited_attr->clone_and_inherit_options(%options); |
1341f10c |
621 | } |
622 | else { |
623 | # NOTE: |
624 | # kind of a kludge to handle Class::MOP::Attributes |
d7d8a8c7 |
625 | return $inherited_attr->Moose::Meta::Attribute::clone_and_inherit_options(%options); |
ac2dc464 |
626 | } |
1341f10c |
627 | } |
628 | |
5cf3dbcf |
629 | ## ------------------------------------------------- |
630 | |
bf6fa6b3 |
631 | our $error_level; |
11c86f15 |
632 | |
633 | sub throw_error { |
634 | my ( $self, @args ) = @_; |
bf6fa6b3 |
635 | local $error_level = ($error_level || 0) + 1; |
11c86f15 |
636 | $self->raise_error($self->create_error(@args)); |
637 | } |
638 | |
639 | sub raise_error { |
640 | my ( $self, @args ) = @_; |
641 | die @args; |
642 | } |
643 | |
644 | sub create_error { |
645 | my ( $self, @args ) = @_; |
646 | |
18748ad6 |
647 | require Carp::Heavy; |
648 | |
bf6fa6b3 |
649 | local $error_level = ($error_level || 0 ) + 1; |
18748ad6 |
650 | |
11c86f15 |
651 | if ( @args % 2 == 1 ) { |
652 | unshift @args, "message"; |
653 | } |
654 | |
fcab1742 |
655 | my %args = ( metaclass => $self, last_error => $@, @args ); |
11c86f15 |
656 | |
bf6fa6b3 |
657 | $args{depth} += $error_level; |
11c86f15 |
658 | |
bf6fa6b3 |
659 | my $class = ref $self ? $self->error_class : "Moose::Error::Default"; |
11c86f15 |
660 | |
a810a01f |
661 | Class::MOP::load_class($class); |
662 | |
11c86f15 |
663 | $class->new( |
bf6fa6b3 |
664 | Carp::caller_info($args{depth}), |
665 | %args |
11c86f15 |
666 | ); |
667 | } |
668 | |
c0e30cf5 |
669 | 1; |
670 | |
671 | __END__ |
672 | |
673 | =pod |
674 | |
675 | =head1 NAME |
676 | |
e522431d |
677 | Moose::Meta::Class - The Moose metaclass |
c0e30cf5 |
678 | |
c0e30cf5 |
679 | =head1 DESCRIPTION |
680 | |
70bb0f97 |
681 | This class is a subclass of L<Class::MOP::Class> that provides |
682 | additional Moose-specific functionality. |
e522431d |
683 | |
7854b409 |
684 | To really understand this class, you will need to start with the |
685 | L<Class::MOP::Class> documentation. This class can be understood as a |
686 | set of additional features on top of the basic feature provided by |
687 | that parent class. |
6ba6d68c |
688 | |
d4b1449e |
689 | =head1 INHERITANCE |
690 | |
691 | C<Moose::Meta::Class> is a subclass of L<Class::MOP::Class>. |
692 | |
c0e30cf5 |
693 | =head1 METHODS |
694 | |
695 | =over 4 |
696 | |
70bb0f97 |
697 | =item B<< Moose::Meta::Class->initialize($package_name, %options) >> |
590868a3 |
698 | |
70bb0f97 |
699 | This overrides the parent's method in order to provide its own |
700 | defaults for the C<attribute_metaclass>, C<instance_metaclass>, and |
701 | C<method_metaclass> options. |
61bdd94f |
702 | |
70bb0f97 |
703 | These all default to the appropriate Moose class. |
61bdd94f |
704 | |
70bb0f97 |
705 | =item B<< Moose::Meta::Class->create($package_name, %options) >> |
17594769 |
706 | |
70bb0f97 |
707 | This overrides the parent's method in order to accept a C<roles> |
9e25a72a |
708 | option. This should be an array reference containing roles |
709 | that the class does, each optionally followed by a hashref of options |
710 | (C<-excludes> and C<-alias>). |
17594769 |
711 | |
70bb0f97 |
712 | my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] ); |
17594769 |
713 | |
70bb0f97 |
714 | =item B<< Moose::Meta::Class->create_anon_class >> |
17594769 |
715 | |
70bb0f97 |
716 | This overrides the parent's method to accept a C<roles> option, just |
717 | as C<create> does. |
5cf3dbcf |
718 | |
70bb0f97 |
719 | It also accepts a C<cache> option. If this is true, then the anonymous |
720 | class will be cached based on its superclasses and roles. If an |
721 | existing anonymous class in the cache has the same superclasses and |
722 | roles, it will be reused. |
ac2dc464 |
723 | |
70bb0f97 |
724 | my $metaclass = Moose::Meta::Class->create_anon_class( |
725 | superclasses => ['Foo'], |
726 | roles => [qw/Some Roles Go Here/], |
727 | cache => 1, |
728 | ); |
ac2dc464 |
729 | |
2e7f6cf4 |
730 | Each entry in both the C<superclasses> and the C<roles> option can be |
b2d54db8 |
731 | followed by a hash reference with arguments. The C<superclasses> |
2e7f6cf4 |
732 | option can be supplied with a L<-version|Class::MOP/Class Loading |
733 | Options> option that ensures the loaded superclass satisfies the |
734 | required version. The C<role> option also takes the C<-version> as an |
735 | argument, but the option hash reference can also contain any other |
736 | role relevant values like exclusions or parameterized role arguments. |
737 | |
70bb0f97 |
738 | =item B<< $metaclass->make_immutable(%options) >> |
ac2dc464 |
739 | |
70bb0f97 |
740 | This overrides the parent's method to add a few options. Specifically, |
741 | it uses the Moose-specific constructor and destructor classes, and |
742 | enables inlining the destructor. |
8c9d74e7 |
743 | |
70bb0f97 |
744 | Also, since Moose always inlines attributes, it sets the |
745 | C<inline_accessors> option to false. |
02a0fb52 |
746 | |
70bb0f97 |
747 | =item B<< $metaclass->new_object(%params) >> |
a15dff8d |
748 | |
70bb0f97 |
749 | This overrides the parent's method in order to add support for |
750 | attribute triggers. |
6ba6d68c |
751 | |
2e7f6cf4 |
752 | =item B<< $metaclass->superclasses(@superclasses) >> |
753 | |
6b958a3e |
754 | This is the accessor allowing you to read or change the parents of |
2e7f6cf4 |
755 | the class. |
756 | |
757 | Each superclass can be followed by a hash reference containing a |
758 | L<-version|Class::MOP/Class Loading Options> value. If the version |
759 | requirement is not satisfied an error will be thrown. |
760 | |
70bb0f97 |
761 | =item B<< $metaclass->add_override_method_modifier($name, $sub) >> |
ef1d5f4b |
762 | |
70bb0f97 |
763 | This adds an C<override> method modifier to the package. |
e9ec68d6 |
764 | |
70bb0f97 |
765 | =item B<< $metaclass->add_augment_method_modifier($name, $sub) >> |
e9ec68d6 |
766 | |
70bb0f97 |
767 | This adds an C<augment> method modifier to the package. |
78cd1d3b |
768 | |
70bb0f97 |
769 | =item B<< $metaclass->calculate_all_roles >> |
02a0fb52 |
770 | |
70bb0f97 |
771 | This will return a unique array of C<Moose::Meta::Role> instances |
772 | which are attached to this class. |
78cd1d3b |
773 | |
70bb0f97 |
774 | =item B<< $metaclass->add_role($role) >> |
02a0fb52 |
775 | |
70bb0f97 |
776 | This takes a L<Moose::Meta::Role> object, and adds it to the class's |
777 | list of roles. This I<does not> actually apply the role to the class. |
2b14ac61 |
778 | |
b90dd4ef |
779 | =item B<< $metaclass->role_applications >> |
780 | |
639f9a1a |
781 | Returns a list of L<Moose::Meta::Role::Application::ToClass> |
b90dd4ef |
782 | objects, which contain the arguments to role application. |
783 | |
784 | =item B<< $metaclass->add_role_application($application) >> |
785 | |
786 | This takes a L<Moose::Meta::Role::Application::ToClass> object, and |
787 | adds it to the class's list of role applications. This I<does not> |
788 | actually apply any role to the class; it is only for tracking role |
789 | applications. |
790 | |
560c498d |
791 | =item B<< $metaclass->does_role($role) >> |
ef333f17 |
792 | |
560c498d |
793 | This returns a boolean indicating whether or not the class does the specified |
794 | role. The role provided can be either a role name or a L<Moose::Meta::Role> |
795 | object. This tests both the class and its parents. |
02a0fb52 |
796 | |
70bb0f97 |
797 | =item B<< $metaclass->excludes_role($role_name) >> |
ef333f17 |
798 | |
70bb0f97 |
799 | A class excludes a role if it has already composed a role which |
800 | excludes the named role. This tests both the class and its parents. |
02a0fb52 |
801 | |
70bb0f97 |
802 | =item B<< $metaclass->add_attribute($attr_name, %params|$params) >> |
ef333f17 |
803 | |
70bb0f97 |
804 | This overrides the parent's method in order to allow the parameters to |
805 | be provided as a hash reference. |
02a0fb52 |
806 | |
9f9fdd08 |
807 | =item B<< $metaclass->constructor_class($class_name) >> |
d79e62fd |
808 | |
9f9fdd08 |
809 | =item B<< $metaclass->destructor_class($class_name) >> |
e606ae5f |
810 | |
811 | These are the names of classes used when making a class |
812 | immutable. These default to L<Moose::Meta::Method::Constructor> and |
813 | L<Moose::Meta::Method::Destructor> respectively. These accessors are |
814 | read-write, so you can use them to change the class name. |
815 | |
70bb0f97 |
816 | =item B<< $metaclass->error_class($class_name) >> |
8b1d510f |
817 | |
70bb0f97 |
818 | The name of the class used to throw errors. This defaults to |
8b1d510f |
819 | L<Moose::Error::Default>, which generates an error with a stacktrace |
820 | just like C<Carp::confess>. |
821 | |
70bb0f97 |
822 | =item B<< $metaclass->throw_error($message, %extra) >> |
11c86f15 |
823 | |
824 | Throws the error created by C<create_error> using C<raise_error> |
825 | |
c0e30cf5 |
826 | =back |
827 | |
828 | =head1 BUGS |
829 | |
d4048ef3 |
830 | See L<Moose/BUGS> for details on reporting bugs. |
c0e30cf5 |
831 | |
c0e30cf5 |
832 | =head1 AUTHOR |
833 | |
834 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
835 | |
836 | =head1 COPYRIGHT AND LICENSE |
837 | |
7e0492d3 |
838 | Copyright 2006-2010 by Infinity Interactive, Inc. |
c0e30cf5 |
839 | |
840 | L<http://www.iinteractive.com> |
841 | |
842 | This library is free software; you can redistribute it and/or modify |
ac2dc464 |
843 | it under the same terms as Perl itself. |
c0e30cf5 |
844 | |
8a7a9c53 |
845 | =cut |
1a563243 |
846 | |