b3e95e45ed88a5799dc49da93f6dafce43216fcc
[gitmo/Moose.git] / lib / Moose / Meta / Class.pm
1
2 package Moose::Meta::Class;
3
4 use strict;
5 use warnings;
6
7 use Class::MOP;
8
9 use Carp qw( confess );
10 use Data::OptList;
11 use List::Util qw( first );
12 use List::MoreUtils qw( any all uniq first_index );
13 use Scalar::Util 'blessed';
14
15 use Moose::Meta::Method::Overridden;
16 use Moose::Meta::Method::Augmented;
17 use Moose::Error::Default;
18 use Moose::Meta::Class::Immutable::Trait;
19 use Moose::Meta::Method::Constructor;
20 use Moose::Meta::Method::Destructor;
21 use Moose::Meta::Method::Meta;
22 use Moose::Util;
23 use Class::MOP::MiniTrait;
24
25 use base 'Class::MOP::Class';
26
27 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
28
29 __PACKAGE__->meta->add_attribute('roles' => (
30     reader  => 'roles',
31     default => sub { [] }
32 ));
33
34 __PACKAGE__->meta->add_attribute('role_applications' => (
35     reader  => '_get_role_applications',
36     default => sub { [] }
37 ));
38
39 __PACKAGE__->meta->add_attribute(
40     Class::MOP::Attribute->new('immutable_trait' => (
41         accessor => "immutable_trait",
42         default  => 'Moose::Meta::Class::Immutable::Trait',
43     ))
44 );
45
46 __PACKAGE__->meta->add_attribute('constructor_class' => (
47     accessor => 'constructor_class',
48     default  => 'Moose::Meta::Method::Constructor',
49 ));
50
51 __PACKAGE__->meta->add_attribute('destructor_class' => (
52     accessor => 'destructor_class',
53     default  => 'Moose::Meta::Method::Destructor',
54 ));
55
56 __PACKAGE__->meta->add_attribute('error_class' => (
57     accessor => 'error_class',
58     default  => 'Moose::Error::Default',
59 ));
60
61 sub initialize {
62     my $class = shift;
63     my @args = @_;
64     unshift @args, 'package' if @args % 2;
65     my %opts = @args;
66     my $package = delete $opts{package};
67     return Class::MOP::get_metaclass_by_name($package)
68         || $class->SUPER::initialize($package,
69                 'attribute_metaclass' => 'Moose::Meta::Attribute',
70                 'method_metaclass'    => 'Moose::Meta::Method',
71                 'instance_metaclass'  => 'Moose::Meta::Instance',
72                 %opts,
73             );
74 }
75
76 sub create {
77     my $class = shift;
78     my @args = @_;
79
80     unshift @args, 'package' if @args % 2 == 1;
81     my %options = @args;
82
83     (ref $options{roles} eq 'ARRAY')
84         || $class->throw_error("You must pass an ARRAY ref of roles", data => $options{roles})
85             if exists $options{roles};
86
87     my $package = delete $options{package};
88     my $roles   = delete $options{roles};
89
90     my $new_meta = $class->SUPER::create($package, %options);
91
92     if ($roles) {
93         Moose::Util::apply_all_roles( $new_meta, @$roles );
94     }
95
96     return $new_meta;
97 }
98
99 sub _meta_method_class { 'Moose::Meta::Method::Meta' }
100
101 sub _anon_package_prefix { 'Moose::Meta::Class::__ANON__::SERIAL::' }
102
103 sub _anon_cache_key {
104     my $class = shift;
105     my %options = @_;
106     # Makes something like Super::Class|Super::Class::2=Role|Role::1
107     return join '=' => (
108         join( '|', @{ $options{superclasses} || [] } ),
109         join( '|', sort @{ $options{roles}   || [] } ),
110     );
111 }
112
113 sub reinitialize {
114     my $self = shift;
115     my $pkg  = shift;
116
117     my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg);
118
119     my %existing_classes;
120     if ($meta) {
121         %existing_classes = map { $_ => $meta->$_() } qw(
122             attribute_metaclass
123             method_metaclass
124             wrapped_method_metaclass
125             instance_metaclass
126             constructor_class
127             destructor_class
128             error_class
129         );
130     }
131
132     return $self->SUPER::reinitialize(
133         $pkg,
134         %existing_classes,
135         @_,
136     );
137 }
138
139 sub add_role {
140     my ($self, $role) = @_;
141     (blessed($role) && $role->isa('Moose::Meta::Role'))
142         || $self->throw_error("Roles must be instances of Moose::Meta::Role", data => $role);
143     push @{$self->roles} => $role;
144 }
145
146 sub role_applications {
147     my ($self) = @_;
148
149     return @{$self->_get_role_applications};
150 }
151
152 sub add_role_application {
153     my ($self, $application) = @_;
154     (blessed($application) && $application->isa('Moose::Meta::Role::Application::ToClass'))
155         || $self->throw_error("Role applications must be instances of Moose::Meta::Role::Application::ToClass", data => $application);
156     push @{$self->_get_role_applications} => $application;
157 }
158
159 sub calculate_all_roles {
160     my $self = shift;
161     my %seen;
162     grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles };
163 }
164
165 sub calculate_all_roles_with_inheritance {
166     my $self = shift;
167     my %seen;
168     grep { !$seen{$_->name}++ }
169          map { Class::MOP::class_of($_)->can('calculate_all_roles')
170                    ? Class::MOP::class_of($_)->calculate_all_roles
171                    : () }
172              $self->linearized_isa;
173 }
174
175 sub does_role {
176     my ($self, $role_name) = @_;
177
178     (defined $role_name)
179         || $self->throw_error("You must supply a role name to look for");
180
181     foreach my $class ($self->class_precedence_list) {
182         my $meta = Class::MOP::class_of($class);
183         # when a Moose metaclass is itself extended with a role,
184         # this check needs to be done since some items in the
185         # class_precedence_list might in fact be Class::MOP
186         # based still.
187         next unless $meta && $meta->can('roles');
188         foreach my $role (@{$meta->roles}) {
189             return 1 if $role->does_role($role_name);
190         }
191     }
192     return 0;
193 }
194
195 sub excludes_role {
196     my ($self, $role_name) = @_;
197
198     (defined $role_name)
199         || $self->throw_error("You must supply a role name to look for");
200
201     foreach my $class ($self->class_precedence_list) {
202         my $meta = Class::MOP::class_of($class);
203         # when a Moose metaclass is itself extended with a role,
204         # this check needs to be done since some items in the
205         # class_precedence_list might in fact be Class::MOP
206         # based still.
207         next unless $meta && $meta->can('roles');
208         foreach my $role (@{$meta->roles}) {
209             return 1 if $role->excludes_role($role_name);
210         }
211     }
212     return 0;
213 }
214
215 sub new_object {
216     my $self   = shift;
217     my $params = @_ == 1 ? $_[0] : {@_};
218     my $object = $self->SUPER::new_object($params);
219
220     foreach my $attr ( $self->get_all_attributes() ) {
221
222         next unless $attr->can('has_trigger') && $attr->has_trigger;
223
224         my $init_arg = $attr->init_arg;
225
226         next unless defined $init_arg;
227
228         next unless exists $params->{$init_arg};
229
230         $attr->trigger->(
231             $object,
232             (
233                   $attr->should_coerce
234                 ? $attr->get_read_method_ref->($object)
235                 : $params->{$init_arg}
236             ),
237         );
238     }
239
240     $object->BUILDALL($params) if $object->can('BUILDALL');
241
242     return $object;
243 }
244
245 sub _generate_fallback_constructor {
246     my $self = shift;
247     my ($class) = @_;
248     return $class . '->Moose::Object::new(@_)'
249 }
250
251 sub _inline_params {
252     my $self = shift;
253     my ($params, $class) = @_;
254     return (
255         'my ' . $params . ' = ',
256         $self->_inline_BUILDARGS($class, '@_'),
257         ';',
258     );
259 }
260
261 sub _inline_BUILDARGS {
262     my $self = shift;
263     my ($class, $args) = @_;
264
265     my $buildargs = $self->find_method_by_name("BUILDARGS");
266
267     if ($args eq '@_'
268      && (!$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS)) {
269         return (
270             'do {',
271                 'my $params;',
272                 'if (scalar @_ == 1) {',
273                     'if (!defined($_[0]) || ref($_[0]) ne \'HASH\') {',
274                         $self->_inline_throw_error(
275                             '"Single parameters to new() must be a HASH ref"',
276                             'data => $_[0]',
277                         ) . ';',
278                     '}',
279                     '$params = { %{ $_[0] } };',
280                 '}',
281                 'elsif (@_ % 2) {',
282                     'Carp::carp(',
283                         '"The new() method for ' . $class . ' expects a '
284                       . 'hash reference or a key/value list. You passed an '
285                       . 'odd number of arguments"',
286                     ');',
287                     '$params = {@_, undef};',
288                 '}',
289                 'else {',
290                     '$params = {@_};',
291                 '}',
292                 '$params;',
293             '}',
294         );
295     }
296     else {
297         return $class . '->BUILDARGS(' . $args . ')';
298     }
299 }
300
301 sub _inline_slot_initializer {
302     my $self  = shift;
303     my ($attr, $idx) = @_;
304
305     return (
306         '## ' . $attr->name,
307         $self->_inline_check_required_attr($attr),
308         $self->SUPER::_inline_slot_initializer(@_),
309     );
310 }
311
312 sub _inline_check_required_attr {
313     my $self = shift;
314     my ($attr) = @_;
315
316     return unless defined $attr->init_arg;
317     return unless $attr->can('is_required') && $attr->is_required;
318     return if $attr->has_default || $attr->has_builder;
319
320     return (
321         'if (!exists $params->{\'' . $attr->init_arg . '\'}) {',
322             $self->_inline_throw_error(
323                 '"Attribute (' . quotemeta($attr->name) . ') is required"'
324             ) . ';',
325         '}',
326     );
327 }
328
329 # XXX: these two are duplicated from cmop, because we have to pass the tc stuff
330 # through to _inline_set_value - this should probably be fixed, but i'm not
331 # quite sure how. -doy
332 sub _inline_init_attr_from_constructor {
333     my $self = shift;
334     my ($attr, $idx) = @_;
335
336     my @initial_value = $attr->_inline_set_value(
337         '$instance',
338         '$params->{\'' . $attr->init_arg . '\'}',
339         '$type_constraint_bodies[' . $idx . ']',
340         '$type_constraints[' . $idx . ']',
341         'for constructor',
342     );
343
344     push @initial_value, (
345         '$attrs->[' . $idx . ']->set_initial_value(',
346             '$instance,',
347             $attr->_inline_instance_get('$instance'),
348         ');',
349     ) if $attr->has_initializer;
350
351     return @initial_value;
352 }
353
354 sub _inline_init_attr_from_default {
355     my $self = shift;
356     my ($attr, $idx) = @_;
357
358     return if $attr->can('is_lazy') && $attr->is_lazy;
359     my $default = $self->_inline_default_value($attr, $idx);
360     return unless $default;
361
362     my @initial_value = (
363         'my $default = ' . $default . ';',
364         $attr->_inline_set_value(
365             '$instance',
366             '$default',
367             '$type_constraint_bodies[' . $idx . ']',
368             '$type_constraints[' . $idx . ']',
369             'for constructor',
370         ),
371     );
372
373     push @initial_value, (
374         '$attrs->[' . $idx . ']->set_initial_value(',
375             '$instance,',
376             $attr->_inline_instance_get('$instance'),
377         ');',
378     ) if $attr->has_initializer;
379
380     return @initial_value;
381 }
382
383 sub _inline_extra_init {
384     my $self = shift;
385     return (
386         $self->_inline_triggers,
387         $self->_inline_BUILDALL,
388     );
389 }
390
391 sub _inline_triggers {
392     my $self = shift;
393     my @trigger_calls;
394
395     my @attrs = sort { $a->name cmp $b->name } $self->get_all_attributes;
396     for my $i (0 .. $#attrs) {
397         my $attr = $attrs[$i];
398
399         next unless $attr->can('has_trigger') && $attr->has_trigger;
400
401         my $init_arg = $attr->init_arg;
402         next unless defined $init_arg;
403
404         push @trigger_calls,
405             'if (exists $params->{\'' . $init_arg . '\'}) {',
406                 '$attrs->[' . $i . ']->trigger->(',
407                     '$instance,',
408                     $attr->_inline_instance_get('$instance') . ',',
409                 ');',
410             '}';
411     }
412
413     return @trigger_calls;
414 }
415
416 sub _inline_BUILDALL {
417     my $self = shift;
418
419     my @methods = reverse $self->find_all_methods_by_name('BUILD');
420     my @BUILD_calls;
421
422     foreach my $method (@methods) {
423         push @BUILD_calls,
424             '$instance->' . $method->{class} . '::BUILD($params);';
425     }
426
427     return @BUILD_calls;
428 }
429
430 sub superclasses {
431     my $self = shift;
432     my $supers = Data::OptList::mkopt(\@_);
433     foreach my $super (@{ $supers }) {
434         my ($name, $opts) = @{ $super };
435         Class::MOP::load_class($name, $opts);
436         my $meta = Class::MOP::class_of($name);
437         $self->throw_error("You cannot inherit from a Moose Role ($name)")
438             if $meta && $meta->isa('Moose::Meta::Role')
439     }
440     return $self->SUPER::superclasses(map { $_->[0] } @{ $supers });
441 }
442
443 ### ---------------------------------------------
444
445 sub add_attribute {
446     my $self = shift;
447     my $attr =
448         (blessed $_[0] && $_[0]->isa('Class::MOP::Attribute')
449             ? $_[0]
450             : $self->_process_attribute(@_));
451     $self->SUPER::add_attribute($attr);
452     # it may be a Class::MOP::Attribute, theoretically, which doesn't have
453     # 'bare' and doesn't implement this method
454     if ($attr->can('_check_associated_methods')) {
455         $attr->_check_associated_methods;
456     }
457     return $attr;
458 }
459
460 sub add_override_method_modifier {
461     my ($self, $name, $method, $_super_package) = @_;
462
463     (!$self->has_method($name))
464         || $self->throw_error("Cannot add an override method if a local method is already present");
465
466     $self->add_method($name => Moose::Meta::Method::Overridden->new(
467         method  => $method,
468         class   => $self,
469         package => $_super_package, # need this for roles
470         name    => $name,
471     ));
472 }
473
474 sub add_augment_method_modifier {
475     my ($self, $name, $method) = @_;
476     (!$self->has_method($name))
477         || $self->throw_error("Cannot add an augment method if a local method is already present");
478
479     $self->add_method($name => Moose::Meta::Method::Augmented->new(
480         method  => $method,
481         class   => $self,
482         name    => $name,
483     ));
484 }
485
486 ## Private Utility methods ...
487
488 sub _find_next_method_by_name_which_is_not_overridden {
489     my ($self, $name) = @_;
490     foreach my $method ($self->find_all_methods_by_name($name)) {
491         return $method->{code}
492             if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overridden');
493     }
494     return undef;
495 }
496
497 ## Metaclass compatibility
498
499 sub _base_metaclasses {
500     my $self = shift;
501     my %metaclasses = $self->SUPER::_base_metaclasses;
502     for my $class (keys %metaclasses) {
503         $metaclasses{$class} =~ s/^Class::MOP/Moose::Meta/;
504     }
505     return (
506         %metaclasses,
507         error_class => 'Moose::Error::Default',
508     );
509 }
510
511 sub _fix_class_metaclass_incompatibility {
512     my $self = shift;
513     my ($super_meta) = @_;
514
515     $self->SUPER::_fix_class_metaclass_incompatibility(@_);
516
517     if ($self->_class_metaclass_can_be_made_compatible($super_meta)) {
518         ($self->is_pristine)
519             || confess "Can't fix metaclass incompatibility for "
520                      . $self->name
521                      . " because it is not pristine.";
522         my $super_meta_name = $super_meta->_real_ref_name;
523         my $class_meta_subclass_meta_name = Moose::Util::_reconcile_roles_for_metaclass(blessed($self), $super_meta_name);
524         my $new_self = $class_meta_subclass_meta_name->reinitialize(
525             $self->name,
526         );
527
528         $self->_replace_self( $new_self, $class_meta_subclass_meta_name );
529     }
530 }
531
532 sub _fix_single_metaclass_incompatibility {
533     my $self = shift;
534     my ($metaclass_type, $super_meta) = @_;
535
536     $self->SUPER::_fix_single_metaclass_incompatibility(@_);
537
538     if ($self->_single_metaclass_can_be_made_compatible($super_meta, $metaclass_type)) {
539         ($self->is_pristine)
540             || confess "Can't fix metaclass incompatibility for "
541                      . $self->name
542                      . " because it is not pristine.";
543         my $super_meta_name = $super_meta->_real_ref_name;
544         my $class_specific_meta_subclass_meta_name = Moose::Util::_reconcile_roles_for_metaclass($self->$metaclass_type, $super_meta->$metaclass_type);
545         my $new_self = $super_meta->reinitialize(
546             $self->name,
547             $metaclass_type => $class_specific_meta_subclass_meta_name,
548         );
549
550         $self->_replace_self( $new_self, $super_meta_name );
551     }
552 }
553
554 sub _replace_self {
555     my $self      = shift;
556     my ( $new_self, $new_class)   = @_;
557
558     %$self = %$new_self;
559     bless $self, $new_class;
560
561     # We need to replace the cached metaclass instance or else when it goes
562     # out of scope Class::MOP::Class destroy's the namespace for the
563     # metaclass's class, causing much havoc.
564     my $weaken = Class::MOP::metaclass_is_weak( $self->name );
565     Class::MOP::store_metaclass_by_name( $self->name, $self );
566     Class::MOP::weaken_metaclass( $self->name ) if $weaken;
567 }
568
569 sub _process_attribute {
570     my ( $self, $name, @args ) = @_;
571
572     @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH';
573
574     if (($name || '') =~ /^\+(.*)/) {
575         return $self->_process_inherited_attribute($1, @args);
576     }
577     else {
578         return $self->_process_new_attribute($name, @args);
579     }
580 }
581
582 sub _process_new_attribute {
583     my ( $self, $name, @args ) = @_;
584
585     $self->attribute_metaclass->interpolate_class_and_new($name, @args);
586 }
587
588 sub _process_inherited_attribute {
589     my ($self, $attr_name, %options) = @_;
590     my $inherited_attr = $self->find_attribute_by_name($attr_name);
591     (defined $inherited_attr)
592         || $self->throw_error("Could not find an attribute by the name of '$attr_name' to inherit from in ${\$self->name}", data => $attr_name);
593     if ($inherited_attr->isa('Moose::Meta::Attribute')) {
594         return $inherited_attr->clone_and_inherit_options(%options);
595     }
596     else {
597         # NOTE:
598         # kind of a kludge to handle Class::MOP::Attributes
599         return $inherited_attr->Moose::Meta::Attribute::clone_and_inherit_options(%options);
600     }
601 }
602
603 # reinitialization support
604
605 sub _restore_metaobjects_from {
606     my $self = shift;
607     my ($old_meta) = @_;
608
609     $self->SUPER::_restore_metaobjects_from($old_meta);
610
611     for my $role ( @{ $old_meta->roles } ) {
612         $self->add_role($role);
613     }
614
615     for my $application ( @{ $old_meta->_get_role_applications } ) {
616         $application->class($self);
617         $self->add_role_application ($application);
618     }
619 }
620
621 ## Immutability
622
623 sub _immutable_options {
624     my ( $self, @args ) = @_;
625
626     $self->SUPER::_immutable_options(
627         inline_destructor => 1,
628
629         # Moose always does this when an attribute is created
630         inline_accessors => 0,
631
632         @args,
633     );
634 }
635
636 ## -------------------------------------------------
637
638 our $error_level;
639
640 sub throw_error {
641     my ( $self, @args ) = @_;
642     local $error_level = ($error_level || 0) + 1;
643     $self->raise_error($self->create_error(@args));
644 }
645
646 sub _inline_throw_error {
647     my ( $self, $msg, $args ) = @_;
648     "\$meta->throw_error($msg" . ($args ? ", $args" : "") . ")"; # FIXME makes deparsing *REALLY* hard
649 }
650
651 sub raise_error {
652     my ( $self, @args ) = @_;
653     die @args;
654 }
655
656 sub create_error {
657     my ( $self, @args ) = @_;
658
659     require Carp::Heavy;
660
661     local $error_level = ($error_level || 0 ) + 1;
662
663     if ( @args % 2 == 1 ) {
664         unshift @args, "message";
665     }
666
667     my %args = ( metaclass => $self, last_error => $@, @args );
668
669     $args{depth} += $error_level;
670
671     my $class = ref $self ? $self->error_class : "Moose::Error::Default";
672
673     Class::MOP::load_class($class);
674
675     $class->new(
676         Carp::caller_info($args{depth}),
677         %args
678     );
679 }
680
681 1;
682
683 # ABSTRACT: The Moose metaclass
684
685 __END__
686
687 =pod
688
689 =head1 DESCRIPTION
690
691 This class is a subclass of L<Class::MOP::Class> that provides
692 additional Moose-specific functionality.
693
694 To really understand this class, you will need to start with the
695 L<Class::MOP::Class> documentation. This class can be understood as a
696 set of additional features on top of the basic feature provided by
697 that parent class.
698
699 =head1 INHERITANCE
700
701 C<Moose::Meta::Class> is a subclass of L<Class::MOP::Class>.
702
703 =head1 METHODS
704
705 =over 4
706
707 =item B<< Moose::Meta::Class->initialize($package_name, %options) >>
708
709 This overrides the parent's method in order to provide its own
710 defaults for the C<attribute_metaclass>, C<instance_metaclass>, and
711 C<method_metaclass> options.
712
713 These all default to the appropriate Moose class.
714
715 =item B<< Moose::Meta::Class->create($package_name, %options) >>
716
717 This overrides the parent's method in order to accept a C<roles>
718 option. This should be an array reference containing roles
719 that the class does, each optionally followed by a hashref of options
720 (C<-excludes> and C<-alias>).
721
722   my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] );
723
724 =item B<< Moose::Meta::Class->create_anon_class >>
725
726 This overrides the parent's method to accept a C<roles> option, just
727 as C<create> does.
728
729 It also accepts a C<cache> option. If this is true, then the anonymous
730 class will be cached based on its superclasses and roles. If an
731 existing anonymous class in the cache has the same superclasses and
732 roles, it will be reused.
733
734   my $metaclass = Moose::Meta::Class->create_anon_class(
735       superclasses => ['Foo'],
736       roles        => [qw/Some Roles Go Here/],
737       cache        => 1,
738   );
739
740 Each entry in both the C<superclasses> and the C<roles> option can be
741 followed by a hash reference with arguments. The C<superclasses>
742 option can be supplied with a L<-version|Class::MOP/Class Loading
743 Options> option that ensures the loaded superclass satisfies the
744 required version. The C<role> option also takes the C<-version> as an
745 argument, but the option hash reference can also contain any other
746 role relevant values like exclusions or parameterized role arguments.
747
748 =item B<< $metaclass->make_immutable(%options) >>
749
750 This overrides the parent's method to add a few options. Specifically,
751 it uses the Moose-specific constructor and destructor classes, and
752 enables inlining the destructor.
753
754 Since Moose always inlines attributes, it sets the C<inline_accessors> option
755 to false.
756
757 =item B<< $metaclass->new_object(%params) >>
758
759 This overrides the parent's method in order to add support for
760 attribute triggers.
761
762 =item B<< $metaclass->superclasses(@superclasses) >>
763
764 This is the accessor allowing you to read or change the parents of
765 the class.
766
767 Each superclass can be followed by a hash reference containing a
768 L<-version|Class::MOP/Class Loading Options> value. If the version
769 requirement is not satisfied an error will be thrown.
770
771 =item B<< $metaclass->add_override_method_modifier($name, $sub) >>
772
773 This adds an C<override> method modifier to the package.
774
775 =item B<< $metaclass->add_augment_method_modifier($name, $sub) >>
776
777 This adds an C<augment> method modifier to the package.
778
779 =item B<< $metaclass->calculate_all_roles >>
780
781 This will return a unique array of C<Moose::Meta::Role> instances
782 which are attached to this class.
783
784 =item B<< $metaclass->calculate_all_roles_with_inheritance >>
785
786 This will return a unique array of C<Moose::Meta::Role> instances
787 which are attached to this class, and each of this class's ancestors.
788
789 =item B<< $metaclass->add_role($role) >>
790
791 This takes a L<Moose::Meta::Role> object, and adds it to the class's
792 list of roles. This I<does not> actually apply the role to the class.
793
794 =item B<< $metaclass->role_applications >>
795
796 Returns a list of L<Moose::Meta::Role::Application::ToClass>
797 objects, which contain the arguments to role application.
798
799 =item B<< $metaclass->add_role_application($application) >>
800
801 This takes a L<Moose::Meta::Role::Application::ToClass> object, and
802 adds it to the class's list of role applications. This I<does not>
803 actually apply any role to the class; it is only for tracking role
804 applications.
805
806 =item B<< $metaclass->does_role($role) >>
807
808 This returns a boolean indicating whether or not the class does the specified
809 role. The role provided can be either a role name or a L<Moose::Meta::Role>
810 object. This tests both the class and its parents.
811
812 =item B<< $metaclass->excludes_role($role_name) >>
813
814 A class excludes a role if it has already composed a role which
815 excludes the named role. This tests both the class and its parents.
816
817 =item B<< $metaclass->add_attribute($attr_name, %params|$params) >>
818
819 This overrides the parent's method in order to allow the parameters to
820 be provided as a hash reference.
821
822 =item B<< $metaclass->constructor_class($class_name) >>
823
824 =item B<< $metaclass->destructor_class($class_name) >>
825
826 These are the names of classes used when making a class immutable. These
827 default to L<Moose::Meta::Method::Constructor> and
828 L<Moose::Meta::Method::Destructor> respectively. These accessors are
829 read-write, so you can use them to change the class name.
830
831 =item B<< $metaclass->error_class($class_name) >>
832
833 The name of the class used to throw errors. This defaults to
834 L<Moose::Error::Default>, which generates an error with a stacktrace
835 just like C<Carp::confess>.
836
837 =item B<< $metaclass->throw_error($message, %extra) >>
838
839 Throws the error created by C<create_error> using C<raise_error>
840
841 =back
842
843 =head1 BUGS
844
845 See L<Moose/BUGS> for details on reporting bugs.
846
847 =cut
848