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