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