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