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 | |
fce211ae |
7 | use Mouse::Util qw/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; |
12 | |
3a63a2e7 |
13 | use base qw(Mouse::Meta::Module); |
14 | |
6cfa1e5e |
15 | sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method() |
3a63a2e7 |
16 | |
8e64d0fa |
17 | sub _construct_meta { |
88ed7189 |
18 | my($class, %args) = @_; |
c3398f5b |
19 | |
8536d351 |
20 | $args{attributes} ||= {}; |
21 | $args{methods} ||= {}; |
22 | $args{roles} ||= []; |
23 | |
c3398f5b |
24 | $args{superclasses} = do { |
25 | no strict 'refs'; |
88ed7189 |
26 | \@{ $args{package} . '::ISA' }; |
c3398f5b |
27 | }; |
28 | |
7a50b450 |
29 | #return Mouse::Meta::Class->initialize($class)->new_object(%args) |
30 | # if $class ne __PACKAGE__; |
31 | |
8e64d0fa |
32 | return bless \%args, ref($class) || $class; |
7a50b450 |
33 | } |
34 | |
35 | sub create_anon_class{ |
36 | my $self = shift; |
37 | return $self->create(undef, @_); |
38 | } |
39 | |
40 | sub is_anon_class{ |
41 | return exists $_[0]->{anon_serial_id}; |
c3398f5b |
42 | } |
43 | |
afc73948 |
44 | sub roles { $_[0]->{roles} } |
c3398f5b |
45 | |
46 | sub 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 | |
57 | sub 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 | |
68 | sub get_all_methods { |
69 | my($self) = @_; |
70 | return map{ $self->find_method_by_name($self) } $self->get_all_method_names; |
c3398f5b |
71 | } |
72 | |
60cfc6ad |
73 | sub 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 | |
1b9e472d |
81 | sub _process_attribute{ |
c3398f5b |
82 | my $self = shift; |
1b9e472d |
83 | my $name = shift; |
c3398f5b |
84 | |
1b9e472d |
85 | my $args = (@_ == 1) ? $_[0] : { @_ }; |
60f6eba9 |
86 | |
1b9e472d |
87 | defined($name) |
88 | or $self->throw_error('You must provide a name for the attribute'); |
89 | |
90 | if ($name =~ s/^\+//) { |
91 | my $inherited_attr; |
92 | |
93 | foreach my $class($self->linearized_isa){ |
94 | my $meta = Mouse::Meta::Module::get_metaclass_by_name($class) or next; |
95 | $inherited_attr = $meta->get_attribute($name) and last; |
60f6eba9 |
96 | } |
1b9e472d |
97 | |
98 | defined($inherited_attr) |
99 | or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name); |
100 | |
101 | return $inherited_attr->clone_and_inherit_options($name, $args); |
102 | } |
103 | else{ |
104 | return Mouse::Meta::Attribute->interpolate_class_and_new($name, $args); |
105 | } |
106 | } |
107 | |
108 | sub add_attribute { |
109 | my $self = shift; |
110 | |
111 | my $attr = blessed($_[0]) ? $_[0] : $self->_process_attribute(@_); |
112 | |
113 | $attr->isa('Mouse::Meta::Attribute') |
114 | || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)"); |
115 | |
116 | weaken( $attr->{associated_class} = $self ); |
117 | |
118 | $self->{attributes}{$attr->name} = $attr; |
119 | $attr->install_accessors(); |
120 | |
121 | if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){ |
122 | 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 |
123 | } |
1b9e472d |
124 | return $attr; |
c3398f5b |
125 | } |
126 | |
d60824af |
127 | sub compute_all_applicable_attributes { shift->get_all_attributes(@_) } |
128 | sub get_all_attributes { |
72b88a88 |
129 | my $self = shift; |
130 | my (@attr, %seen); |
131 | |
132 | for my $class ($self->linearized_isa) { |
133 | my $meta = $self->_metaclass_cache($class) |
134 | or next; |
135 | |
136 | for my $name (keys %{ $meta->get_attribute_map }) { |
137 | next if $seen{$name}++; |
138 | push @attr, $meta->get_attribute($name); |
139 | } |
140 | } |
141 | |
142 | return @attr; |
143 | } |
144 | |
8536d351 |
145 | sub linearized_isa { @{ get_linear_isa($_[0]->name) } } |
146 | |
147 | sub new_object { |
c68b4110 |
148 | my $self = shift; |
7a50b450 |
149 | my %args = (@_ == 1 ? %{$_[0]} : @_); |
c3398f5b |
150 | |
fce211ae |
151 | my $instance = bless {}, $self->name; |
152 | |
1b9e472d |
153 | $self->_initialize_instance($instance, \%args); |
154 | return $instance; |
155 | } |
156 | |
157 | sub _initialize_instance{ |
158 | my($self, $instance, $args) = @_; |
159 | |
53c495ce |
160 | my @triggers_queue; |
161 | |
fce211ae |
162 | foreach my $attribute ($self->get_all_attributes) { |
8536d351 |
163 | my $from = $attribute->init_arg; |
164 | my $key = $attribute->name; |
165 | |
1b9e472d |
166 | if (defined($from) && exists($args->{$from})) { |
167 | $args->{$from} = $attribute->coerce_constraint($args->{$from}) |
8536d351 |
168 | if $attribute->should_coerce; |
8536d351 |
169 | |
1b9e472d |
170 | $attribute->verify_against_type_constraint($args->{$from}); |
171 | |
172 | $instance->{$key} = $args->{$from}; |
8536d351 |
173 | |
174 | weaken($instance->{$key}) |
fce211ae |
175 | if ref($instance->{$key}) && $attribute->is_weak_ref; |
8536d351 |
176 | |
177 | if ($attribute->has_trigger) { |
1b9e472d |
178 | push @triggers_queue, [ $attribute->trigger, $args->{$from} ]; |
8536d351 |
179 | } |
180 | } |
181 | else { |
182 | if ($attribute->has_default || $attribute->has_builder) { |
183 | unless ($attribute->is_lazy) { |
184 | my $default = $attribute->default; |
185 | my $builder = $attribute->builder; |
186 | my $value = $attribute->has_builder |
187 | ? $instance->$builder |
188 | : ref($default) eq 'CODE' |
189 | ? $default->($instance) |
190 | : $default; |
191 | |
192 | $value = $attribute->coerce_constraint($value) |
193 | if $attribute->should_coerce; |
194 | $attribute->verify_against_type_constraint($value); |
195 | |
196 | $instance->{$key} = $value; |
197 | |
198 | weaken($instance->{$key}) |
fce211ae |
199 | if ref($instance->{$key}) && $attribute->is_weak_ref; |
8536d351 |
200 | } |
201 | } |
202 | else { |
203 | if ($attribute->is_required) { |
fce211ae |
204 | $self->throw_error("Attribute (".$attribute->name.") is required"); |
8536d351 |
205 | } |
206 | } |
207 | } |
208 | } |
53c495ce |
209 | |
210 | foreach my $trigger_and_value(@triggers_queue){ |
211 | my($trigger, $value) = @{$trigger_and_value}; |
212 | $trigger->($instance, $value); |
213 | } |
214 | |
2efc0af1 |
215 | if($self->is_anon_class){ |
216 | $instance->{__METACLASS__} = $self; |
217 | } |
218 | |
8536d351 |
219 | return $instance; |
220 | } |
c3398f5b |
221 | |
7a59f4e8 |
222 | sub clone_object { |
223 | my $class = shift; |
224 | my $instance = shift; |
225 | |
226 | (blessed($instance) && $instance->isa($class->name)) |
fce211ae |
227 | || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)"); |
7a59f4e8 |
228 | |
229 | $class->clone_instance($instance, @_); |
230 | } |
231 | |
232 | sub clone_instance { |
233 | my ($class, $instance, %params) = @_; |
234 | |
235 | (blessed($instance)) |
fce211ae |
236 | || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance"); |
7a59f4e8 |
237 | |
238 | my $clone = bless { %$instance }, ref $instance; |
239 | |
d60824af |
240 | foreach my $attr ($class->get_all_attributes()) { |
7a59f4e8 |
241 | if ( defined( my $init_arg = $attr->init_arg ) ) { |
242 | if (exists $params{$init_arg}) { |
243 | $clone->{ $attr->name } = $params{$init_arg}; |
244 | } |
245 | } |
246 | } |
247 | |
248 | return $clone; |
249 | |
250 | } |
251 | |
fc1d8369 |
252 | sub make_immutable { |
253 | my $self = shift; |
6a1d1835 |
254 | my %args = ( |
255 | inline_constructor => 1, |
e578d610 |
256 | inline_destructor => 1, |
6a1d1835 |
257 | @_, |
258 | ); |
259 | |
fc1d8369 |
260 | $self->{is_immutable}++; |
c7a6403f |
261 | |
6a1d1835 |
262 | if ($args{inline_constructor}) { |
c7a6403f |
263 | $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self )); |
264 | } |
265 | |
8632b6fe |
266 | if ($args{inline_destructor}) { |
267 | $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self )); |
268 | } |
2276cb14 |
269 | |
270 | # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value |
271 | # at the end of a source file. |
272 | return 1; |
fc1d8369 |
273 | } |
ad958001 |
274 | |
fce211ae |
275 | sub make_mutable { not_supported } |
ad958001 |
276 | |
6cfa1e5e |
277 | sub is_immutable { $_[0]->{is_immutable} } |
278 | sub is_mutable { !$_[0]->{is_immutable} } |
84ef660f |
279 | |
4859d490 |
280 | sub _install_modifier { |
281 | my ( $self, $into, $type, $name, $code ) = @_; |
4f5b44a0 |
282 | |
283 | # which is modifer class available? |
284 | my $modifier_class = do { |
285 | if (eval "require Class::Method::Modifiers::Fast; 1") { |
286 | 'Class::Method::Modifiers::Fast'; |
287 | } elsif (eval "require Class::Method::Modifiers; 1") { |
288 | 'Class::Method::Modifiers'; |
289 | } else { |
290 | 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."); |
291 | } |
292 | }; |
293 | my $modifier = $modifier_class->can('_install_modifier'); |
294 | |
295 | # replace this method itself :) |
296 | { |
4f5b44a0 |
297 | no warnings 'redefine'; |
4f9945f5 |
298 | *_install_modifier = sub { |
4f5b44a0 |
299 | my ( $self, $into, $type, $name, $code ) = @_; |
300 | $modifier->( |
301 | $into, |
302 | $type, |
303 | $name, |
304 | $code |
305 | ); |
6cfa1e5e |
306 | $self->{methods}{$name}++; # register it to the method map |
307 | return; |
4f5b44a0 |
308 | }; |
1b79a118 |
309 | } |
4f5b44a0 |
310 | |
311 | # call me. for first time. |
312 | $self->_install_modifier( $into, $type, $name, $code ); |
4859d490 |
313 | } |
314 | |
50dc6ee5 |
315 | sub add_before_method_modifier { |
4859d490 |
316 | my ( $self, $name, $code ) = @_; |
317 | $self->_install_modifier( $self->name, 'before', $name, $code ); |
50dc6ee5 |
318 | } |
319 | |
320 | sub add_around_method_modifier { |
4859d490 |
321 | my ( $self, $name, $code ) = @_; |
322 | $self->_install_modifier( $self->name, 'around', $name, $code ); |
50dc6ee5 |
323 | } |
324 | |
325 | sub add_after_method_modifier { |
4859d490 |
326 | my ( $self, $name, $code ) = @_; |
327 | $self->_install_modifier( $self->name, 'after', $name, $code ); |
50dc6ee5 |
328 | } |
329 | |
67199842 |
330 | sub add_override_method_modifier { |
331 | my ($self, $name, $code) = @_; |
332 | |
6cfa1e5e |
333 | my $package = $self->name; |
67199842 |
334 | |
6cfa1e5e |
335 | my $body = $package->can($name) |
336 | or $self->throw_error("You cannot override '$name' because it has no super method"); |
67199842 |
337 | |
6cfa1e5e |
338 | $self->add_method($name => sub { $code->($package, $body, @_) }); |
67199842 |
339 | } |
340 | |
47f36c05 |
341 | sub does_role { |
342 | my ($self, $role_name) = @_; |
ad958001 |
343 | |
47f36c05 |
344 | (defined $role_name) |
fce211ae |
345 | || $self->throw_error("You must supply a role name to look for"); |
ad958001 |
346 | |
f7fec86c |
347 | for my $class ($self->linearized_isa) { |
08f7a8db |
348 | my $meta = Mouse::Meta::Module::class_of($class); |
3a63a2e7 |
349 | next unless $meta && $meta->can('roles'); |
350 | |
351 | for my $role (@{ $meta->roles }) { |
ff687069 |
352 | |
3a63a2e7 |
353 | return 1 if $role->does_role($role_name); |
f7fec86c |
354 | } |
47f36c05 |
355 | } |
ad958001 |
356 | |
47f36c05 |
357 | return 0; |
358 | } |
359 | |
c3398f5b |
360 | 1; |
361 | |
362 | __END__ |
363 | |
364 | =head1 NAME |
365 | |
306290e8 |
366 | Mouse::Meta::Class - hook into the Mouse MOP |
c3398f5b |
367 | |
368 | =head1 METHODS |
369 | |
306290e8 |
370 | =head2 initialize ClassName -> Mouse::Meta::Class |
c3398f5b |
371 | |
306290e8 |
372 | Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only |
373 | one instance should exist for a given class. |
c3398f5b |
374 | |
306290e8 |
375 | =head2 new %args -> Mouse::Meta::Class |
c3398f5b |
376 | |
306290e8 |
377 | Creates a new Mouse::Meta::Class. Don't call this directly. |
c3398f5b |
378 | |
379 | =head2 name -> ClassName |
380 | |
381 | Returns the name of the owner class. |
382 | |
383 | =head2 superclasses -> [ClassName] |
384 | |
385 | Gets (or sets) the list of superclasses of the owner class. |
386 | |
60f6eba9 |
387 | =head2 add_attribute (Mouse::Meta::Attribute| name => spec) |
c3398f5b |
388 | |
306290e8 |
389 | Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner |
390 | class. |
c3398f5b |
391 | |
d60824af |
392 | =head2 get_all_attributes -> (Mouse::Meta::Attribute) |
72b88a88 |
393 | |
394 | Returns the list of all L<Mouse::Meta::Attribute> instances associated with |
395 | this class and its superclasses. |
396 | |
306290e8 |
397 | =head2 get_attribute_map -> { name => Mouse::Meta::Attribute } |
c3398f5b |
398 | |
399 | Returns a mapping of attribute names to their corresponding |
306290e8 |
400 | L<Mouse::Meta::Attribute> objects. |
c3398f5b |
401 | |
c68b4110 |
402 | =head2 get_attribute_list -> { name => Mouse::Meta::Attribute } |
403 | |
404 | This returns a list of attribute names which are defined in the local |
405 | class. If you want a list of all applicable attributes for a class, |
d60824af |
406 | use the C<get_all_attributes> method. |
c68b4110 |
407 | |
cbc437f2 |
408 | =head2 has_attribute Name -> Bool |
66eea168 |
409 | |
410 | Returns whether we have a L<Mouse::Meta::Attribute> with the given name. |
411 | |
306290e8 |
412 | =head2 get_attribute Name -> Mouse::Meta::Attribute | undef |
c3398f5b |
413 | |
306290e8 |
414 | Returns the L<Mouse::Meta::Attribute> with the given name. |
c3398f5b |
415 | |
416 | =head2 linearized_isa -> [ClassNames] |
417 | |
418 | Returns the list of classes in method dispatch order, with duplicates removed. |
419 | |
f7b11a21 |
420 | =head2 clone_object Instance -> Instance |
421 | |
422 | Clones the given C<Instance> which must be an instance governed by this |
423 | metaclass. |
424 | |
425 | =head2 clone_instance Instance, Parameters -> Instance |
426 | |
518e303a |
427 | The clone_instance method has been made private. |
428 | The public version is deprecated. |
f7b11a21 |
429 | |
c3398f5b |
430 | =cut |
431 | |