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