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