Add a confess to intialize_body in the Moose::Meta::Method::Constructor, to catch...
[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
1b2aea39 10our $VERSION = '0.11';
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})
24 || confess "You must supply the package_name and name parameters";
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
78 $source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;';
79
5cf3dbcf 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
123sub _generate_BUILDALL {
124 my $self = shift;
125 my @BUILD_calls;
1f779926 126 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
7a5b07b3 127 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)';
5cf3dbcf 128 }
7a5b07b3 129 return join ";\n" => @BUILD_calls;
5cf3dbcf 130}
131
1b55c340 132sub _generate_triggers {
133 my $self = shift;
134 my @trigger_calls;
135 foreach my $i (0 .. $#{ $self->attributes }) {
136 my $attr = $self->attributes->[$i];
137 if ($attr->can('has_trigger') && $attr->has_trigger) {
138 if (defined(my $init_arg = $attr->init_arg)) {
139 push @trigger_calls => (
140 '(exists $params{\'' . $init_arg . '\'}) && do {' . "\n "
141 . '$attrs->[' . $i . ']->trigger->('
142 . '$instance, '
143 . $self->meta_instance->inline_get_slot_value(
144 '$instance',
145 ("'" . $attr->name . "'")
146 )
147 . ', '
148 . '$attrs->[' . $i . ']'
149 . ');'
150 ."\n}"
151 );
152 }
153 }
154 }
155 return join ";\n" => @trigger_calls;
156}
157
5cf3dbcf 158sub _generate_slot_initializer {
159 my $self = shift;
160 my $index = shift;
7a5b07b3 161
5cf3dbcf 162 my $attr = $self->attributes->[$index];
7a5b07b3 163
5cf3dbcf 164 my @source = ('## ' . $attr->name);
d66bea3c 165
166 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
7a5b07b3 167
84981146 168 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
7a5b07b3 169 push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' .
5cf3dbcf 170 '|| confess "Attribute (' . $attr->name . ') is required";');
171 }
7a5b07b3 172
ca168e89 173 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
7a5b07b3 174
84981146 175 if ( defined( my $init_arg = $attr->init_arg ) ) {
176 push @source => 'if (exists $params{\'' . $init_arg . '\'}) {';
8ecb1fa0 177
84981146 178 push @source => ('my $val = $params{\'' . $init_arg . '\'};');
179
180 if ($is_moose && $attr->has_type_constraint) {
181 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
688fcdda 182 push @source => $self->_generate_type_coercion(
183 $attr,
184 '$type_constraints[' . $index . ']',
185 '$val',
186 '$val'
187 );
84981146 188 }
688fcdda 189 push @source => $self->_generate_type_constraint_check(
190 $attr,
191 '$type_constraint_bodies[' . $index . ']',
192 '$type_constraints[' . $index . ']',
193 '$val'
194 );
8ecb1fa0 195 }
9df136d0 196 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
7a5b07b3 197
84981146 198 push @source => "} else {";
199 }
ca168e89 200 my $default;
97e11ef5 201 if ( $attr->has_default ) {
ca168e89 202 $default = $self->_generate_default_value($attr, $index);
97e11ef5 203 }
204 else {
ca168e89 205 my $builder = $attr->builder;
206 $default = '$instance->' . $builder;
207 }
688fcdda 208
3db3ea82 209 push @source => '{'; # wrap this to avoid my $val overwrite warnings
5cf3dbcf 210 push @source => ('my $val = ' . $default . ';');
211 push @source => $self->_generate_type_constraint_check(
212 $attr,
c9183ffc 213 ('$type_constraint_bodies[' . $index . ']'),
688fcdda 214 ('$type_constraints[' . $index . ']'),
5cf3dbcf 215 '$val'
7a5b07b3 216 ) if ($is_moose && $attr->has_type_constraint);
bad76b8e 217
51c107ef 218 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
bad76b8e 219 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
7a5b07b3 220
84981146 221 push @source => "}" if defined $attr->init_arg;
7a5b07b3 222 }
84981146 223 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
224 push @source => '(exists $params{\'' . $init_arg . '\'}) && do {';
8ecb1fa0 225
84981146 226 push @source => ('my $val = $params{\'' . $init_arg . '\'};');
d66bea3c 227 if ($is_moose && $attr->has_type_constraint) {
7a5b07b3 228 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
688fcdda 229 push @source => $self->_generate_type_coercion(
230 $attr,
231 '$type_constraints[' . $index . ']',
232 '$val',
233 '$val'
234 );
8ecb1fa0 235 }
688fcdda 236 push @source => $self->_generate_type_constraint_check(
237 $attr,
238 '$type_constraint_bodies[' . $index . ']',
239 '$type_constraints[' . $index . ']',
240 '$val'
241 );
8ecb1fa0 242 }
9df136d0 243 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
7a5b07b3 244
245 push @source => "}";
5cf3dbcf 246 }
7a5b07b3 247
5cf3dbcf 248 return join "\n" => @source;
249}
250
251sub _generate_slot_assignment {
9df136d0 252 my ($self, $attr, $value, $index) = @_;
253
254 my $source;
255
256 if ($attr->has_initializer) {
257 $source = (
258 '$attrs->[' . $index . ']->set_initial_value($instance, ' . $value . ');'
259 );
260 }
261 else {
262 $source = (
263 $self->meta_instance->inline_set_slot_value(
264 '$instance',
265 ("'" . $attr->name . "'"),
266 $value
267 ) . ';'
268 );
269 }
270
271 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
7a5b07b3 272
d66bea3c 273 if ($is_moose && $attr->is_weak_ref) {
5cf3dbcf 274 $source .= (
275 "\n" .
276 $self->meta_instance->inline_weaken_slot_value(
7a5b07b3 277 '$instance',
5cf3dbcf 278 ("'" . $attr->name . "'")
7a5b07b3 279 ) .
5cf3dbcf 280 ' if ref ' . $value . ';'
7a5b07b3 281 );
282 }
283
5cf3dbcf 284 return $source;
285}
286
287sub _generate_type_coercion {
288 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
289 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
290}
291
292sub _generate_type_constraint_check {
688fcdda 293 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
5cf3dbcf 294 return (
c9183ffc 295 $type_constraint_cv . '->(' . $value_name . ')'
688fcdda 296 . "\n\t" . '|| confess "Attribute ('
297 . $attr->name
298 . ') does not pass the type constraint because: " . '
299 . $type_constraint_obj . '->get_message(' . $value_name . ');'
7a5b07b3 300 );
5cf3dbcf 301}
302
303sub _generate_default_value {
304 my ($self, $attr, $index) = @_;
305 # NOTE:
306 # default values can either be CODE refs
7a5b07b3 307 # in which case we need to call them. Or
5cf3dbcf 308 # they can be scalars (strings/numbers)
309 # in which case we can just deal with them
310 # in the code we eval.
311 if ($attr->is_default_a_coderef) {
312 return '$attrs->[' . $index . ']->default($instance)';
313 }
314 else {
315 my $default = $attr->default;
316 # make sure to quote strings ...
317 unless (looks_like_number($default)) {
318 $default = "'$default'";
319 }
7a5b07b3 320
5cf3dbcf 321 return $default;
7a5b07b3 322 }
5cf3dbcf 323}
324
3251;
326
5cf3dbcf 327__END__
328
329=pod
330
7a5b07b3 331=head1 NAME
5cf3dbcf 332
333Moose::Meta::Method::Constructor - Method Meta Object for constructors
334
5cf3dbcf 335=head1 DESCRIPTION
336
7a5b07b3 337This is a subclass of L<Class::MOP::Method> which handles
338constructing an approprate Constructor methods. This is primarily
339used in the making of immutable metaclasses, otherwise it is
d44714be 340not particularly useful.
341
5cf3dbcf 342=head1 METHODS
343
344=over 4
345
346=item B<new>
347
348=item B<attributes>
349
350=item B<meta_instance>
351
352=item B<options>
353
415e6f85 354=item B<initialize_body>
5cf3dbcf 355
356=item B<associated_metaclass>
357
358=back
359
360=head1 AUTHORS
361
362Stevan Little E<lt>stevan@iinteractive.comE<gt>
363
364=head1 COPYRIGHT AND LICENSE
365
778db3ac 366Copyright 2006-2008 by Infinity Interactive, Inc.
5cf3dbcf 367
368L<http://www.iinteractive.com>
369
370This library is free software; you can redistribute it and/or modify
7a5b07b3 371it under the same terms as Perl itself.
5cf3dbcf 372
373=cut
374