Commit | Line | Data |
5cf3dbcf |
1 | |
2 | package Moose::Meta::Method::Constructor; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
0fa70d03 |
7 | use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr'; |
5cf3dbcf |
8 | |
19a330e5 |
9 | our $VERSION = '1.03'; |
5cf3dbcf |
10 | our $AUTHORITY = 'cpan:STEVAN'; |
11 | |
badb7e89 |
12 | use base 'Moose::Meta::Method', |
bc89e9b5 |
13 | 'Class::MOP::Method::Constructor'; |
5cf3dbcf |
14 | |
15 | sub new { |
16 | my $class = shift; |
17 | my %options = @_; |
7a5b07b3 |
18 | |
3e504337 |
19 | my $meta = $options{metaclass}; |
20 | |
21 | (ref $options{options} eq 'HASH') |
a9538ac9 |
22 | || $class->throw_error("You must pass a hash of options", data => $options{options}); |
7a5b07b3 |
23 | |
1b2aea39 |
24 | ($options{package_name} && $options{name}) |
a9538ac9 |
25 | || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"); |
1b2aea39 |
26 | |
5cf3dbcf |
27 | my $self = bless { |
d03bd989 |
28 | 'body' => undef, |
e606ae5f |
29 | 'package_name' => $options{package_name}, |
30 | 'name' => $options{name}, |
e606ae5f |
31 | 'options' => $options{options}, |
e606ae5f |
32 | 'associated_metaclass' => $meta, |
0fa70d03 |
33 | '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object', |
5cf3dbcf |
34 | } => $class; |
35 | |
7a5b07b3 |
36 | # we don't want this creating |
37 | # a cycle in the code, if not |
5cf3dbcf |
38 | # needed |
e606ae5f |
39 | weaken($self->{'associated_metaclass'}); |
5cf3dbcf |
40 | |
f5b0af77 |
41 | $self->_initialize_body; |
5cf3dbcf |
42 | |
7a5b07b3 |
43 | return $self; |
5cf3dbcf |
44 | } |
45 | |
5cf3dbcf |
46 | ## method |
47 | |
f5b0af77 |
48 | sub _initialize_body { |
5cf3dbcf |
49 | my $self = shift; |
50 | # TODO: |
7a5b07b3 |
51 | # the %options should also include a both |
52 | # a call 'initializer' and call 'SUPER::' |
53 | # options, which should cover approx 90% |
54 | # of the possible use cases (even if it |
55 | # requires some adaption on the part of |
5cf3dbcf |
56 | # the author, after all, nothing is free) |
57 | my $source = 'sub {'; |
5d27ac73 |
58 | $source .= "\n" . 'my $_instance = shift;'; |
62c8675e |
59 | |
5d27ac73 |
60 | $source .= "\n" . 'my $class = Scalar::Util::blessed($_instance) || $_instance;'; |
7a5b07b3 |
61 | |
587ae0d2 |
62 | $source .= "\n" . 'return $class->Moose::Object::new(@_)'; |
d03bd989 |
63 | $source .= "\n if \$class ne '" . $self->associated_metaclass->name |
ac070e13 |
64 | . "';\n"; |
93e98578 |
65 | |
ac070e13 |
66 | $source .= $self->_generate_params('$params', '$class'); |
67 | $source .= $self->_generate_instance('$instance', '$class'); |
68 | $source .= $self->_generate_slot_initializers; |
7a5b07b3 |
69 | |
ac070e13 |
70 | $source .= $self->_generate_triggers(); |
5cf3dbcf |
71 | $source .= ";\n" . $self->_generate_BUILDALL(); |
7a5b07b3 |
72 | |
ac070e13 |
73 | $source .= ";\nreturn \$instance"; |
7a5b07b3 |
74 | $source .= ";\n" . '}'; |
75 | warn $source if $self->options->{debug}; |
76 | |
5442a061 |
77 | # We need to check if the attribute ->can('type_constraint') |
78 | # since we may be trying to immutabilize a Moose meta class, |
79 | # which in turn has attributes which are Class::MOP::Attribute |
80 | # objects, rather than Moose::Meta::Attribute. And |
81 | # Class::MOP::Attribute attributes have no type constraints. |
82 | # However we need to make sure we leave an undef value there |
83 | # because the inlined code is using the index of the attributes |
84 | # to determine where to find the type constraint |
85 | |
0772362a |
86 | my $attrs = $self->_attributes; |
5442a061 |
87 | |
88 | my @type_constraints = map { |
89 | $_->can('type_constraint') ? $_->type_constraint : undef |
90 | } @$attrs; |
91 | |
92 | my @type_constraint_bodies = map { |
93 | defined $_ ? $_->_compiled_type_constraint : undef; |
94 | } @type_constraints; |
95 | |
34aab661 |
96 | my ( $code, $e ) = $self->_compile_code( |
5442a061 |
97 | code => $source, |
98 | environment => { |
99 | '$meta' => \$self, |
100 | '$attrs' => \$attrs, |
101 | '@type_constraints' => \@type_constraints, |
102 | '@type_constraint_bodies' => \@type_constraint_bodies, |
103 | }, |
34aab661 |
104 | ); |
105 | |
106 | $self->throw_error( |
107 | "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e", |
108 | error => $e, data => $source ) |
109 | if $e; |
d03bd989 |
110 | |
e606ae5f |
111 | $self->{'body'} = $code; |
112 | } |
113 | |
b905f0db |
114 | sub _generate_params { |
115 | my ( $self, $var, $class_var ) = @_; |
116 | "my $var = " . $self->_generate_BUILDARGS( $class_var, '@_' ) . ";\n"; |
117 | } |
118 | |
119 | sub _generate_instance { |
120 | my ( $self, $var, $class_var ) = @_; |
121 | "my $var = " |
0772362a |
122 | . $self->_meta_instance->inline_create_instance($class_var) . ";\n"; |
b905f0db |
123 | } |
124 | |
125 | sub _generate_slot_initializers { |
126 | my ($self) = @_; |
127 | return (join ";\n" => map { |
128 | $self->_generate_slot_initializer($_) |
0772362a |
129 | } 0 .. (@{$self->_attributes} - 1)) . ";\n"; |
b905f0db |
130 | } |
131 | |
e606ae5f |
132 | sub _generate_BUILDARGS { |
133 | my ( $self, $class, $args ) = @_; |
134 | |
135 | my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS"); |
136 | |
137 | if ( $args eq '@_' and ( !$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS ) ) { |
138 | return join("\n", |
139 | 'do {', |
140 | $self->_inline_throw_error('"Single parameters to new() must be a HASH ref"', 'data => $_[0]'), |
a62dcd43 |
141 | ' if scalar @_ == 1 && !( defined $_[0] && ref $_[0] eq q{HASH} );', |
e606ae5f |
142 | '(scalar @_ == 1) ? {%{$_[0]}} : {@_};', |
143 | '}', |
144 | ); |
145 | } else { |
146 | return $class . "->BUILDARGS($args)"; |
147 | } |
5cf3dbcf |
148 | } |
149 | |
150 | sub _generate_BUILDALL { |
151 | my $self = shift; |
152 | my @BUILD_calls; |
1f779926 |
153 | foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) { |
e606ae5f |
154 | push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD($params)'; |
5cf3dbcf |
155 | } |
7a5b07b3 |
156 | return join ";\n" => @BUILD_calls; |
5cf3dbcf |
157 | } |
158 | |
1b55c340 |
159 | sub _generate_triggers { |
160 | my $self = shift; |
161 | my @trigger_calls; |
0772362a |
162 | foreach my $i ( 0 .. $#{ $self->_attributes } ) { |
163 | my $attr = $self->_attributes->[$i]; |
708b4070 |
164 | |
165 | next unless $attr->can('has_trigger') && $attr->has_trigger; |
166 | |
167 | my $init_arg = $attr->init_arg; |
168 | |
169 | next unless defined $init_arg; |
170 | |
171 | push @trigger_calls => '(exists $params->{\'' |
172 | . $init_arg |
173 | . '\'}) && do {' |
174 | . "\n " |
175 | . '$attrs->[' |
176 | . $i |
177 | . ']->trigger->(' |
178 | . '$instance, ' |
0772362a |
179 | . $self->_meta_instance->inline_get_slot_value( |
708b4070 |
180 | '$instance', |
181 | $attr->name, |
182 | ) |
183 | . ', ' |
c2685d20 |
184 | . ');' . "\n}"; |
1b55c340 |
185 | } |
708b4070 |
186 | |
187 | return join ";\n" => @trigger_calls; |
1b55c340 |
188 | } |
189 | |
5cf3dbcf |
190 | sub _generate_slot_initializer { |
191 | my $self = shift; |
192 | my $index = shift; |
7a5b07b3 |
193 | |
0772362a |
194 | my $attr = $self->_attributes->[$index]; |
7a5b07b3 |
195 | |
5cf3dbcf |
196 | my @source = ('## ' . $attr->name); |
d66bea3c |
197 | |
198 | my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME |
7a5b07b3 |
199 | |
84981146 |
200 | if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) { |
e606ae5f |
201 | push @source => ('(exists $params->{\'' . $attr->init_arg . '\'}) ' . |
95d922a2 |
202 | '|| ' . $self->_inline_throw_error('"Attribute (' . quotemeta($attr->name) . ') is required"') .';'); |
5cf3dbcf |
203 | } |
7a5b07b3 |
204 | |
ca168e89 |
205 | if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) { |
7a5b07b3 |
206 | |
84981146 |
207 | if ( defined( my $init_arg = $attr->init_arg ) ) { |
e606ae5f |
208 | push @source => 'if (exists $params->{\'' . $init_arg . '\'}) {'; |
209 | push @source => ('my $val = $params->{\'' . $init_arg . '\'};'); |
210 | push @source => $self->_generate_type_constraint_and_coercion($attr, $index) |
211 | if $is_moose; |
212 | push @source => $self->_generate_slot_assignment($attr, '$val', $index); |
84981146 |
213 | push @source => "} else {"; |
214 | } |
ca168e89 |
215 | my $default; |
97e11ef5 |
216 | if ( $attr->has_default ) { |
ca168e89 |
217 | $default = $self->_generate_default_value($attr, $index); |
d03bd989 |
218 | } |
97e11ef5 |
219 | else { |
ca168e89 |
220 | my $builder = $attr->builder; |
221 | $default = '$instance->' . $builder; |
222 | } |
d03bd989 |
223 | |
3db3ea82 |
224 | push @source => '{'; # wrap this to avoid my $val overwrite warnings |
5cf3dbcf |
225 | push @source => ('my $val = ' . $default . ';'); |
e606ae5f |
226 | push @source => $self->_generate_type_constraint_and_coercion($attr, $index) |
d03bd989 |
227 | if $is_moose; |
51c107ef |
228 | push @source => $self->_generate_slot_assignment($attr, '$val', $index); |
d03bd989 |
229 | push @source => '}'; # close - wrap this to avoid my $val overrite warnings |
7a5b07b3 |
230 | |
84981146 |
231 | push @source => "}" if defined $attr->init_arg; |
7a5b07b3 |
232 | } |
84981146 |
233 | elsif ( defined( my $init_arg = $attr->init_arg ) ) { |
e606ae5f |
234 | push @source => '(exists $params->{\'' . $init_arg . '\'}) && do {'; |
8ecb1fa0 |
235 | |
e606ae5f |
236 | push @source => ('my $val = $params->{\'' . $init_arg . '\'};'); |
d66bea3c |
237 | if ($is_moose && $attr->has_type_constraint) { |
7a5b07b3 |
238 | if ($attr->should_coerce && $attr->type_constraint->has_coercion) { |
688fcdda |
239 | push @source => $self->_generate_type_coercion( |
d03bd989 |
240 | $attr, |
241 | '$type_constraints[' . $index . ']', |
242 | '$val', |
688fcdda |
243 | '$val' |
244 | ); |
8ecb1fa0 |
245 | } |
688fcdda |
246 | push @source => $self->_generate_type_constraint_check( |
d03bd989 |
247 | $attr, |
248 | '$type_constraint_bodies[' . $index . ']', |
249 | '$type_constraints[' . $index . ']', |
688fcdda |
250 | '$val' |
251 | ); |
8ecb1fa0 |
252 | } |
9df136d0 |
253 | push @source => $self->_generate_slot_assignment($attr, '$val', $index); |
7a5b07b3 |
254 | |
255 | push @source => "}"; |
5cf3dbcf |
256 | } |
7a5b07b3 |
257 | |
5cf3dbcf |
258 | return join "\n" => @source; |
259 | } |
260 | |
261 | sub _generate_slot_assignment { |
9df136d0 |
262 | my ($self, $attr, $value, $index) = @_; |
263 | |
264 | my $source; |
d03bd989 |
265 | |
9df136d0 |
266 | if ($attr->has_initializer) { |
267 | $source = ( |
268 | '$attrs->[' . $index . ']->set_initial_value($instance, ' . $value . ');' |
d03bd989 |
269 | ); |
9df136d0 |
270 | } |
271 | else { |
272 | $source = ( |
0772362a |
273 | $self->_meta_instance->inline_set_slot_value( |
9df136d0 |
274 | '$instance', |
eae37c67 |
275 | $attr->name, |
9df136d0 |
276 | $value |
277 | ) . ';' |
d03bd989 |
278 | ); |
9df136d0 |
279 | } |
d03bd989 |
280 | |
281 | my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME |
7a5b07b3 |
282 | |
d66bea3c |
283 | if ($is_moose && $attr->is_weak_ref) { |
5cf3dbcf |
284 | $source .= ( |
285 | "\n" . |
0772362a |
286 | $self->_meta_instance->inline_weaken_slot_value( |
7a5b07b3 |
287 | '$instance', |
eae37c67 |
288 | $attr->name |
7a5b07b3 |
289 | ) . |
5cf3dbcf |
290 | ' if ref ' . $value . ';' |
7a5b07b3 |
291 | ); |
292 | } |
293 | |
5cf3dbcf |
294 | return $source; |
295 | } |
296 | |
e606ae5f |
297 | sub _generate_type_constraint_and_coercion { |
298 | my ($self, $attr, $index) = @_; |
d03bd989 |
299 | |
e606ae5f |
300 | return unless $attr->has_type_constraint; |
d03bd989 |
301 | |
e606ae5f |
302 | my @source; |
303 | if ($attr->should_coerce && $attr->type_constraint->has_coercion) { |
304 | push @source => $self->_generate_type_coercion( |
305 | $attr, |
306 | '$type_constraints[' . $index . ']', |
307 | '$val', |
308 | '$val' |
309 | ); |
310 | } |
311 | push @source => $self->_generate_type_constraint_check( |
312 | $attr, |
313 | ('$type_constraint_bodies[' . $index . ']'), |
d03bd989 |
314 | ('$type_constraints[' . $index . ']'), |
e606ae5f |
315 | '$val' |
316 | ); |
317 | return @source; |
318 | } |
319 | |
5cf3dbcf |
320 | sub _generate_type_coercion { |
321 | my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_; |
322 | return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');'); |
323 | } |
324 | |
325 | sub _generate_type_constraint_check { |
688fcdda |
326 | my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_; |
5cf3dbcf |
327 | return ( |
3e504337 |
328 | $self->_inline_throw_error('"Attribute (' # FIXME add 'dad' |
95d922a2 |
329 | . quotemeta( $attr->name ) |
d03bd989 |
330 | . ') does not pass the type constraint because: " . ' |
3e504337 |
331 | . $type_constraint_obj . '->get_message(' . $value_name . ')') |
332 | . "\n\t unless " . $type_constraint_cv . '->(' . $value_name . ');' |
7a5b07b3 |
333 | ); |
5cf3dbcf |
334 | } |
335 | |
336 | sub _generate_default_value { |
337 | my ($self, $attr, $index) = @_; |
338 | # NOTE: |
339 | # default values can either be CODE refs |
7a5b07b3 |
340 | # in which case we need to call them. Or |
5cf3dbcf |
341 | # they can be scalars (strings/numbers) |
342 | # in which case we can just deal with them |
343 | # in the code we eval. |
344 | if ($attr->is_default_a_coderef) { |
345 | return '$attrs->[' . $index . ']->default($instance)'; |
346 | } |
347 | else { |
4aa3d405 |
348 | return q{"} . quotemeta( $attr->default ) . q{"}; |
7a5b07b3 |
349 | } |
5cf3dbcf |
350 | } |
351 | |
352 | 1; |
353 | |
5cf3dbcf |
354 | __END__ |
355 | |
356 | =pod |
357 | |
7a5b07b3 |
358 | =head1 NAME |
5cf3dbcf |
359 | |
360 | Moose::Meta::Method::Constructor - Method Meta Object for constructors |
361 | |
5cf3dbcf |
362 | =head1 DESCRIPTION |
363 | |
cec39889 |
364 | This class is a subclass of L<Class::MOP::Method::Constructor> that |
cefc9e36 |
365 | provides additional Moose-specific functionality |
366 | |
367 | To understand this class, you should read the the |
cec39889 |
368 | L<Class::MOP::Method::Constructor> documentation as well. |
d44714be |
369 | |
bc89e9b5 |
370 | =head1 INHERITANCE |
371 | |
372 | C<Moose::Meta::Method::Constructor> is a subclass of |
373 | L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>. |
374 | |
c5fc2c21 |
375 | =head1 BUGS |
376 | |
377 | See L<Moose/BUGS> for details on reporting bugs. |
378 | |
5cf3dbcf |
379 | =head1 AUTHORS |
380 | |
381 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
382 | |
383 | =head1 COPYRIGHT AND LICENSE |
384 | |
7e0492d3 |
385 | Copyright 2006-2010 by Infinity Interactive, Inc. |
5cf3dbcf |
386 | |
387 | L<http://www.iinteractive.com> |
388 | |
389 | This library is free software; you can redistribute it and/or modify |
7a5b07b3 |
390 | it under the same terms as Perl itself. |
5cf3dbcf |
391 | |
392 | =cut |
393 | |