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 push @source => 'if ($params{\'' . $attr->init_arg . '\'}) {';
126 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
127 if ($attr->has_type_constraint) {
128 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
130 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
131 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
133 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
135 push @source => $self->_generate_slot_assignment($attr, '$val');
137 if ($attr->has_default && !$attr->is_lazy) {
139 push @source => "} else {";
141 my $default = $self->_generate_default_value($attr, $index);
143 push @source => ('my $val = ' . $default . ';');
144 push @source => $self->_generate_type_constraint_check(
146 ('$attrs->[' . $index . ']->type_constraint'),
148 ) if $attr->has_type_constraint;
149 push @source => $self->_generate_slot_assignment($attr, $default);
157 return join "\n" => @source;
160 sub _generate_slot_assignment {
161 my ($self, $attr, $value) = @_;
163 $self->meta_instance->inline_set_slot_value(
165 ("'" . $attr->name . "'"),
170 if ($attr->is_weak_ref) {
173 $self->meta_instance->inline_weaken_slot_value(
175 ("'" . $attr->name . "'")
177 ' if ref ' . $value . ';'
184 sub _generate_type_coercion {
185 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
186 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
189 sub _generate_type_constraint_check {
190 my ($self, $attr, $type_constraint_name, $value_name) = @_;
192 'defined(' . $type_constraint_name . '->_compiled_type_constraint->(' . $value_name . '))'
193 . "\n\t" . '|| confess "Attribute (' . $attr->name . ') does not pass the type constraint ('
194 . $attr->type_constraint->name . ') with " . (defined() ? "' . $value_name . '" : "undef");'
198 sub _generate_default_value {
199 my ($self, $attr, $index) = @_;
201 # default values can either be CODE refs
202 # in which case we need to call them. Or
203 # they can be scalars (strings/numbers)
204 # in which case we can just deal with them
205 # in the code we eval.
206 if ($attr->is_default_a_coderef) {
207 return '$attrs->[' . $index . ']->default($instance)';
210 my $default = $attr->default;
211 # make sure to quote strings ...
212 unless (looks_like_number($default)) {
213 $default = "'$default'";
230 Moose::Meta::Method::Constructor - Method Meta Object for constructors
244 =item B<meta_instance>
248 =item B<intialize_body>
250 =item B<associated_metaclass>
256 Stevan Little E<lt>stevan@iinteractive.comE<gt>
258 =head1 COPYRIGHT AND LICENSE
260 Copyright 2006 by Infinity Interactive, Inc.
262 L<http://www.iinteractive.com>
264 This library is free software; you can redistribute it and/or modify
265 it under the same terms as Perl itself.