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