Merge 'Moose-moosex_compile_support' into 'trunk'
[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
a4e516f6 10our $VERSION = '0.04';
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
39 $self->intialize_body;
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
54sub intialize_body {
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
5cf3dbcf 77 $source .= ";\n" . $self->_generate_BUILDALL();
7a5b07b3 78
5cf3dbcf 79 $source .= ";\n" . 'return $instance';
7a5b07b3 80 $source .= ";\n" . '}';
81 warn $source if $self->options->{debug};
82
5cf3dbcf 83 my $code;
84 {
85 # NOTE:
86 # create the nessecary lexicals
7a5b07b3 87 # to be picked up in the eval
5cf3dbcf 88 my $attrs = $self->attributes;
7a5b07b3 89
7c4f0d32 90 my @type_constraints = map { $_->type_constraint } @$attrs;
c9183ffc 91 my @type_constraint_bodies = map {
5e02366b 92 $_ && $_->_compiled_type_constraint;
c9183ffc 93 } @type_constraints;
7c4f0d32 94
5cf3dbcf 95 $code = eval $source;
96 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
97 }
98 $self->{'&!body'} = $code;
99}
100
101sub _generate_BUILDALL {
102 my $self = shift;
103 my @BUILD_calls;
1f779926 104 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
7a5b07b3 105 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)';
5cf3dbcf 106 }
7a5b07b3 107 return join ";\n" => @BUILD_calls;
5cf3dbcf 108}
109
110sub _generate_slot_initializer {
111 my $self = shift;
112 my $index = shift;
7a5b07b3 113
5cf3dbcf 114 my $attr = $self->attributes->[$index];
7a5b07b3 115
5cf3dbcf 116 my @source = ('## ' . $attr->name);
d66bea3c 117
118 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
7a5b07b3 119
120 if ($is_moose && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
121 push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' .
5cf3dbcf 122 '|| confess "Attribute (' . $attr->name . ') is required";');
123 }
7a5b07b3 124
ca168e89 125 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
7a5b07b3 126
8ecb1fa0 127 push @source => 'if (exists $params{\'' . $attr->init_arg . '\'}) {';
128
129 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
d66bea3c 130 if ($is_moose && $attr->has_type_constraint) {
7a5b07b3 131 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
c9183ffc 132 push @source => $self->_generate_type_coercion($attr, '$type_constraints[' . $index . ']', '$val', '$val');
8ecb1fa0 133 }
c9183ffc 134 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint_bodies[' . $index . ']', '$val');
8ecb1fa0 135 }
7a5b07b3 136 push @source => $self->_generate_slot_assignment($attr, '$val');
137
7a5b07b3 138 push @source => "} else {";
139
ca168e89 140 my $default;
97e11ef5 141 if ( $attr->has_default ) {
ca168e89 142 $default = $self->_generate_default_value($attr, $index);
97e11ef5 143 }
144 else {
ca168e89 145 my $builder = $attr->builder;
146 $default = '$instance->' . $builder;
147 }
5cf3dbcf 148 push @source => ('my $val = ' . $default . ';');
149 push @source => $self->_generate_type_constraint_check(
150 $attr,
c9183ffc 151 ('$type_constraint_bodies[' . $index . ']'),
5cf3dbcf 152 '$val'
7a5b07b3 153 ) if ($is_moose && $attr->has_type_constraint);
154 push @source => $self->_generate_slot_assignment($attr, $default);
155
156 push @source => "}";
157 }
5cf3dbcf 158 else {
8ecb1fa0 159 push @source => '(exists $params{\'' . $attr->init_arg . '\'}) && do {';
160
161 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
d66bea3c 162 if ($is_moose && $attr->has_type_constraint) {
7a5b07b3 163 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
c9183ffc 164 push @source => $self->_generate_type_coercion($attr, '$type_constraints[' . $index . ']', '$val', '$val');
8ecb1fa0 165 }
c9183ffc 166 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint_bodies[' . $index . ']', '$val');
8ecb1fa0 167 }
7a5b07b3 168 push @source => $self->_generate_slot_assignment($attr, '$val');
169
170 push @source => "}";
5cf3dbcf 171 }
7a5b07b3 172
5cf3dbcf 173 return join "\n" => @source;
174}
175
176sub _generate_slot_assignment {
177 my ($self, $attr, $value) = @_;
178 my $source = (
179 $self->meta_instance->inline_set_slot_value(
7a5b07b3 180 '$instance',
181 ("'" . $attr->name . "'"),
5cf3dbcf 182 $value
183 ) . ';'
7a5b07b3 184 );
d66bea3c 185
186 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
7a5b07b3 187
d66bea3c 188 if ($is_moose && $attr->is_weak_ref) {
5cf3dbcf 189 $source .= (
190 "\n" .
191 $self->meta_instance->inline_weaken_slot_value(
7a5b07b3 192 '$instance',
5cf3dbcf 193 ("'" . $attr->name . "'")
7a5b07b3 194 ) .
5cf3dbcf 195 ' if ref ' . $value . ';'
7a5b07b3 196 );
197 }
198
5cf3dbcf 199 return $source;
200}
201
202sub _generate_type_coercion {
203 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
204 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
205}
206
207sub _generate_type_constraint_check {
c9183ffc 208 my ($self, $attr, $type_constraint_cv, $value_name) = @_;
5cf3dbcf 209 return (
c9183ffc 210 $type_constraint_cv . '->(' . $value_name . ')'
7a5b07b3 211 . "\n\t" . '|| confess "Attribute (' . $attr->name . ') does not pass the type constraint ('
212 . $attr->type_constraint->name
6361ccf5 213 . ') with " . (defined(' . $value_name . ') ? overload::StrVal(' . $value_name . ') : "undef");'
7a5b07b3 214 );
5cf3dbcf 215}
216
217sub _generate_default_value {
218 my ($self, $attr, $index) = @_;
219 # NOTE:
220 # default values can either be CODE refs
7a5b07b3 221 # in which case we need to call them. Or
5cf3dbcf 222 # they can be scalars (strings/numbers)
223 # in which case we can just deal with them
224 # in the code we eval.
225 if ($attr->is_default_a_coderef) {
226 return '$attrs->[' . $index . ']->default($instance)';
227 }
228 else {
229 my $default = $attr->default;
230 # make sure to quote strings ...
231 unless (looks_like_number($default)) {
232 $default = "'$default'";
233 }
7a5b07b3 234
5cf3dbcf 235 return $default;
7a5b07b3 236 }
5cf3dbcf 237}
238
2391;
240
5cf3dbcf 241__END__
242
243=pod
244
7a5b07b3 245=head1 NAME
5cf3dbcf 246
247Moose::Meta::Method::Constructor - Method Meta Object for constructors
248
5cf3dbcf 249=head1 DESCRIPTION
250
7a5b07b3 251This is a subclass of L<Class::MOP::Method> which handles
252constructing an approprate Constructor methods. This is primarily
253used in the making of immutable metaclasses, otherwise it is
d44714be 254not particularly useful.
255
5cf3dbcf 256=head1 METHODS
257
258=over 4
259
260=item B<new>
261
262=item B<attributes>
263
264=item B<meta_instance>
265
266=item B<options>
267
268=item B<intialize_body>
269
270=item B<associated_metaclass>
271
272=back
273
274=head1 AUTHORS
275
276Stevan Little E<lt>stevan@iinteractive.comE<gt>
277
278=head1 COPYRIGHT AND LICENSE
279
778db3ac 280Copyright 2006-2008 by Infinity Interactive, Inc.
5cf3dbcf 281
282L<http://www.iinteractive.com>
283
284This library is free software; you can redistribute it and/or modify
7a5b07b3 285it under the same terms as Perl itself.
5cf3dbcf 286
287=cut
288