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