bug in inlined constructor and tests
[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
ca168e89 10our $VERSION = '0.03';
5cf3dbcf 11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Moose::Meta::Method';
14
15sub new {
16 my $class = shift;
17 my %options = @_;
7a5b07b3 18
5cf3dbcf 19 (exists $options{options} && ref $options{options} eq 'HASH')
7a5b07b3 20 || confess "You must pass a hash of options";
21
5cf3dbcf 22 my $self = bless {
23 # from our superclass
24 '&!body' => undef,
25 # specific to this subclass
26 '%!options' => $options{options},
587ae0d2 27 '$!meta_instance' => $options{metaclass}->get_meta_instance,
7a5b07b3 28 '@!attributes' => [ $options{metaclass}->compute_all_applicable_attributes ],
5cf3dbcf 29 # ...
30 '$!associated_metaclass' => $options{metaclass},
31 } => $class;
32
7a5b07b3 33 # we don't want this creating
34 # a cycle in the code, if not
5cf3dbcf 35 # needed
7a5b07b3 36 weaken($self->{'$!associated_metaclass'});
5cf3dbcf 37
38 $self->intialize_body;
39
7a5b07b3 40 return $self;
5cf3dbcf 41}
42
7a5b07b3 43## accessors
5cf3dbcf 44
45sub options { (shift)->{'%!options'} }
46sub meta_instance { (shift)->{'$!meta_instance'} }
47sub attributes { (shift)->{'@!attributes'} }
48
49sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
50
51## method
52
53sub intialize_body {
54 my $self = shift;
55 # TODO:
7a5b07b3 56 # the %options should also include a both
57 # a call 'initializer' and call 'SUPER::'
58 # options, which should cover approx 90%
59 # of the possible use cases (even if it
60 # requires some adaption on the part of
5cf3dbcf 61 # the author, after all, nothing is free)
62 my $source = 'sub {';
1f779926 63 $source .= "\n" . 'my $class = shift;';
7a5b07b3 64
587ae0d2 65 $source .= "\n" . 'return $class->Moose::Object::new(@_)';
7a5b07b3 66 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
67
68 $source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;';
69
5cf3dbcf 70 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
7a5b07b3 71
72 $source .= ";\n" . (join ";\n" => map {
73 $self->_generate_slot_initializer($_)
5cf3dbcf 74 } 0 .. (@{$self->attributes} - 1));
7a5b07b3 75
5cf3dbcf 76 $source .= ";\n" . $self->_generate_BUILDALL();
7a5b07b3 77
5cf3dbcf 78 $source .= ";\n" . 'return $instance';
7a5b07b3 79 $source .= ";\n" . '}';
80 warn $source if $self->options->{debug};
81
5cf3dbcf 82 my $code;
83 {
84 # NOTE:
85 # create the nessecary lexicals
7a5b07b3 86 # to be picked up in the eval
5cf3dbcf 87 my $attrs = $self->attributes;
7a5b07b3 88
5cf3dbcf 89 $code = eval $source;
90 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
91 }
92 $self->{'&!body'} = $code;
93}
94
95sub _generate_BUILDALL {
96 my $self = shift;
97 my @BUILD_calls;
1f779926 98 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
7a5b07b3 99 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)';
5cf3dbcf 100 }
7a5b07b3 101 return join ";\n" => @BUILD_calls;
5cf3dbcf 102}
103
104sub _generate_slot_initializer {
105 my $self = shift;
106 my $index = shift;
7a5b07b3 107
5cf3dbcf 108 my $attr = $self->attributes->[$index];
7a5b07b3 109
5cf3dbcf 110 my @source = ('## ' . $attr->name);
d66bea3c 111
112 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
7a5b07b3 113
114 if ($is_moose && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
115 push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' .
5cf3dbcf 116 '|| confess "Attribute (' . $attr->name . ') is required";');
117 }
7a5b07b3 118
ca168e89 119 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
7a5b07b3 120
8ecb1fa0 121 push @source => 'if (exists $params{\'' . $attr->init_arg . '\'}) {';
122
123 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
d66bea3c 124 if ($is_moose && $attr->has_type_constraint) {
8ecb1fa0 125 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
126
7a5b07b3 127 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
128 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
8ecb1fa0 129 }
7a5b07b3 130 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
8ecb1fa0 131 }
7a5b07b3 132 push @source => $self->_generate_slot_assignment($attr, '$val');
133
134
135 push @source => "} else {";
136
ca168e89 137 my $default;
138 if( $attr->has_default ){
139 $default = $self->_generate_default_value($attr, $index);
140 } else {
141 my $builder = $attr->builder;
142 $default = '$instance->' . $builder;
143 }
5cf3dbcf 144 push @source => ('my $val = ' . $default . ';');
145 push @source => $self->_generate_type_constraint_check(
146 $attr,
7a5b07b3 147 ('$attrs->[' . $index . ']->type_constraint'),
5cf3dbcf 148 '$val'
7a5b07b3 149 ) if ($is_moose && $attr->has_type_constraint);
150 push @source => $self->_generate_slot_assignment($attr, $default);
151
152 push @source => "}";
153 }
5cf3dbcf 154 else {
8ecb1fa0 155 push @source => '(exists $params{\'' . $attr->init_arg . '\'}) && do {';
156
157 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
d66bea3c 158 if ($is_moose && $attr->has_type_constraint) {
8ecb1fa0 159 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
160
7a5b07b3 161 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
162 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
8ecb1fa0 163 }
7a5b07b3 164 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
8ecb1fa0 165 }
7a5b07b3 166 push @source => $self->_generate_slot_assignment($attr, '$val');
167
168 push @source => "}";
5cf3dbcf 169 }
7a5b07b3 170
5cf3dbcf 171 return join "\n" => @source;
172}
173
174sub _generate_slot_assignment {
175 my ($self, $attr, $value) = @_;
176 my $source = (
177 $self->meta_instance->inline_set_slot_value(
7a5b07b3 178 '$instance',
179 ("'" . $attr->name . "'"),
5cf3dbcf 180 $value
181 ) . ';'
7a5b07b3 182 );
d66bea3c 183
184 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
7a5b07b3 185
d66bea3c 186 if ($is_moose && $attr->is_weak_ref) {
5cf3dbcf 187 $source .= (
188 "\n" .
189 $self->meta_instance->inline_weaken_slot_value(
7a5b07b3 190 '$instance',
5cf3dbcf 191 ("'" . $attr->name . "'")
7a5b07b3 192 ) .
5cf3dbcf 193 ' if ref ' . $value . ';'
7a5b07b3 194 );
195 }
196
5cf3dbcf 197 return $source;
198}
199
200sub _generate_type_coercion {
201 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
202 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
203}
204
205sub _generate_type_constraint_check {
206 my ($self, $attr, $type_constraint_name, $value_name) = @_;
207 return (
208 'defined(' . $type_constraint_name . '->_compiled_type_constraint->(' . $value_name . '))'
7a5b07b3 209 . "\n\t" . '|| confess "Attribute (' . $attr->name . ') does not pass the type constraint ('
210 . $attr->type_constraint->name
8ac5969a 211 . ') with " . (defined(' . $value_name . ') ? (Scalar::Util::blessed(' . $value_name . ') && overload::Overloaded(' . $value_name . ') ? overload::StrVal(' . $value_name . ') : ' . $value_name . ') : "undef");'
7a5b07b3 212 );
5cf3dbcf 213}
214
215sub _generate_default_value {
216 my ($self, $attr, $index) = @_;
217 # NOTE:
218 # default values can either be CODE refs
7a5b07b3 219 # in which case we need to call them. Or
5cf3dbcf 220 # they can be scalars (strings/numbers)
221 # in which case we can just deal with them
222 # in the code we eval.
223 if ($attr->is_default_a_coderef) {
224 return '$attrs->[' . $index . ']->default($instance)';
225 }
226 else {
227 my $default = $attr->default;
228 # make sure to quote strings ...
229 unless (looks_like_number($default)) {
230 $default = "'$default'";
231 }
7a5b07b3 232
5cf3dbcf 233 return $default;
7a5b07b3 234 }
5cf3dbcf 235}
236
2371;
238
5cf3dbcf 239__END__
240
241=pod
242
7a5b07b3 243=head1 NAME
5cf3dbcf 244
245Moose::Meta::Method::Constructor - Method Meta Object for constructors
246
5cf3dbcf 247=head1 DESCRIPTION
248
7a5b07b3 249This is a subclass of L<Class::MOP::Method> which handles
250constructing an approprate Constructor methods. This is primarily
251used in the making of immutable metaclasses, otherwise it is
d44714be 252not particularly useful.
253
5cf3dbcf 254=head1 METHODS
255
256=over 4
257
258=item B<new>
259
260=item B<attributes>
261
262=item B<meta_instance>
263
264=item B<options>
265
266=item B<intialize_body>
267
268=item B<associated_metaclass>
269
270=back
271
272=head1 AUTHORS
273
274Stevan Little E<lt>stevan@iinteractive.comE<gt>
275
276=head1 COPYRIGHT AND LICENSE
277
b77fdbed 278Copyright 2006, 2007 by Infinity Interactive, Inc.
5cf3dbcf 279
280L<http://www.iinteractive.com>
281
282This library is free software; you can redistribute it and/or modify
7a5b07b3 283it under the same terms as Perl itself.
5cf3dbcf 284
285=cut
286