2 package Moose::Meta::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Method';
19 (exists $options{options} && ref $options{options} eq 'HASH')
20 || confess "You must pass a hash of options";
22 (blessed $options{meta_instance} && $options{meta_instance}->isa('Class::MOP::Instance'))
23 || confess "You must supply a meta-instance";
25 (exists $options{attributes} && ref $options{attributes} eq 'ARRAY')
26 || confess "You must pass an array of options";
28 (blessed($_) && $_->isa('Class::MOP::Attribute'))
29 || confess "You must supply a list of attributes which is a 'Class::MOP::Attribute' instance"
30 for @{$options{attributes}};
35 # specific to this subclass
36 '%!options' => $options{options},
37 '$!meta_instance' => $options{meta_instance},
38 '@!attributes' => $options{attributes},
40 '$!associated_metaclass' => $options{metaclass},
43 # we don't want this creating
44 # a cycle in the code, if not
46 weaken($self->{'$!meta_instance'});
47 weaken($self->{'$!associated_metaclass'});
49 $self->intialize_body;
56 sub options { (shift)->{'%!options'} }
57 sub meta_instance { (shift)->{'$!meta_instance'} }
58 sub attributes { (shift)->{'@!attributes'} }
60 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
67 # the %options should also include a both
68 # a call 'initializer' and call 'SUPER::'
69 # options, which should cover approx 90%
70 # of the possible use cases (even if it
71 # requires some adaption on the part of
72 # the author, after all, nothing is free)
74 $source .= "\n" . 'my $class = shift; ';
75 $source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;';
77 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
79 $source .= ";\n" . (join ";\n" => map {
80 $self->_generate_slot_initializer($_)
81 } 0 .. (@{$self->attributes} - 1));
83 $source .= ";\n" . $self->_generate_BUILDALL();
85 $source .= ";\n" . 'return $instance';
86 $source .= ";\n" . '}';
87 warn $source if $self->options->{debug};
92 # create the nessecary lexicals
93 # to be picked up in the eval
94 my $attrs = $self->attributes;
97 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
99 $self->{'&!body'} = $code;
102 sub _generate_BUILDALL {
105 foreach my $method ($self->associated_metaclass->find_all_methods_by_name('BUILD')) {
106 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)';
108 return join "\n" => @BUILD_calls;
111 sub _generate_slot_initializer {
115 my $attr = $self->attributes->[$index];
117 my @source = ('## ' . $attr->name);
119 if ($attr->is_required && !$attr->has_default) {
120 push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' .
121 '|| confess "Attribute (' . $attr->name . ') is required";');
124 if ($attr->has_default && !$attr->is_lazy) {
126 push @source => 'if (exists $params{\'' . $attr->init_arg . '\'}) {';
128 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
129 if ($attr->has_type_constraint) {
130 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
132 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
133 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
135 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
137 push @source => $self->_generate_slot_assignment($attr, '$val');
140 push @source => "} else {";
142 my $default = $self->_generate_default_value($attr, $index);
144 push @source => ('my $val = ' . $default . ';');
145 push @source => $self->_generate_type_constraint_check(
147 ('$attrs->[' . $index . ']->type_constraint'),
149 ) if $attr->has_type_constraint;
150 push @source => $self->_generate_slot_assignment($attr, $default);
155 push @source => '(exists $params{\'' . $attr->init_arg . '\'}) && do {';
157 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
158 if ($attr->has_type_constraint) {
159 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
161 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
162 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
164 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
166 push @source => $self->_generate_slot_assignment($attr, '$val');
171 return join "\n" => @source;
174 sub _generate_slot_assignment {
175 my ($self, $attr, $value) = @_;
177 $self->meta_instance->inline_set_slot_value(
179 ("'" . $attr->name . "'"),
184 if ($attr->is_weak_ref) {
187 $self->meta_instance->inline_weaken_slot_value(
189 ("'" . $attr->name . "'")
191 ' if ref ' . $value . ';'
198 sub _generate_type_coercion {
199 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
200 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
203 sub _generate_type_constraint_check {
204 my ($self, $attr, $type_constraint_name, $value_name) = @_;
206 'defined(' . $type_constraint_name . '->_compiled_type_constraint->(' . $value_name . '))'
207 . "\n\t" . '|| confess "Attribute (' . $attr->name . ') does not pass the type constraint ('
208 . $attr->type_constraint->name . ') with " . (defined() ? "' . $value_name . '" : "undef");'
212 sub _generate_default_value {
213 my ($self, $attr, $index) = @_;
215 # default values can either be CODE refs
216 # in which case we need to call them. Or
217 # they can be scalars (strings/numbers)
218 # in which case we can just deal with them
219 # in the code we eval.
220 if ($attr->is_default_a_coderef) {
221 return '$attrs->[' . $index . ']->default($instance)';
224 my $default = $attr->default;
225 # make sure to quote strings ...
226 unless (looks_like_number($default)) {
227 $default = "'$default'";
244 Moose::Meta::Method::Constructor - Method Meta Object for constructors
258 =item B<meta_instance>
262 =item B<intialize_body>
264 =item B<associated_metaclass>
270 Stevan Little E<lt>stevan@iinteractive.comE<gt>
272 =head1 COPYRIGHT AND LICENSE
274 Copyright 2006 by Infinity Interactive, Inc.
276 L<http://www.iinteractive.com>
278 This library is free software; you can redistribute it and/or modify
279 it under the same terms as Perl itself.