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