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