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