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