2 package Moose::Meta::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.03';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Method',
14 'Class::MOP::Method::Generated';
20 (exists $options{options} && ref $options{options} eq 'HASH')
21 || confess "You must pass a hash of options";
26 # specific to this subclass
27 '%!options' => $options{options},
28 '$!meta_instance' => $options{metaclass}->get_meta_instance,
29 '@!attributes' => [ $options{metaclass}->compute_all_applicable_attributes ],
31 '$!associated_metaclass' => $options{metaclass},
34 # we don't want this creating
35 # a cycle in the code, if not
37 weaken($self->{'$!associated_metaclass'});
39 $self->intialize_body;
46 sub options { (shift)->{'%!options'} }
47 sub meta_instance { (shift)->{'$!meta_instance'} }
48 sub attributes { (shift)->{'@!attributes'} }
50 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
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
62 # the author, after all, nothing is free)
64 $source .= "\n" . 'my $class = shift;';
66 $source .= "\n" . 'return $class->Moose::Object::new(@_)';
67 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
69 $source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;';
71 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
73 $source .= ";\n" . (join ";\n" => map {
74 $self->_generate_slot_initializer($_)
75 } 0 .. (@{$self->attributes} - 1));
77 $source .= ";\n" . $self->_generate_BUILDALL();
79 $source .= ";\n" . 'return $instance';
80 $source .= ";\n" . '}';
81 warn $source if $self->options->{debug};
86 # create the nessecary lexicals
87 # to be picked up in the eval
88 my $attrs = $self->attributes;
91 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
93 $self->{'&!body'} = $code;
96 sub _generate_BUILDALL {
99 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
100 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)';
102 return join ";\n" => @BUILD_calls;
105 sub _generate_slot_initializer {
109 my $attr = $self->attributes->[$index];
111 my @source = ('## ' . $attr->name);
113 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
115 if ($is_moose && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
116 push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' .
117 '|| confess "Attribute (' . $attr->name . ') is required";');
120 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
122 push @source => 'if (exists $params{\'' . $attr->init_arg . '\'}) {';
124 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
125 if ($is_moose && $attr->has_type_constraint) {
126 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
128 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
129 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
131 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
133 push @source => $self->_generate_slot_assignment($attr, '$val');
135 push @source => "} else {";
138 if ( $attr->has_default ) {
139 $default = $self->_generate_default_value($attr, $index);
142 my $builder = $attr->builder;
143 $default = '$instance->' . $builder;
145 push @source => ('my $val = ' . $default . ';');
146 push @source => $self->_generate_type_constraint_check(
148 ('$attrs->[' . $index . ']->type_constraint'),
150 ) if ($is_moose && $attr->has_type_constraint);
151 push @source => $self->_generate_slot_assignment($attr, $default);
156 push @source => '(exists $params{\'' . $attr->init_arg . '\'}) && do {';
158 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
159 if ($is_moose && $attr->has_type_constraint) {
160 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
162 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
163 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
165 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
167 push @source => $self->_generate_slot_assignment($attr, '$val');
172 return join "\n" => @source;
175 sub _generate_slot_assignment {
176 my ($self, $attr, $value) = @_;
178 $self->meta_instance->inline_set_slot_value(
180 ("'" . $attr->name . "'"),
185 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
187 if ($is_moose && $attr->is_weak_ref) {
190 $self->meta_instance->inline_weaken_slot_value(
192 ("'" . $attr->name . "'")
194 ' if ref ' . $value . ';'
201 sub _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 . ');');
206 sub _generate_type_constraint_check {
207 my ($self, $attr, $type_constraint_name, $value_name) = @_;
209 'defined(' . $type_constraint_name . '->_compiled_type_constraint->(' . $value_name . '))'
210 . "\n\t" . '|| confess "Attribute (' . $attr->name . ') does not pass the type constraint ('
211 . $attr->type_constraint->name
212 . ') with " . (defined(' . $value_name . ') ? (Scalar::Util::blessed(' . $value_name . ') && overload::Overloaded(' . $value_name . ') ? overload::StrVal(' . $value_name . ') : ' . $value_name . ') : "undef");'
216 sub _generate_default_value {
217 my ($self, $attr, $index) = @_;
219 # default values can either be CODE refs
220 # in which case we need to call them. Or
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)';
228 my $default = $attr->default;
229 # make sure to quote strings ...
230 unless (looks_like_number($default)) {
231 $default = "'$default'";
246 Moose::Meta::Method::Constructor - Method Meta Object for constructors
250 This is a subclass of L<Class::MOP::Method> which handles
251 constructing an approprate Constructor methods. This is primarily
252 used in the making of immutable metaclasses, otherwise it is
253 not particularly useful.
263 =item B<meta_instance>
267 =item B<intialize_body>
269 =item B<associated_metaclass>
275 Stevan Little E<lt>stevan@iinteractive.comE<gt>
277 =head1 COPYRIGHT AND LICENSE
279 Copyright 2006, 2007 by Infinity Interactive, Inc.
281 L<http://www.iinteractive.com>
283 This library is free software; you can redistribute it and/or modify
284 it under the same terms as Perl itself.