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