Commit | Line | Data |
306290e8 |
1 | package Mouse::Meta::Class; |
c3398f5b |
2 | use strict; |
3 | use warnings; |
4 | |
fc1d8369 |
5 | use Mouse::Meta::Method::Constructor; |
bbf64e76 |
6 | use Mouse::Meta::Method::Destructor; |
cecfb973 |
7 | use Scalar::Util qw/blessed weaken/; |
fce211ae |
8 | use Mouse::Util qw/get_linear_isa not_supported/; |
72b88a88 |
9 | |
3a63a2e7 |
10 | use base qw(Mouse::Meta::Module); |
11 | |
6cfa1e5e |
12 | sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method() |
3a63a2e7 |
13 | |
8536d351 |
14 | sub _new { |
88ed7189 |
15 | my($class, %args) = @_; |
c3398f5b |
16 | |
8536d351 |
17 | $args{attributes} ||= {}; |
18 | $args{methods} ||= {}; |
19 | $args{roles} ||= []; |
20 | |
c3398f5b |
21 | $args{superclasses} = do { |
22 | no strict 'refs'; |
88ed7189 |
23 | \@{ $args{package} . '::ISA' }; |
c3398f5b |
24 | }; |
25 | |
26 | bless \%args, $class; |
27 | } |
28 | |
afc73948 |
29 | sub roles { $_[0]->{roles} } |
c3398f5b |
30 | |
31 | sub superclasses { |
32 | my $self = shift; |
33 | |
34 | if (@_) { |
35 | Mouse::load_class($_) for @_; |
36 | @{ $self->{superclasses} } = @_; |
37 | } |
38 | |
39 | @{ $self->{superclasses} }; |
40 | } |
41 | |
60cfc6ad |
42 | sub get_all_method_names { |
43 | my $self = shift; |
44 | my %uniq; |
45 | return grep { $uniq{$_}++ == 0 } |
3a63a2e7 |
46 | map { Mouse::Meta::Class->initialize($_)->get_method_list() } |
60cfc6ad |
47 | $self->linearized_isa; |
48 | } |
49 | |
c3398f5b |
50 | sub add_attribute { |
51 | my $self = shift; |
c3398f5b |
52 | |
60f6eba9 |
53 | if (@_ == 1 && blessed($_[0])) { |
54 | my $attr = shift @_; |
55 | $self->{'attributes'}{$attr->name} = $attr; |
56 | } else { |
57 | my $names = shift @_; |
58 | $names = [$names] if !ref($names); |
59 | my $metaclass = 'Mouse::Meta::Attribute'; |
60 | my %options = @_; |
61 | |
62 | if ( my $metaclass_name = delete $options{metaclass} ) { |
63 | my $new_class = Mouse::Util::resolve_metaclass_alias( |
64 | 'Attribute', |
65 | $metaclass_name |
66 | ); |
67 | if ( $metaclass ne $new_class ) { |
68 | $metaclass = $new_class; |
69 | } |
70 | } |
71 | |
72 | for my $name (@$names) { |
73 | if ($name =~ s/^\+//) { |
74 | $metaclass->clone_parent($self, $name, @_); |
75 | } |
76 | else { |
77 | $metaclass->create($self, $name, @_); |
78 | } |
79 | } |
80 | } |
c3398f5b |
81 | } |
82 | |
d60824af |
83 | sub compute_all_applicable_attributes { shift->get_all_attributes(@_) } |
84 | sub get_all_attributes { |
72b88a88 |
85 | my $self = shift; |
86 | my (@attr, %seen); |
87 | |
88 | for my $class ($self->linearized_isa) { |
89 | my $meta = $self->_metaclass_cache($class) |
90 | or next; |
91 | |
92 | for my $name (keys %{ $meta->get_attribute_map }) { |
93 | next if $seen{$name}++; |
94 | push @attr, $meta->get_attribute($name); |
95 | } |
96 | } |
97 | |
98 | return @attr; |
99 | } |
100 | |
8536d351 |
101 | sub linearized_isa { @{ get_linear_isa($_[0]->name) } } |
102 | |
103 | sub new_object { |
c68b4110 |
104 | my $self = shift; |
8536d351 |
105 | my $args = (@_ == 1) ? $_[0] : { @_ }; |
c3398f5b |
106 | |
fce211ae |
107 | my $instance = bless {}, $self->name; |
108 | |
109 | foreach my $attribute ($self->get_all_attributes) { |
8536d351 |
110 | my $from = $attribute->init_arg; |
111 | my $key = $attribute->name; |
112 | |
113 | if (defined($from) && exists($args->{$from})) { |
114 | $args->{$from} = $attribute->coerce_constraint($args->{$from}) |
115 | if $attribute->should_coerce; |
116 | $attribute->verify_against_type_constraint($args->{$from}); |
117 | |
118 | $instance->{$key} = $args->{$from}; |
119 | |
120 | weaken($instance->{$key}) |
fce211ae |
121 | if ref($instance->{$key}) && $attribute->is_weak_ref; |
8536d351 |
122 | |
123 | if ($attribute->has_trigger) { |
124 | $attribute->trigger->($instance, $args->{$from}); |
125 | } |
126 | } |
127 | else { |
128 | if ($attribute->has_default || $attribute->has_builder) { |
129 | unless ($attribute->is_lazy) { |
130 | my $default = $attribute->default; |
131 | my $builder = $attribute->builder; |
132 | my $value = $attribute->has_builder |
133 | ? $instance->$builder |
134 | : ref($default) eq 'CODE' |
135 | ? $default->($instance) |
136 | : $default; |
137 | |
138 | $value = $attribute->coerce_constraint($value) |
139 | if $attribute->should_coerce; |
140 | $attribute->verify_against_type_constraint($value); |
141 | |
142 | $instance->{$key} = $value; |
143 | |
144 | weaken($instance->{$key}) |
fce211ae |
145 | if ref($instance->{$key}) && $attribute->is_weak_ref; |
8536d351 |
146 | } |
147 | } |
148 | else { |
149 | if ($attribute->is_required) { |
fce211ae |
150 | $self->throw_error("Attribute (".$attribute->name.") is required"); |
8536d351 |
151 | } |
152 | } |
153 | } |
154 | } |
155 | return $instance; |
156 | } |
c3398f5b |
157 | |
7a59f4e8 |
158 | sub clone_object { |
159 | my $class = shift; |
160 | my $instance = shift; |
161 | |
162 | (blessed($instance) && $instance->isa($class->name)) |
fce211ae |
163 | || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)"); |
7a59f4e8 |
164 | |
165 | $class->clone_instance($instance, @_); |
166 | } |
167 | |
168 | sub clone_instance { |
169 | my ($class, $instance, %params) = @_; |
170 | |
171 | (blessed($instance)) |
fce211ae |
172 | || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance"); |
7a59f4e8 |
173 | |
174 | my $clone = bless { %$instance }, ref $instance; |
175 | |
d60824af |
176 | foreach my $attr ($class->get_all_attributes()) { |
7a59f4e8 |
177 | if ( defined( my $init_arg = $attr->init_arg ) ) { |
178 | if (exists $params{$init_arg}) { |
179 | $clone->{ $attr->name } = $params{$init_arg}; |
180 | } |
181 | } |
182 | } |
183 | |
184 | return $clone; |
185 | |
186 | } |
187 | |
fc1d8369 |
188 | sub make_immutable { |
189 | my $self = shift; |
6a1d1835 |
190 | my %args = ( |
191 | inline_constructor => 1, |
e578d610 |
192 | inline_destructor => 1, |
6a1d1835 |
193 | @_, |
194 | ); |
195 | |
fc1d8369 |
196 | $self->{is_immutable}++; |
c7a6403f |
197 | |
6a1d1835 |
198 | if ($args{inline_constructor}) { |
c7a6403f |
199 | $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self )); |
200 | } |
201 | |
8632b6fe |
202 | if ($args{inline_destructor}) { |
203 | $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self )); |
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 | |
4859d490 |
216 | sub _install_modifier { |
217 | my ( $self, $into, $type, $name, $code ) = @_; |
4f5b44a0 |
218 | |
219 | # which is modifer class available? |
220 | my $modifier_class = do { |
221 | if (eval "require Class::Method::Modifiers::Fast; 1") { |
222 | 'Class::Method::Modifiers::Fast'; |
223 | } elsif (eval "require Class::Method::Modifiers; 1") { |
224 | 'Class::Method::Modifiers'; |
225 | } else { |
226 | Carp::croak("Method modifiers require the use of Class::Method::Modifiers or Class::Method::Modifiers::Fast. Please install it from CPAN and file a bug report with this application."); |
227 | } |
228 | }; |
229 | my $modifier = $modifier_class->can('_install_modifier'); |
230 | |
231 | # replace this method itself :) |
232 | { |
4f5b44a0 |
233 | no warnings 'redefine'; |
4f9945f5 |
234 | *_install_modifier = sub { |
4f5b44a0 |
235 | my ( $self, $into, $type, $name, $code ) = @_; |
236 | $modifier->( |
237 | $into, |
238 | $type, |
239 | $name, |
240 | $code |
241 | ); |
6cfa1e5e |
242 | $self->{methods}{$name}++; # register it to the method map |
243 | return; |
4f5b44a0 |
244 | }; |
1b79a118 |
245 | } |
4f5b44a0 |
246 | |
247 | # call me. for first time. |
248 | $self->_install_modifier( $into, $type, $name, $code ); |
4859d490 |
249 | } |
250 | |
50dc6ee5 |
251 | sub add_before_method_modifier { |
4859d490 |
252 | my ( $self, $name, $code ) = @_; |
253 | $self->_install_modifier( $self->name, 'before', $name, $code ); |
50dc6ee5 |
254 | } |
255 | |
256 | sub add_around_method_modifier { |
4859d490 |
257 | my ( $self, $name, $code ) = @_; |
258 | $self->_install_modifier( $self->name, 'around', $name, $code ); |
50dc6ee5 |
259 | } |
260 | |
261 | sub add_after_method_modifier { |
4859d490 |
262 | my ( $self, $name, $code ) = @_; |
263 | $self->_install_modifier( $self->name, 'after', $name, $code ); |
50dc6ee5 |
264 | } |
265 | |
67199842 |
266 | sub add_override_method_modifier { |
267 | my ($self, $name, $code) = @_; |
268 | |
6cfa1e5e |
269 | my $package = $self->name; |
67199842 |
270 | |
6cfa1e5e |
271 | my $body = $package->can($name) |
272 | or $self->throw_error("You cannot override '$name' because it has no super method"); |
67199842 |
273 | |
6cfa1e5e |
274 | $self->add_method($name => sub { $code->($package, $body, @_) }); |
67199842 |
275 | } |
276 | |
47f36c05 |
277 | sub does_role { |
278 | my ($self, $role_name) = @_; |
ad958001 |
279 | |
47f36c05 |
280 | (defined $role_name) |
fce211ae |
281 | || $self->throw_error("You must supply a role name to look for"); |
ad958001 |
282 | |
f7fec86c |
283 | for my $class ($self->linearized_isa) { |
08f7a8db |
284 | my $meta = Mouse::Meta::Module::class_of($class); |
3a63a2e7 |
285 | next unless $meta && $meta->can('roles'); |
286 | |
287 | for my $role (@{ $meta->roles }) { |
ff687069 |
288 | |
3a63a2e7 |
289 | return 1 if $role->does_role($role_name); |
f7fec86c |
290 | } |
47f36c05 |
291 | } |
ad958001 |
292 | |
47f36c05 |
293 | return 0; |
294 | } |
295 | |
ae3edb8a |
296 | sub create { |
88ed7189 |
297 | my ($class, $package_name, %options) = @_; |
ae3edb8a |
298 | |
299 | (ref $options{superclasses} eq 'ARRAY') |
fce211ae |
300 | || $class->throw_error("You must pass an ARRAY ref of superclasses") |
ae3edb8a |
301 | if exists $options{superclasses}; |
ad958001 |
302 | |
ae3edb8a |
303 | (ref $options{attributes} eq 'ARRAY') |
fce211ae |
304 | || $class->throw_error("You must pass an ARRAY ref of attributes") |
ad958001 |
305 | if exists $options{attributes}; |
306 | |
ae3edb8a |
307 | (ref $options{methods} eq 'HASH') |
fce211ae |
308 | || $class->throw_error("You must pass a HASH ref of methods") |
ad958001 |
309 | if exists $options{methods}; |
ae3edb8a |
310 | |
ff687069 |
311 | (ref $options{roles} eq 'ARRAY') |
312 | || $class->throw_error("You must pass an ARRAY ref of roles") |
313 | if exists $options{roles}; |
314 | |
ad022aac |
315 | { |
ae3edb8a |
316 | ( defined $package_name && $package_name ) |
fce211ae |
317 | || $class->throw_error("You must pass a package name"); |
ae3edb8a |
318 | |
ad022aac |
319 | no strict 'refs'; |
320 | ${ $package_name . '::VERSION' } = $options{version} if exists $options{version}; |
321 | ${ $package_name . '::AUTHORITY' } = $options{authority} if exists $options{authority}; |
322 | } |
ae3edb8a |
323 | |
dd3c04d7 |
324 | my %initialize_options = %options; |
325 | delete @initialize_options{qw( |
ae3edb8a |
326 | package |
327 | superclasses |
328 | attributes |
329 | methods |
ff687069 |
330 | roles |
ae3edb8a |
331 | version |
332 | authority |
333 | )}; |
88ed7189 |
334 | my $meta = $class->initialize( $package_name => %initialize_options ); |
ae3edb8a |
335 | |
336 | # FIXME totally lame |
337 | $meta->add_method('meta' => sub { |
88ed7189 |
338 | Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]); |
ae3edb8a |
339 | }); |
340 | |
341 | $meta->superclasses(@{$options{superclasses}}) |
342 | if exists $options{superclasses}; |
343 | # NOTE: |
344 | # process attributes first, so that they can |
345 | # install accessors, but locally defined methods |
346 | # can then overwrite them. It is maybe a little odd, but |
347 | # I think this should be the order of things. |
348 | if (exists $options{attributes}) { |
349 | foreach my $attr (@{$options{attributes}}) { |
350 | Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr); |
351 | } |
352 | } |
353 | if (exists $options{methods}) { |
354 | foreach my $method_name (keys %{$options{methods}}) { |
355 | $meta->add_method($method_name, $options{methods}->{$method_name}); |
356 | } |
357 | } |
ff687069 |
358 | if (exists $options{roles}){ |
359 | Mouse::Util::apply_all_roles($package_name, @{$options{roles}}); |
360 | } |
ae3edb8a |
361 | return $meta; |
362 | } |
363 | |
364 | { |
365 | my $ANON_CLASS_SERIAL = 0; |
366 | my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::'; |
ad022aac |
367 | |
368 | my %IMMORTAL_ANON_CLASSES; |
ae3edb8a |
369 | sub create_anon_class { |
370 | my ( $class, %options ) = @_; |
ad022aac |
371 | |
372 | my $cache = $options{cache}; |
373 | my $cache_key; |
374 | |
375 | if($cache){ # anonymous but not mortal |
376 | # something like Super::Class|Super::Class::2=Role|Role::1\r |
377 | $cache_key = join '=' => (\r |
378 | join('|', @{$options{superclasses} || []}),\r |
379 | join('|', sort @{$options{roles} || []}),\r |
380 | ); |
381 | return $IMMORTAL_ANON_CLASSES{$cache_key} if exists $IMMORTAL_ANON_CLASSES{$cache_key}; |
382 | } |
ae3edb8a |
383 | my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL; |
ad022aac |
384 | my $meta = $class->create( $package_name, anon_class_id => $ANON_CLASS_SERIAL, %options ); |
385 | |
386 | if($cache){ |
387 | $IMMORTAL_ANON_CLASSES{$cache_key} = $meta; |
388 | } |
389 | else{ |
390 | Mouse::Meta::Module::weaken_metaclass($package_name); |
391 | } |
392 | return $meta; |
ae3edb8a |
393 | } |
ad022aac |
394 | |
395 | sub is_anon_class{ |
396 | return exists $_[0]->{anon_class_id}; |
397 | } |
398 | |
399 | |
400 | sub DESTROY{ |
401 | my($self) = @_; |
402 | |
403 | my $serial_id = $self->{anon_class_id}; |
404 | |
405 | return if !$serial_id; |
406 | |
407 | my $stash = $self->namespace; |
408 | |
409 | @{$self->{sperclasses}} = (); |
410 | %{$stash} = (); |
411 | Mouse::Meta::Module::remove_metaclass_by_name($self->name); |
412 | |
413 | no strict 'refs'; |
414 | delete ${$ANON_CLASS_PREFIX}{ $serial_id . '::' }; |
415 | |
416 | return; |
417 | } |
418 | |
ae3edb8a |
419 | } |
420 | |
c3398f5b |
421 | 1; |
422 | |
423 | __END__ |
424 | |
425 | =head1 NAME |
426 | |
306290e8 |
427 | Mouse::Meta::Class - hook into the Mouse MOP |
c3398f5b |
428 | |
429 | =head1 METHODS |
430 | |
306290e8 |
431 | =head2 initialize ClassName -> Mouse::Meta::Class |
c3398f5b |
432 | |
306290e8 |
433 | Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only |
434 | one instance should exist for a given class. |
c3398f5b |
435 | |
306290e8 |
436 | =head2 new %args -> Mouse::Meta::Class |
c3398f5b |
437 | |
306290e8 |
438 | Creates a new Mouse::Meta::Class. Don't call this directly. |
c3398f5b |
439 | |
440 | =head2 name -> ClassName |
441 | |
442 | Returns the name of the owner class. |
443 | |
444 | =head2 superclasses -> [ClassName] |
445 | |
446 | Gets (or sets) the list of superclasses of the owner class. |
447 | |
60f6eba9 |
448 | =head2 add_attribute (Mouse::Meta::Attribute| name => spec) |
c3398f5b |
449 | |
306290e8 |
450 | Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner |
451 | class. |
c3398f5b |
452 | |
d60824af |
453 | =head2 get_all_attributes -> (Mouse::Meta::Attribute) |
72b88a88 |
454 | |
455 | Returns the list of all L<Mouse::Meta::Attribute> instances associated with |
456 | this class and its superclasses. |
457 | |
306290e8 |
458 | =head2 get_attribute_map -> { name => Mouse::Meta::Attribute } |
c3398f5b |
459 | |
460 | Returns a mapping of attribute names to their corresponding |
306290e8 |
461 | L<Mouse::Meta::Attribute> objects. |
c3398f5b |
462 | |
c68b4110 |
463 | =head2 get_attribute_list -> { name => Mouse::Meta::Attribute } |
464 | |
465 | This returns a list of attribute names which are defined in the local |
466 | class. If you want a list of all applicable attributes for a class, |
d60824af |
467 | use the C<get_all_attributes> method. |
c68b4110 |
468 | |
cbc437f2 |
469 | =head2 has_attribute Name -> Bool |
66eea168 |
470 | |
471 | Returns whether we have a L<Mouse::Meta::Attribute> with the given name. |
472 | |
306290e8 |
473 | =head2 get_attribute Name -> Mouse::Meta::Attribute | undef |
c3398f5b |
474 | |
306290e8 |
475 | Returns the L<Mouse::Meta::Attribute> with the given name. |
c3398f5b |
476 | |
477 | =head2 linearized_isa -> [ClassNames] |
478 | |
479 | Returns the list of classes in method dispatch order, with duplicates removed. |
480 | |
f7b11a21 |
481 | =head2 clone_object Instance -> Instance |
482 | |
483 | Clones the given C<Instance> which must be an instance governed by this |
484 | metaclass. |
485 | |
486 | =head2 clone_instance Instance, Parameters -> Instance |
487 | |
518e303a |
488 | The clone_instance method has been made private. |
489 | The public version is deprecated. |
f7b11a21 |
490 | |
c3398f5b |
491 | =cut |
492 | |