Make get_all_attributes() sorted by their definition order
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
CommitLineData
306290e8 1package Mouse::Meta::Class;
bc76ed1c 2use Mouse::Util qw/:meta/; # enables strict and warnings
c3398f5b 3
cecfb973 4use Scalar::Util qw/blessed weaken/;
6d28c5cf 5
6d28c5cf 6use Mouse::Meta::Module;
f3bb863f 7our @ISA = qw(Mouse::Meta::Module);
3a63a2e7 8
3b48bcc8 9our @CARP_NOT = qw(Mouse); # trust Mouse
10
e058b279 11sub attribute_metaclass;
d9659f80 12sub method_metaclass;
3a63a2e7 13
e058b279 14sub constructor_class;
15sub destructor_class;
380e1cd7 16
d9659f80 17
8e64d0fa 18sub _construct_meta {
88ed7189 19 my($class, %args) = @_;
c3398f5b 20
5132ec42 21 $args{attributes} = {};
22 $args{methods} = {};
23 $args{roles} = [];
8536d351 24
c3398f5b 25 $args{superclasses} = do {
26 no strict 'refs';
88ed7189 27 \@{ $args{package} . '::ISA' };
c3398f5b 28 };
29
8d40c3b8 30 my $self = bless \%args, ref($class) || $class;
7eb3a8d5 31 if(ref($self) ne __PACKAGE__){
9009aca1 32 $self->meta->_initialize_object($self, \%args);
8d40c3b8 33 }
34 return $self;
7a50b450 35}
36
37sub create_anon_class{
38 my $self = shift;
39 return $self->create(undef, @_);
40}
41
43165725 42sub is_anon_class;
c3398f5b 43
43165725 44sub roles;
c3398f5b 45
e7264861 46sub calculate_all_roles {
47 my $self = shift;
48 my %seen;
49 return grep { !$seen{ $_->name }++ }
50 map { $_->calculate_all_roles } @{ $self->roles };
51}
52
c3398f5b 53sub superclasses {
54 my $self = shift;
55
56 if (@_) {
9d0686b2 57 foreach my $super(@_){
58 Mouse::Util::load_class($super);
b6369395 59
902174eb 60 my $meta = Mouse::Util::get_metaclass_by_name($super);
61 unless(defined $meta) {
62 # checks if $super is a foreign class (i.e. non-Mouse class)
63 my $mm = $super->can('meta');
64 if(!($mm && $mm == \&Mouse::Util::meta)) {
65 if($super->can('new') or $super->can('DESTROY')) {
66 $self->inherit_from_foreign_class($super);
67 }
68 }
69 next;
70 }
b6369395 71
f48920c1 72 if(Mouse::Util::is_a_metarole($meta)){
9d0686b2 73 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
74 }
b6369395 75
902174eb 76 # checks and fixes in metaclass compatiility
b6369395 77 next if $self->isa(ref $meta); # _superclass_meta_is_compatible
b6369395 78 $self->_reconcile_with_superclass_meta($meta);
9d0686b2 79 }
c3398f5b 80 @{ $self->{superclasses} } = @_;
81 }
82
8e64d0fa 83 return @{ $self->{superclasses} };
84}
902174eb 85
86sub inherit_from_foreign_class {
87 my($class, $super) = @_;
88 Carp::carp("You inherit from non-Mouse class ($super),"
89 . " but it is unlikely to work correctly."
90 . " Please concider to use MouseX::Foreign");
91 return;
92}
93
c0b18afd 94my @MetaClassTypes = (
95 'attribute', # Mouse::Meta::Attribute
96 'method', # Mouse::Meta::Method
97 'constructor', # Mouse::Meta::Method::Constructor
98 'destructor', # Mouse::Meta::Method::Destructor
99);
8e64d0fa 100
b6369395 101sub _reconcile_with_superclass_meta {
c0b18afd 102 my($self, $other) = @_;
b6369395 103
733f404b 104 # find incompatible traits
c0b18afd 105 my %metaroles;
b6369395 106 foreach my $metaclass_type(@MetaClassTypes){
c0b18afd 107 my $accessor = $self->can($metaclass_type . '_metaclass')
108 || $self->can($metaclass_type . '_class');
b6369395 109
c0b18afd 110 my $other_c = $other->$accessor();
111 my $self_c = $self->$accessor();
b6369395 112
c0b18afd 113 if(!$self_c->isa($other_c)){
114 $metaroles{$metaclass_type}
115 = [ $self_c->meta->_collect_roles($other_c->meta) ];
d9659f80 116 }
117 }
118
c0b18afd 119 $metaroles{class} = [$self->meta->_collect_roles($other->meta)];
120
121 #use Data::Dumper; print Data::Dumper->new([\%metaroles], ['*metaroles'])->Indent(1)->Dump;
d9659f80 122
123 require Mouse::Util::MetaRole;
c0b18afd 124 $_[0] = Mouse::Util::MetaRole::apply_metaroles(
125 for => $self,
126 class_metaroles => \%metaroles,
d9659f80 127 );
b6369395 128 return;
129}
130
c0b18afd 131sub _collect_roles {
132 my ($self, $other) = @_;
133
134 # find common ancestor
135 my @self_lin_isa = $self->linearized_isa;
136 my @other_lin_isa = $other->linearized_isa;
137
138 my(@self_anon_supers, @other_anon_supers);
139 push @self_anon_supers, shift @self_lin_isa while $self_lin_isa[0]->meta->is_anon_class;
140 push @other_anon_supers, shift @other_lin_isa while $other_lin_isa[0]->meta->is_anon_class;
141
142 my $common_ancestor = $self_lin_isa[0] eq $other_lin_isa[0] && $self_lin_isa[0];
143
144 if(!$common_ancestor){
145 $self->throw_error(sprintf '%s cannot have %s as a super class because of their metaclass incompatibility',
146 $self->name, $other->name);
147 }
148
149 my %seen;
a4b15169 150 return sort grep { !$seen{$_}++ } ## no critic
c0b18afd 151 (map{ $_->name } map{ $_->meta->calculate_all_roles } @self_anon_supers),
152 (map{ $_->name } map{ $_->meta->calculate_all_roles } @other_anon_supers),
153 ;
154}
155
156
8e64d0fa 157sub find_method_by_name{
158 my($self, $method_name) = @_;
159 defined($method_name)
160 or $self->throw_error('You must define a method name to find');
0126c27c 161
8e64d0fa 162 foreach my $class( $self->linearized_isa ){
163 my $method = $self->initialize($class)->get_method($method_name);
164 return $method if defined $method;
165 }
166 return undef;
167}
168
169sub get_all_methods {
170 my($self) = @_;
612d3e1a 171 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
c3398f5b 172}
173
60cfc6ad 174sub get_all_method_names {
175 my $self = shift;
176 my %uniq;
177 return grep { $uniq{$_}++ == 0 }
3a63a2e7 178 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
60cfc6ad 179 $self->linearized_isa;
180}
181
abbcd124 182sub find_attribute_by_name {
2b908b79 183 my($self, $name) = @_;
abbcd124 184 foreach my $attr($self->get_all_attributes) {
185 return $attr if $attr->name eq $name;
2b908b79 186 }
abbcd124 187 return undef;
2b908b79 188}
189
87ca293b 190sub add_attribute {
c3398f5b 191 my $self = shift;
60f6eba9 192
87ca293b 193 my($attr, $name);
1b9e472d 194
87ca293b 195 if(blessed $_[0]){
196 $attr = $_[0];
1b9e472d 197
87ca293b 198 $attr->isa('Mouse::Meta::Attribute')
199 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
1b9e472d 200
87ca293b 201 $name = $attr->name;
1b9e472d 202 }
203 else{
87ca293b 204 # _process_attribute
205 $name = shift;
1b9e472d 206
87ca293b 207 my %args = (@_ == 1) ? %{$_[0]} : @_;
1b9e472d 208
87ca293b 209 defined($name)
210 or $self->throw_error('You must provide a name for the attribute');
1b9e472d 211
87ca293b 212 if ($name =~ s/^\+//) { # inherited attributes
2b908b79 213 my $inherited_attr = $self->find_attribute_by_name($name)
87ca293b 214 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
215
8cf51b82 216 $attr = $inherited_attr->clone_and_inherit_options(%args);
87ca293b 217 }
218 else{
8cf51b82 219 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
87ca293b 220 $args{traits} = \@traits if @traits;
221
2a464664 222 $attr = $attribute_class->new($name, %args);
87ca293b 223 }
224 }
1b9e472d 225
226 weaken( $attr->{associated_class} = $self );
227
df7e4729 228 # install accessors first
229 $attr->install_accessors();
230
231 # then register the attribute to the metaclass
abbcd124 232 $attr->{insertion_order} = keys %{ $self->{attributes} };
233 $self->{attributes}{$name} = $attr;
234 delete $self->{_mouse_cache}; # clears internal cache
1b9e472d 235
3b48bcc8 236 if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
237 Carp::carp(qq{Attribute ($name) of class }.$self->name
238 .qq{ has no associated methods (did you mean to provide an "is" argument?)});
60f6eba9 239 }
abbcd124 240 return $attr;
241}
74de0f8b 242
abbcd124 243sub _calculate_all_attributes {
244 my($self) = @_;
245 my %seen;
246 my @all_attrs;
247 foreach my $class($self->linearized_isa) {
248 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
249 my @attrs = grep { !$seen{$_->name}++ } values %{$meta->{attributes}};
250 @attrs = sort {
251 $b->{insertion_order} <=> $a->{insertion_order}
252 } @attrs;
253 push @all_attrs, @attrs;
a3a09e38 254 }
abbcd124 255 return [reverse @all_attrs];
c3398f5b 256}
257
cccb83de 258sub linearized_isa;
8536d351 259
ba153b33 260sub new_object;
13b99908 261sub clone_object;
1b9e472d 262
cfef75f0 263sub immutable_options {
264 my ( $self, @args ) = @_;
265
266 return (
7efbc77d 267 inline_constructor => 1,
e578d610 268 inline_destructor => 1,
2a464664 269 constructor_name => 'new',
cfef75f0 270 @args,
6a1d1835 271 );
cfef75f0 272}
273
cfef75f0 274sub make_immutable {
275 my $self = shift;
276 my %args = $self->immutable_options(@_);
6a1d1835 277
fc1d8369 278 $self->{is_immutable}++;
c7a6403f 279
6a1d1835 280 if ($args{inline_constructor}) {
380e1cd7 281 $self->add_method($args{constructor_name} =>
637d4f17 282 Mouse::Util::load_class($self->constructor_class)
283 ->_generate_constructor($self, \%args));
c7a6403f 284 }
285
8632b6fe 286 if ($args{inline_destructor}) {
380e1cd7 287 $self->add_method(DESTROY =>
637d4f17 288 Mouse::Util::load_class($self->destructor_class)
289 ->_generate_destructor($self, \%args));
8632b6fe 290 }
2276cb14 291
89681b0b 292 # Moose's make_immutable returns true allowing calling code to skip
293 # setting an explicit true value at the end of a source file.
2276cb14 294 return 1;
fc1d8369 295}
ad958001 296
32490a72 297sub make_mutable {
298 my($self) = @_;
299 $self->{is_immutable} = 0;
300 return;
301}
ad958001 302
80efe911 303sub is_immutable;
304sub is_mutable { !$_[0]->is_immutable }
84ef660f 305
8d5287a7 306sub _install_modifier {
8d59c723 307 my( $self, $type, $name, $code ) = @_;
308 my $into = $self->name;
3fbade18 309
310 my $original = $into->can($name)
083e49a5 311 or $self->throw_error("The method '$name' was not found in the inheritance hierarchy for $into");
3fbade18 312
313 my $modifier_table = $self->{modifiers}{$name};
314
315 if(!$modifier_table){
d686eec5 316 my(@before, @after, @around);
317 my $cache = $original;
1b490891 318 my $modified = sub {
d686eec5 319 if(@before) {
320 for my $c (@before) { $c->(@_) }
321 }
322 unless(@after) {
1b490891 323 return $cache->(@_);
324 }
325
3fbade18 326 if(wantarray){ # list context
327 my @rval = $cache->(@_);
328
329 for my $c(@after){ $c->(@_) }
330 return @rval;
331 }
332 elsif(defined wantarray){ # scalar context
333 my $rval = $cache->(@_);
334
335 for my $c(@after){ $c->(@_) }
336 return $rval;
337 }
338 else{ # void context
339 $cache->(@_);
340
341 for my $c(@after){ $c->(@_) }
342 return;
343 }
344 };
345
346 $self->{modifiers}{$name} = $modifier_table = {
347 original => $original,
348
349 before => \@before,
350 after => \@after,
d686eec5 351 around => \@around,
3fbade18 352
353 cache => \$cache, # cache for around modifiers
354 };
355
356 $self->add_method($name => $modified);
357 }
358
359 if($type eq 'before'){
360 unshift @{$modifier_table->{before}}, $code;
361 }
362 elsif($type eq 'after'){
363 push @{$modifier_table->{after}}, $code;
364 }
365 else{ # around
366 push @{$modifier_table->{around}}, $code;
367
368 my $next = ${ $modifier_table->{cache} };
369 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
370 }
371
372 return;
373}
374
50dc6ee5 375sub add_before_method_modifier {
4859d490 376 my ( $self, $name, $code ) = @_;
8d59c723 377 $self->_install_modifier( 'before', $name, $code );
50dc6ee5 378}
379
380sub add_around_method_modifier {
4859d490 381 my ( $self, $name, $code ) = @_;
8d59c723 382 $self->_install_modifier( 'around', $name, $code );
50dc6ee5 383}
384
385sub add_after_method_modifier {
4859d490 386 my ( $self, $name, $code ) = @_;
8d59c723 387 $self->_install_modifier( 'after', $name, $code );
50dc6ee5 388}
389
67199842 390sub add_override_method_modifier {
391 my ($self, $name, $code) = @_;
392
768804c0 393 if($self->has_method($name)){
394 $self->throw_error("Cannot add an override method if a local method is already present");
85bd3f44 395 }
396
6cfa1e5e 397 my $package = $self->name;
67199842 398
85bd3f44 399 my $super_body = $package->can($name)
6cfa1e5e 400 or $self->throw_error("You cannot override '$name' because it has no super method");
67199842 401
85bd3f44 402 $self->add_method($name => sub {
403 local $Mouse::SUPER_PACKAGE = $package;
404 local $Mouse::SUPER_BODY = $super_body;
405 local @Mouse::SUPER_ARGS = @_;
406
407 $code->(@_);
408 });
409 return;
67199842 410}
411
768804c0 412sub add_augment_method_modifier {
413 my ($self, $name, $code) = @_;
414 if($self->has_method($name)){
415 $self->throw_error("Cannot add an augment method if a local method is already present");
416 }
417
418 my $super = $self->find_method_by_name($name)
419 or $self->throw_error("You cannot augment '$name' because it has no super method");
420
421 my $super_package = $super->package_name;
422 my $super_body = $super->body;
423
424 $self->add_method($name => sub{
425 local $Mouse::INNER_BODY{$super_package} = $code;
426 local $Mouse::INNER_ARGS{$super_package} = [@_];
427 $super_body->(@_);
428 });
429 return;
430}
431
47f36c05 432sub does_role {
433 my ($self, $role_name) = @_;
ad958001 434
47f36c05 435 (defined $role_name)
fce211ae 436 || $self->throw_error("You must supply a role name to look for");
ad958001 437
f3e11122 438 $role_name = $role_name->name if ref $role_name;
439
f7fec86c 440 for my $class ($self->linearized_isa) {
95ecd6f1 441 my $meta = Mouse::Util::get_metaclass_by_name($class)
442 or next;
3a63a2e7 443
444 for my $role (@{ $meta->roles }) {
ff687069 445
3a63a2e7 446 return 1 if $role->does_role($role_name);
f7fec86c 447 }
47f36c05 448 }
ad958001 449
47f36c05 450 return 0;
451}
452
c3398f5b 4531;
c3398f5b 454__END__
455
456=head1 NAME
457
1820fffe 458Mouse::Meta::Class - The Mouse class metaclass
c3398f5b 459
a25ca8d6 460=head1 VERSION
461
b0d52f03 462This document describes Mouse version 0.73
a25ca8d6 463
503ed648 464=head1 DESCRIPTION
465
466This class is a meta object protocol for Mouse classes,
467which is a subset of Moose::Meta:::Class.
468
c3398f5b 469=head1 METHODS
470
612d3e1a 471=head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
c3398f5b 472
31c5194b 473Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
306290e8 474one instance should exist for a given class.
c3398f5b 475
612d3e1a 476=head2 C<< name -> ClassName >>
c3398f5b 477
478Returns the name of the owner class.
479
612d3e1a 480=head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
c3398f5b 481
482Gets (or sets) the list of superclasses of the owner class.
483
31c5194b 484=head2 C<< add_method(name => CodeRef) >>
c3398f5b 485
31c5194b 486Adds a method to the owner class.
c3398f5b 487
31c5194b 488=head2 C<< has_method(name) -> Bool >>
72b88a88 489
31c5194b 490Returns whether we have a method with the given name.
72b88a88 491
31c5194b 492=head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
c68b4110 493
31c5194b 494Returns a L<Mouse::Meta::Method> with the given name.
495
496Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
497
498=head2 C<< get_method_list -> Names >>
499
500Returns a list of method names which are defined in the local class.
501If you want a list of all applicable methods for a class, use the
502C<get_all_methods> method.
503
504=head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
505
506Return the list of all L<Mouse::Meta::Method> instances associated with
507the class and its superclasses.
508
509=head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
510
511Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
512class.
c68b4110 513
612d3e1a 514=head2 C<< has_attribute(Name) -> Bool >>
66eea168 515
516Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
517
31c5194b 518=head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
c3398f5b 519
306290e8 520Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 521
31c5194b 522=head2 C<< get_attribute_list -> Names >>
523
524Returns a list of attribute names which are defined in the local
525class. If you want a list of all applicable attributes for a class,
526use the C<get_all_attributes> method.
527
528=head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
529
530Returns the list of all L<Mouse::Meta::Attribute> instances associated with
531this class and its superclasses.
532
612d3e1a 533=head2 C<< linearized_isa -> [ClassNames] >>
c3398f5b 534
535Returns the list of classes in method dispatch order, with duplicates removed.
536
612d3e1a 537=head2 C<< new_object(Parameters) -> Instance >>
2cea7a5f 538
612d3e1a 539Creates a new instance.
2cea7a5f 540
612d3e1a 541=head2 C<< clone_object(Instance, Parameters) -> Instance >>
f7b11a21 542
31c5194b 543Clones the given instance which must be an instance governed by this
f7b11a21 544metaclass.
545
31c5194b 546=head2 C<< throw_error(Message, Parameters) >>
547
548Throws an error with the given message.
549
1820fffe 550=head1 SEE ALSO
f7b11a21 551
6b82419e 552L<Mouse::Meta::Module>
553
1820fffe 554L<Moose::Meta::Class>
f7b11a21 555
31c5194b 556L<Class::MOP::Class>
557
c3398f5b 558=cut
559