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