2 package Moose::Meta::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
11 our $VERSION = '1.19';
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Moose::Meta::Method',
15 'Class::MOP::Method::Constructor';
21 my $meta = $options{metaclass};
23 (ref $options{options} eq 'HASH')
24 || $class->throw_error("You must pass a hash of options", data => $options{options});
26 ($options{package_name} && $options{name})
27 || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
31 'package_name' => $options{package_name},
32 'name' => $options{name},
33 'options' => $options{options},
34 'associated_metaclass' => $meta,
35 '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object',
38 # we don't want this creating
39 # a cycle in the code, if not
41 weaken($self->{'associated_metaclass'});
43 $self->_initialize_body;
50 sub _initialize_body {
53 # the %options should also include a both
54 # a call 'initializer' and call 'SUPER::'
55 # options, which should cover approx 90%
56 # of the possible use cases (even if it
57 # requires some adaption on the part of
58 # the author, after all, nothing is free)
60 $source .= "\n" . 'my $_instance = shift;';
62 $source .= "\n" . 'my $class = Scalar::Util::blessed($_instance) || $_instance;';
64 $source .= "\n" . "if (\$class ne '" . $self->associated_metaclass->name
66 $source .= "\n return "
67 . $self->_generate_fallback_constructor('$class') . ";";
70 $source .= $self->_generate_params('$params', '$class');
71 $source .= $self->_generate_instance('$instance', '$class');
72 $source .= $self->_generate_slot_initializers;
74 $source .= $self->_generate_triggers();
75 $source .= ";\n" . $self->_generate_BUILDALL();
77 $source .= ";\nreturn \$instance";
78 $source .= ";\n" . '}';
79 warn $source if $self->options->{debug};
81 # We need to check if the attribute ->can('type_constraint')
82 # since we may be trying to immutabilize a Moose meta class,
83 # which in turn has attributes which are Class::MOP::Attribute
84 # objects, rather than Moose::Meta::Attribute. And
85 # Class::MOP::Attribute attributes have no type constraints.
86 # However we need to make sure we leave an undef value there
87 # because the inlined code is using the index of the attributes
88 # to determine where to find the type constraint
90 my $attrs = $self->_attributes;
92 my @type_constraints = map {
93 $_->can('type_constraint') ? $_->type_constraint : undef
96 my @type_constraint_bodies = map {
97 defined $_ ? $_->_compiled_type_constraint : undef;
100 my $defaults = [map { $_->default } @$attrs];
103 $self->_compile_code(
108 '$defaults' => \$defaults,
109 '@type_constraints' => \@type_constraints,
110 '@type_constraint_bodies' => \@type_constraint_bodies,
116 "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_",
122 $self->{'body'} = $code;
125 sub _generate_fallback_constructor {
126 my ( $self, $class_var ) = @_;
127 "${class_var}->Moose::Object::new(\@_)";
130 sub _generate_params {
131 my ( $self, $var, $class_var ) = @_;
132 "my $var = " . $self->_generate_BUILDARGS( $class_var, '@_' ) . ";\n";
135 sub _generate_instance {
136 my ( $self, $var, $class_var ) = @_;
138 . $self->associated_metaclass->inline_create_instance($class_var) . ";\n";
141 sub _generate_slot_initializers {
143 return (join ";\n" => map {
144 $self->_generate_slot_initializer($_)
145 } 0 .. (@{$self->_attributes} - 1)) . ";\n";
148 sub _generate_BUILDARGS {
149 my ( $self, $class, $args ) = @_;
151 my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS");
154 and ( !$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS )
157 # This is the same logic as Moose::Object::BUILDARGS
158 return sprintf( <<'EOF', $self->_inline_throw_error( q{'Single parameters to new() must be a HASH ref'}, 'data => $_[0]' ) );
161 if ( scalar @_ == 1 ) {
162 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
165 $params = { %%{ $_[0] } };
169 "The new() method for $class expects a hash reference or a key/value list."
170 . " You passed an odd number of arguments" );
171 $params = { @_, undef };
182 return $class . "->BUILDARGS($args)";
186 sub _generate_BUILDALL {
189 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
190 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD($params)';
192 return join ";\n" => @BUILD_calls;
195 sub _generate_triggers {
198 foreach my $i ( 0 .. $#{ $self->_attributes } ) {
199 my $attr = $self->_attributes->[$i];
201 next unless $attr->can('has_trigger') && $attr->has_trigger;
203 my $init_arg = $attr->init_arg;
205 next unless defined $init_arg;
207 push @trigger_calls => '(exists $params->{\''
215 . $attr->inline_get('$instance')
220 return join ";\n" => @trigger_calls;
223 sub _generate_slot_initializer {
227 my $attr = $self->_attributes->[$index];
229 my @source = ('## ' . $attr->name);
231 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
233 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
234 push @source => ('(exists $params->{\'' . $attr->init_arg . '\'}) ' .
235 '|| ' . $self->_inline_throw_error('"Attribute (' . quotemeta($attr->name) . ') is required"') .';');
238 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
240 if ( defined( my $init_arg = $attr->init_arg ) ) {
241 push @source => 'if (exists $params->{\'' . $init_arg . '\'}) {';
242 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
243 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
245 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
246 push @source => "} else {";
249 if ( $attr->has_default ) {
250 $default = $self->_generate_default_value($attr, $index);
253 my $builder = $attr->builder;
254 $default = '$instance->' . $builder;
257 push @source => '{'; # wrap this to avoid my $val overwrite warnings
258 push @source => ('my $val = ' . $default . ';');
259 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
261 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
262 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
264 push @source => "}" if defined $attr->init_arg;
266 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
267 push @source => '(exists $params->{\'' . $init_arg . '\'}) && do {';
269 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
270 if ($is_moose && $attr->has_type_constraint) {
271 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
272 push @source => $self->_generate_type_coercion(
274 '$type_constraints[' . $index . ']',
279 push @source => $self->_generate_type_constraint_check(
281 '$type_constraint_bodies[' . $index . ']',
282 '$type_constraints[' . $index . ']',
286 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
291 return join "\n" => @source;
294 sub _generate_slot_assignment {
295 my ($self, $attr, $value, $index) = @_;
299 if ( $attr->has_initializer ) {
303 . ']->set_initial_value($instance, '
307 return $attr->inline_set(
316 sub _generate_type_constraint_and_coercion {
317 my ($self, $attr, $index) = @_;
319 return unless $attr->has_type_constraint;
322 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
323 push @source => $self->_generate_type_coercion(
325 '$type_constraints[' . $index . ']',
330 push @source => $self->_generate_type_constraint_check(
332 ('$type_constraint_bodies[' . $index . ']'),
333 ('$type_constraints[' . $index . ']'),
339 sub _generate_type_coercion {
340 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
341 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
344 sub _generate_type_constraint_check {
345 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
347 $self->_inline_throw_error('"Attribute (' # FIXME add 'dad'
348 . quotemeta( $attr->name )
349 . ') does not pass the type constraint because: " . '
350 . $type_constraint_obj . '->get_message(' . $value_name . ')')
351 . "\n\t unless " . $type_constraint_cv . '->(' . $value_name . ');'
363 Moose::Meta::Method::Constructor - Method Meta Object for constructors
367 This class is a subclass of L<Class::MOP::Method::Constructor> that
368 provides additional Moose-specific functionality
370 To understand this class, you should read the the
371 L<Class::MOP::Method::Constructor> documentation as well.
375 C<Moose::Meta::Method::Constructor> is a subclass of
376 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
380 See L<Moose/BUGS> for details on reporting bugs.
384 Stevan Little E<lt>stevan@iinteractive.comE<gt>
386 =head1 COPYRIGHT AND LICENSE
388 Copyright 2006-2010 by Infinity Interactive, Inc.
390 L<http://www.iinteractive.com>
392 This library is free software; you can redistribute it and/or modify
393 it under the same terms as Perl itself.