Add failing test from Catalyst
[gitmo/Moose.git] / lib / Moose / Meta / Class.pm
CommitLineData
c0e30cf5 1
2package Moose::Meta::Class;
3
4use strict;
5use warnings;
6
0addec44 7use Class::MOP;
648e79ae 8
11c86f15 9use Carp ();
2e7f6cf4 10use Data::OptList;
f8b6827f 11use List::Util qw( first );
349cda54 12use List::MoreUtils qw( any all uniq first_index );
21f1e231 13use Scalar::Util 'weaken', 'blessed';
a15dff8d 14
40290d18 15our $VERSION = '1.04';
e606ae5f 16$VERSION = eval $VERSION;
d44714be 17our $AUTHORITY = 'cpan:STEVAN';
bc1e29b5 18
74862722 19use Moose::Meta::Method::Overridden;
3f9e4b0a 20use Moose::Meta::Method::Augmented;
77f14411 21use Moose::Error::Default;
0fa70d03 22use Moose::Meta::Class::Immutable::Trait;
23use Moose::Meta::Method::Constructor;
24use Moose::Meta::Method::Destructor;
8ee73eeb 25
c0e30cf5 26use 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 60sub 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 72sub _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 85sub 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
17594769 102my %ANON_CLASSES;
103
104sub create_anon_class {
105 my ($self, %options) = @_;
106
107 my $cache_ok = delete $options{cache};
d03bd989 108
cf600c83 109 my $cache_key
110 = _anon_cache_key( $options{superclasses}, $options{roles} );
d03bd989 111
6d5cbd2b 112 if ($cache_ok && defined $ANON_CLASSES{$cache_key}) {
17594769 113 return $ANON_CLASSES{$cache_key};
114 }
d03bd989 115
17594769 116 my $new_class = $self->SUPER::create_anon_class(%options);
117
6d5cbd2b 118 $ANON_CLASSES{$cache_key} = $new_class
119 if $cache_ok;
17594769 120
121 return $new_class;
122}
123
cf600c83 124sub _anon_cache_key {
125 # Makes something like Super::Class|Super::Class::2=Role|Role::1
126 return join '=' => (
127 join( '|', @{ $_[0] || [] } ),
128 join( '|', sort @{ $_[1] || [] } ),
129 );
130}
131
132sub reinitialize {
133 my $self = shift;
134 my $pkg = shift;
135
136 my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg);
137
138 my $cache_key;
139
140 my %existing_classes;
141 if ($meta) {
142 %existing_classes = map { $_ => $meta->$_() } qw(
143 attribute_metaclass
144 method_metaclass
145 wrapped_method_metaclass
146 instance_metaclass
147 constructor_class
148 destructor_class
149 error_class
150 );
151
152 $cache_key = _anon_cache_key(
153 [ $meta->superclasses ],
154 [ map { $_->name } @{ $meta->roles } ],
155 ) if $meta->is_anon_class;
156 }
157
158 my $new_meta = $self->SUPER::reinitialize(
159 $pkg,
160 %existing_classes,
161 @_,
162 );
163
164 return $new_meta unless defined $cache_key;
165
166 my $new_cache_key = _anon_cache_key(
167 [ $meta->superclasses ],
168 [ map { $_->name } @{ $meta->roles } ],
169 );
170
171 delete $ANON_CLASSES{$cache_key};
172 $ANON_CLASSES{$new_cache_key} = $new_meta;
173
174 return $new_meta;
175}
176
ef333f17 177sub add_role {
178 my ($self, $role) = @_;
179 (blessed($role) && $role->isa('Moose::Meta::Role'))
11c86f15 180 || $self->throw_error("Roles must be instances of Moose::Meta::Role", data => $role);
ef333f17 181 push @{$self->roles} => $role;
182}
183
639f9a1a 184sub role_applications {
185 my ($self) = @_;
186
187 return @{$self->_get_role_applications};
188}
189
a9b63d79 190sub add_role_application {
191 my ($self, $application) = @_;
192 (blessed($application) && $application->isa('Moose::Meta::Role::Application::ToClass'))
193 || $self->throw_error("Role applications must be instances of Moose::Meta::Role::Application::ToClass", data => $application);
639f9a1a 194 push @{$self->_get_role_applications} => $application;
a9b63d79 195}
196
b8aeb4dc 197sub calculate_all_roles {
198 my $self = shift;
199 my %seen;
200 grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles };
201}
202
9f83eb5d 203sub calculate_all_roles_with_inheritance {
204 my $self = shift;
205 my %seen;
206 grep { !$seen{$_->name}++ }
207 map { Class::MOP::class_of($_)->can('calculate_all_roles')
208 ? Class::MOP::class_of($_)->calculate_all_roles
209 : () }
210 $self->linearized_isa;
211}
212
ef333f17 213sub 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 233sub 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 253sub 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 283sub 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 298sub 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 313sub 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
327sub 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 341sub _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
f6df97ae 350## Metaclass compatibility
f8b6827f 351
f6df97ae 352sub _base_metaclasses {
353 my $self = shift;
354 my %metaclasses = $self->SUPER::_base_metaclasses;
355 for my $class (keys %metaclasses) {
356 $metaclasses{$class} =~ s/^Class::MOP/Moose::Meta/;
1341f10c 357 }
f6df97ae 358 return (
359 %metaclasses,
360 error_class => 'Moose::Error::Default',
f8b6827f 361 );
f8b6827f 362}
363
f6df97ae 364sub _find_common_base {
365 my $self = shift;
366 my ($meta1, $meta2) = map { Class::MOP::class_of($_) } @_;
367 return unless defined($meta1) && defined($meta2);
f8b6827f 368
369 # FIXME? This doesn't account for multiple inheritance (not sure
370 # if it needs to though). For example, is somewhere in $meta1's
db9fda52 371 # history it inherits from both ClassA and ClassB, and $meta2
f8b6827f 372 # inherits from ClassB & ClassA, does it matter? And what crazy
373 # fool would do that anyway?
374
375 my %meta1_parents = map { $_ => 1 } $meta1->linearized_isa;
376
377 return first { $meta1_parents{$_} } $meta2->linearized_isa;
378}
379
f6df97ae 380sub _get_ancestors_until {
381 my $self = shift;
dd3ac8f9 382 my ($start_name, $until_name) = @_;
f8b6827f 383
dd3ac8f9 384 my @ancestor_names;
385 for my $ancestor_name (Class::MOP::class_of($start_name)->linearized_isa) {
386 last if $ancestor_name eq $until_name;
387 push @ancestor_names, $ancestor_name;
f6df97ae 388 }
dd3ac8f9 389 return @ancestor_names;
f6df97ae 390}
f8b6827f 391
f6df97ae 392sub _is_role_only_subclass {
393 my $self = shift;
dd3ac8f9 394 my ($meta_name) = @_;
395 my $meta = Class::MOP::Class->initialize($meta_name);
396 my @parent_names = $meta->superclasses;
f6df97ae 397
398 # XXX: don't feel like messing with multiple inheritance here... what would
399 # that even do?
dd3ac8f9 400 return unless @parent_names == 1;
401 my ($parent_name) = @parent_names;
402 my $parent_meta = Class::MOP::Class->initialize($parent_name);
f6df97ae 403
404 # loop over all methods that are a part of the current class
405 # (not inherited)
dd3ac8f9 406 for my $method (map { $meta->get_method($_) } $meta->get_method_list) {
f6df97ae 407 # always ignore meta
f8b6827f 408 next if $method->name eq 'meta';
f6df97ae 409 # we'll deal with attributes below
410 next if $method->isa('Class::MOP::Method::Accessor');
411 # if the method comes from a role we consumed, ignore it
dd3ac8f9 412 next if $meta->can('does_role')
413 && $meta->does_role($method->original_package_name);
f8b6827f 414
415 return 0;
416 }
417
f6df97ae 418 # loop over all attributes that are a part of the current class
419 # (not inherited)
f8b6827f 420 # FIXME - this really isn't right. Just because an attribute is
421 # defined in a role doesn't mean it isn't _also_ defined in the
422 # subclass.
dd3ac8f9 423 for my $attr (map { $meta->get_attribute($_) } $meta->get_attribute_list) {
f6df97ae 424 next if any { $_->has_attribute($attr->name) }
9f83eb5d 425 $meta->calculate_all_roles_with_inheritance;
f8b6827f 426
427 return 0;
428 }
429
430 return 1;
431}
432
f6df97ae 433sub _can_fix_class_metaclass_incompatibility_by_role_reconciliation {
434 my $self = shift;
435 my ($super_meta) = @_;
436
9f83eb5d 437 my $super_meta_name = $super_meta->is_immutable
438 ? $super_meta->_get_mutable_metaclass_name
439 : blessed($super_meta);
440 my $common_base_name = $self->_find_common_base(blessed($self), $super_meta_name);
f6df97ae 441 # if they're not both moose metaclasses, and the cmop fixing couldn't
442 # do anything, there's nothing more we can do
dd3ac8f9 443 return unless defined($common_base_name);
444 return unless $common_base_name->isa('Moose::Meta::Class');
f8b6827f 445
9f83eb5d 446 my @super_meta_name_ancestor_names = $self->_get_ancestors_until($super_meta_name, $common_base_name);
dd3ac8f9 447 my @class_meta_name_ancestor_names = $self->_get_ancestors_until(blessed($self), $common_base_name);
f6df97ae 448 # we're only dealing with roles here
449 return unless all { $self->_is_role_only_subclass($_) }
dd3ac8f9 450 (@super_meta_name_ancestor_names,
451 @class_meta_name_ancestor_names);
f6df97ae 452
453 return 1;
f8b6827f 454}
455
f6df97ae 456sub _can_fix_single_metaclass_incompatibility_by_role_reconciliation {
457 my $self = shift;
dd3ac8f9 458 my ($metaclass_type, $super_meta) = @_;
f8b6827f 459
dd3ac8f9 460 my $class_specific_meta_name = $self->$metaclass_type;
461 return unless $super_meta->can($metaclass_type);
462 my $super_specific_meta_name = $super_meta->$metaclass_type;
f6df97ae 463 my %metaclasses = $self->_base_metaclasses;
f8b6827f 464
dd3ac8f9 465 my $common_base_name = $self->_find_common_base($class_specific_meta_name, $super_specific_meta_name);
f6df97ae 466 # if they're not both moose metaclasses, and the cmop fixing couldn't
467 # do anything, there's nothing more we can do
dd3ac8f9 468 return unless defined($common_base_name);
469 return unless $common_base_name->isa($metaclasses{$metaclass_type});
f8b6827f 470
dd3ac8f9 471 my @super_specific_meta_name_ancestor_names = $self->_get_ancestors_until($super_specific_meta_name, $common_base_name);
472 my @class_specific_meta_name_ancestor_names = $self->_get_ancestors_until($class_specific_meta_name, $common_base_name);
f6df97ae 473 # we're only dealing with roles here
474 return unless all { $self->_is_role_only_subclass($_) }
dd3ac8f9 475 (@super_specific_meta_name_ancestor_names,
476 @class_specific_meta_name_ancestor_names);
f8b6827f 477
f6df97ae 478 return 1;
479}
f8b6827f 480
f6df97ae 481sub _role_differences {
482 my $self = shift;
dd3ac8f9 483 my ($class_meta_name, $super_meta_name) = @_;
9f83eb5d 484 my @super_role_metas = $super_meta_name->meta->can('calculate_all_roles_with_inheritance')
485 ? $super_meta_name->meta->calculate_all_roles_with_inheritance
dd3ac8f9 486 : ();
9f83eb5d 487 my @role_metas = $class_meta_name->meta->can('calculate_all_roles_with_inheritance')
488 ? $class_meta_name->meta->calculate_all_roles_with_inheritance
dd3ac8f9 489 : ();
f6df97ae 490 my @differences;
dd3ac8f9 491 for my $role_meta (@role_metas) {
492 push @differences, $role_meta
493 unless any { $_->name eq $role_meta->name } @super_role_metas;
f8b6827f 494 }
f6df97ae 495 return @differences;
f8b6827f 496}
497
f6df97ae 498sub _reconcile_roles_for_metaclass {
499 my $self = shift;
dd3ac8f9 500 my ($class_meta_name, $super_meta_name) = @_;
f8b6827f 501
dd3ac8f9 502 my @role_differences = $self->_role_differences(
503 $class_meta_name, $super_meta_name,
504 );
9f83eb5d 505 return Moose::Meta::Class->create_anon_class(
dd3ac8f9 506 superclasses => [$super_meta_name],
f6df97ae 507 roles => \@role_differences,
508 cache => 1,
509 );
510}
511
512sub _can_fix_metaclass_incompatibility_by_role_reconciliation {
513 my $self = shift;
514 my ($super_meta) = @_;
f8b6827f 515
f6df97ae 516 return 1 if $self->_can_fix_class_metaclass_incompatibility_by_role_reconciliation($super_meta);
f8b6827f 517
f6df97ae 518 my %base_metaclass = $self->_base_metaclasses;
519 for my $metaclass_type (keys %base_metaclass) {
520 next unless defined $self->$metaclass_type;
521 return 1 if $self->_can_fix_single_metaclass_incompatibility_by_role_reconciliation($metaclass_type, $super_meta);
f8b6827f 522 }
523
f6df97ae 524 return;
525}
f8b6827f 526
f6df97ae 527sub _can_fix_metaclass_incompatibility {
528 my $self = shift;
529 return 1 if $self->_can_fix_metaclass_incompatibility_by_role_reconciliation(@_);
530 return $self->SUPER::_can_fix_metaclass_incompatibility(@_);
531}
532
533sub _fix_class_metaclass_incompatibility {
534 my $self = shift;
535 my ($super_meta) = @_;
f8b6827f 536
f6df97ae 537 $self->SUPER::_fix_class_metaclass_incompatibility(@_);
f8b6827f 538
f6df97ae 539 if ($self->_can_fix_class_metaclass_incompatibility_by_role_reconciliation($super_meta)) {
9f83eb5d 540 my $super_meta_name = $super_meta->is_immutable
541 ? $super_meta->_get_mutable_metaclass_name
542 : blessed($super_meta);
543 my $class_meta_subclass_meta = $self->_reconcile_roles_for_metaclass(blessed($self), $super_meta_name);
cf7febc7 544 my $new_self = $class_meta_subclass_meta->name->reinitialize(
545 $self->name,
546 );
547 %$self = %$new_self;
548 bless $self, $class_meta_subclass_meta->name;
549 # We need to replace the cached metaclass instance or else when it
550 # goes out of scope Class::MOP::Class destroy's the namespace for
551 # the metaclass's class, causing much havoc.
552 Class::MOP::store_metaclass_by_name( $self->name, $self );
553 Class::MOP::weaken_metaclass( $self->name ) if $self->is_anon_class;
f8b6827f 554 }
f6df97ae 555}
f8b6827f 556
f6df97ae 557sub _fix_single_metaclass_incompatibility {
558 my $self = shift;
559 my ($metaclass_type, $super_meta) = @_;
f8b6827f 560
f6df97ae 561 $self->SUPER::_fix_single_metaclass_incompatibility(@_);
f8b6827f 562
f6df97ae 563 if ($self->_can_fix_single_metaclass_incompatibility_by_role_reconciliation($metaclass_type, $super_meta)) {
564 my %metaclasses = $self->_base_metaclasses;
dd3ac8f9 565 my $class_specific_meta_subclass_meta = $self->_reconcile_roles_for_metaclass($self->$metaclass_type, $super_meta->$metaclass_type);
cf7febc7 566 my $new_self = $super_meta->reinitialize(
567 $self->name,
568 $metaclass_type => $class_specific_meta_subclass_meta->name,
569 );
570 %$self = %$new_self;
571 bless $self, blessed($super_meta);
572 # We need to replace the cached metaclass instance or else when it
573 # goes out of scope Class::MOP::Class destroy's the namespace for
574 # the metaclass's class, causing much havoc.
575 Class::MOP::store_metaclass_by_name( $self->name, $self );
576 Class::MOP::weaken_metaclass( $self->name ) if $self->is_anon_class;
f6df97ae 577 }
f8b6827f 578}
579
1341f10c 580sub _process_attribute {
a3738e5b 581 my ( $self, $name, @args ) = @_;
7e59b803 582
583 @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH';
d9bb6c63 584
f9b5f5f8 585 if (($name || '') =~ /^\+(.*)/) {
7e59b803 586 return $self->_process_inherited_attribute($1, @args);
1341f10c 587 }
588 else {
7e59b803 589 return $self->_process_new_attribute($name, @args);
590 }
591}
592
593sub _process_new_attribute {
594 my ( $self, $name, @args ) = @_;
7e59b803 595
d5c30e52 596 $self->attribute_metaclass->interpolate_class_and_new($name, @args);
1341f10c 597}
598
599sub _process_inherited_attribute {
600 my ($self, $attr_name, %options) = @_;
601 my $inherited_attr = $self->find_attribute_by_name($attr_name);
602 (defined $inherited_attr)
329c5dd4 603 || $self->throw_error("Could not find an attribute by the name of '$attr_name' to inherit from in ${\$self->name}", data => $attr_name);
1341f10c 604 if ($inherited_attr->isa('Moose::Meta::Attribute')) {
d7d8a8c7 605 return $inherited_attr->clone_and_inherit_options(%options);
1341f10c 606 }
607 else {
608 # NOTE:
609 # kind of a kludge to handle Class::MOP::Attributes
d7d8a8c7 610 return $inherited_attr->Moose::Meta::Attribute::clone_and_inherit_options(%options);
ac2dc464 611 }
1341f10c 612}
613
5cf3dbcf 614## -------------------------------------------------
615
bf6fa6b3 616our $error_level;
11c86f15 617
618sub throw_error {
619 my ( $self, @args ) = @_;
bf6fa6b3 620 local $error_level = ($error_level || 0) + 1;
11c86f15 621 $self->raise_error($self->create_error(@args));
622}
623
624sub raise_error {
625 my ( $self, @args ) = @_;
626 die @args;
627}
628
629sub create_error {
630 my ( $self, @args ) = @_;
631
18748ad6 632 require Carp::Heavy;
633
bf6fa6b3 634 local $error_level = ($error_level || 0 ) + 1;
18748ad6 635
11c86f15 636 if ( @args % 2 == 1 ) {
637 unshift @args, "message";
638 }
639
fcab1742 640 my %args = ( metaclass => $self, last_error => $@, @args );
11c86f15 641
bf6fa6b3 642 $args{depth} += $error_level;
11c86f15 643
bf6fa6b3 644 my $class = ref $self ? $self->error_class : "Moose::Error::Default";
11c86f15 645
a810a01f 646 Class::MOP::load_class($class);
647
11c86f15 648 $class->new(
bf6fa6b3 649 Carp::caller_info($args{depth}),
650 %args
11c86f15 651 );
652}
653
c0e30cf5 6541;
655
656__END__
657
658=pod
659
660=head1 NAME
661
e522431d 662Moose::Meta::Class - The Moose metaclass
c0e30cf5 663
c0e30cf5 664=head1 DESCRIPTION
665
70bb0f97 666This class is a subclass of L<Class::MOP::Class> that provides
667additional Moose-specific functionality.
e522431d 668
7854b409 669To really understand this class, you will need to start with the
670L<Class::MOP::Class> documentation. This class can be understood as a
671set of additional features on top of the basic feature provided by
672that parent class.
6ba6d68c 673
d4b1449e 674=head1 INHERITANCE
675
676C<Moose::Meta::Class> is a subclass of L<Class::MOP::Class>.
677
c0e30cf5 678=head1 METHODS
679
680=over 4
681
70bb0f97 682=item B<< Moose::Meta::Class->initialize($package_name, %options) >>
590868a3 683
70bb0f97 684This overrides the parent's method in order to provide its own
685defaults for the C<attribute_metaclass>, C<instance_metaclass>, and
686C<method_metaclass> options.
61bdd94f 687
70bb0f97 688These all default to the appropriate Moose class.
61bdd94f 689
70bb0f97 690=item B<< Moose::Meta::Class->create($package_name, %options) >>
17594769 691
70bb0f97 692This overrides the parent's method in order to accept a C<roles>
9e25a72a 693option. This should be an array reference containing roles
694that the class does, each optionally followed by a hashref of options
695(C<-excludes> and C<-alias>).
17594769 696
70bb0f97 697 my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] );
17594769 698
70bb0f97 699=item B<< Moose::Meta::Class->create_anon_class >>
17594769 700
70bb0f97 701This overrides the parent's method to accept a C<roles> option, just
702as C<create> does.
5cf3dbcf 703
70bb0f97 704It also accepts a C<cache> option. If this is true, then the anonymous
705class will be cached based on its superclasses and roles. If an
706existing anonymous class in the cache has the same superclasses and
707roles, it will be reused.
ac2dc464 708
70bb0f97 709 my $metaclass = Moose::Meta::Class->create_anon_class(
710 superclasses => ['Foo'],
711 roles => [qw/Some Roles Go Here/],
712 cache => 1,
713 );
ac2dc464 714
2e7f6cf4 715Each entry in both the C<superclasses> and the C<roles> option can be
b2d54db8 716followed by a hash reference with arguments. The C<superclasses>
2e7f6cf4 717option can be supplied with a L<-version|Class::MOP/Class Loading
718Options> option that ensures the loaded superclass satisfies the
719required version. The C<role> option also takes the C<-version> as an
720argument, but the option hash reference can also contain any other
721role relevant values like exclusions or parameterized role arguments.
722
70bb0f97 723=item B<< $metaclass->make_immutable(%options) >>
ac2dc464 724
70bb0f97 725This overrides the parent's method to add a few options. Specifically,
726it uses the Moose-specific constructor and destructor classes, and
727enables inlining the destructor.
8c9d74e7 728
70bb0f97 729Also, since Moose always inlines attributes, it sets the
730C<inline_accessors> option to false.
02a0fb52 731
70bb0f97 732=item B<< $metaclass->new_object(%params) >>
a15dff8d 733
70bb0f97 734This overrides the parent's method in order to add support for
735attribute triggers.
6ba6d68c 736
2e7f6cf4 737=item B<< $metaclass->superclasses(@superclasses) >>
738
6b958a3e 739This is the accessor allowing you to read or change the parents of
2e7f6cf4 740the class.
741
742Each superclass can be followed by a hash reference containing a
743L<-version|Class::MOP/Class Loading Options> value. If the version
744requirement is not satisfied an error will be thrown.
745
70bb0f97 746=item B<< $metaclass->add_override_method_modifier($name, $sub) >>
ef1d5f4b 747
70bb0f97 748This adds an C<override> method modifier to the package.
e9ec68d6 749
70bb0f97 750=item B<< $metaclass->add_augment_method_modifier($name, $sub) >>
e9ec68d6 751
70bb0f97 752This adds an C<augment> method modifier to the package.
78cd1d3b 753
70bb0f97 754=item B<< $metaclass->calculate_all_roles >>
02a0fb52 755
70bb0f97 756This will return a unique array of C<Moose::Meta::Role> instances
757which are attached to this class.
78cd1d3b 758
9f83eb5d 759=item B<< $metaclass->calculate_all_roles_with_inheritance >>
760
761This will return a unique array of C<Moose::Meta::Role> instances
762which are attached to this class, and each of this class's ancestors.
763
70bb0f97 764=item B<< $metaclass->add_role($role) >>
02a0fb52 765
70bb0f97 766This takes a L<Moose::Meta::Role> object, and adds it to the class's
767list of roles. This I<does not> actually apply the role to the class.
2b14ac61 768
b90dd4ef 769=item B<< $metaclass->role_applications >>
770
639f9a1a 771Returns a list of L<Moose::Meta::Role::Application::ToClass>
b90dd4ef 772objects, which contain the arguments to role application.
773
774=item B<< $metaclass->add_role_application($application) >>
775
776This takes a L<Moose::Meta::Role::Application::ToClass> object, and
777adds it to the class's list of role applications. This I<does not>
778actually apply any role to the class; it is only for tracking role
779applications.
780
560c498d 781=item B<< $metaclass->does_role($role) >>
ef333f17 782
560c498d 783This returns a boolean indicating whether or not the class does the specified
784role. The role provided can be either a role name or a L<Moose::Meta::Role>
785object. This tests both the class and its parents.
02a0fb52 786
70bb0f97 787=item B<< $metaclass->excludes_role($role_name) >>
ef333f17 788
70bb0f97 789A class excludes a role if it has already composed a role which
790excludes the named role. This tests both the class and its parents.
02a0fb52 791
70bb0f97 792=item B<< $metaclass->add_attribute($attr_name, %params|$params) >>
ef333f17 793
70bb0f97 794This overrides the parent's method in order to allow the parameters to
795be provided as a hash reference.
02a0fb52 796
9f9fdd08 797=item B<< $metaclass->constructor_class($class_name) >>
d79e62fd 798
9f9fdd08 799=item B<< $metaclass->destructor_class($class_name) >>
e606ae5f 800
801These are the names of classes used when making a class
802immutable. These default to L<Moose::Meta::Method::Constructor> and
803L<Moose::Meta::Method::Destructor> respectively. These accessors are
804read-write, so you can use them to change the class name.
805
70bb0f97 806=item B<< $metaclass->error_class($class_name) >>
8b1d510f 807
70bb0f97 808The name of the class used to throw errors. This defaults to
8b1d510f 809L<Moose::Error::Default>, which generates an error with a stacktrace
810just like C<Carp::confess>.
811
70bb0f97 812=item B<< $metaclass->throw_error($message, %extra) >>
11c86f15 813
814Throws the error created by C<create_error> using C<raise_error>
815
c0e30cf5 816=back
817
818=head1 BUGS
819
d4048ef3 820See L<Moose/BUGS> for details on reporting bugs.
c0e30cf5 821
c0e30cf5 822=head1 AUTHOR
823
824Stevan Little E<lt>stevan@iinteractive.comE<gt>
825
826=head1 COPYRIGHT AND LICENSE
827
7e0492d3 828Copyright 2006-2010 by Infinity Interactive, Inc.
c0e30cf5 829
830L<http://www.iinteractive.com>
831
832This library is free software; you can redistribute it and/or modify
ac2dc464 833it under the same terms as Perl itself.
c0e30cf5 834
8a7a9c53 835=cut
1a563243 836