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