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