2 package Moose::Meta::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
10 our $VERSION = '1.20';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Method',
14 'Class::MOP::Method::Constructor';
20 my $meta = $options{metaclass};
22 (ref $options{options} eq 'HASH')
23 || $class->throw_error("You must pass a hash of options", data => $options{options});
25 ($options{package_name} && $options{name})
26 || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
30 'package_name' => $options{package_name},
31 'name' => $options{name},
32 'options' => $options{options},
33 'associated_metaclass' => $meta,
34 '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object',
37 # we don't want this creating
38 # a cycle in the code, if not
40 weaken($self->{'associated_metaclass'});
42 $self->_initialize_body;
49 sub _initialize_body {
52 # the %options should also include a both
53 # a call 'initializer' and call 'SUPER::'
54 # options, which should cover approx 90%
55 # of the possible use cases (even if it
56 # requires some adaption on the part of
57 # the author, after all, nothing is free)
59 $source .= "\n" . 'my $_instance = shift;';
61 $source .= "\n" . 'my $class = Scalar::Util::blessed($_instance) || $_instance;';
63 $source .= "\n" . "if (\$class ne '" . $self->associated_metaclass->name
65 $source .= "\n return "
66 . $self->_generate_fallback_constructor('$class') . ";";
69 $source .= $self->_generate_params('$params', '$class');
70 $source .= $self->_generate_instance('$instance', '$class');
71 $source .= $self->_generate_slot_initializers;
73 $source .= $self->_generate_triggers();
74 $source .= ";\n" . $self->_generate_BUILDALL();
76 $source .= ";\nreturn \$instance";
77 $source .= ";\n" . '}';
78 warn $source if $self->options->{debug};
80 # We need to check if the attribute ->can('type_constraint')
81 # since we may be trying to immutabilize a Moose meta class,
82 # which in turn has attributes which are Class::MOP::Attribute
83 # objects, rather than Moose::Meta::Attribute. And
84 # Class::MOP::Attribute attributes have no type constraints.
85 # However we need to make sure we leave an undef value there
86 # because the inlined code is using the index of the attributes
87 # to determine where to find the type constraint
89 my $attrs = $self->_attributes;
91 my @type_constraints = map {
92 $_->can('type_constraint') ? $_->type_constraint : undef
95 my @type_constraint_bodies = map {
96 defined $_ ? $_->_compiled_type_constraint : undef;
99 my $defaults = [map { $_->default } @$attrs];
101 my ( $code, $e ) = $self->_compile_code(
106 '$defaults' => \$defaults,
107 '@type_constraints' => \@type_constraints,
108 '@type_constraint_bodies' => \@type_constraint_bodies,
113 "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e",
114 error => $e, data => $source )
117 $self->{'body'} = $code;
120 sub _generate_fallback_constructor {
121 my ( $self, $class_var ) = @_;
122 "${class_var}->Moose::Object::new(\@_)";
125 sub _generate_params {
126 my ( $self, $var, $class_var ) = @_;
127 "my $var = " . $self->_generate_BUILDARGS( $class_var, '@_' ) . ";\n";
130 sub _generate_instance {
131 my ( $self, $var, $class_var ) = @_;
133 . $self->associated_metaclass->inline_create_instance($class_var) . ";\n";
136 sub _generate_slot_initializers {
138 return (join ";\n" => map {
139 $self->_generate_slot_initializer($_)
140 } 0 .. (@{$self->_attributes} - 1)) . ";\n";
143 sub _generate_BUILDARGS {
144 my ( $self, $class, $args ) = @_;
146 my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS");
149 and ( !$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS )
152 # This is the same logic as Moose::Object::BUILDARGS
153 return sprintf( <<'EOF', $self->_inline_throw_error( q{'Single parameters to new() must be a HASH ref'}, 'data => $_[0]' ) );
156 if ( scalar @_ == 1 ) {
157 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
160 $params = { %%{ $_[0] } };
164 "The new() method for $class expects a hash reference or a key/value list."
165 . " You passed an odd number of arguments" );
166 $params = { @_, undef };
177 return $class . "->BUILDARGS($args)";
181 sub _generate_BUILDALL {
184 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
185 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD($params)';
187 return join ";\n" => @BUILD_calls;
190 sub _generate_triggers {
193 foreach my $i ( 0 .. $#{ $self->_attributes } ) {
194 my $attr = $self->_attributes->[$i];
196 next unless $attr->can('has_trigger') && $attr->has_trigger;
198 my $init_arg = $attr->init_arg;
200 next unless defined $init_arg;
202 push @trigger_calls => '(exists $params->{\''
210 . $attr->inline_get('$instance')
215 return join ";\n" => @trigger_calls;
218 sub _generate_slot_initializer {
222 my $attr = $self->_attributes->[$index];
224 my @source = ('## ' . $attr->name);
226 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
228 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
229 push @source => ('(exists $params->{\'' . $attr->init_arg . '\'}) ' .
230 '|| ' . $self->_inline_throw_error('"Attribute (' . quotemeta($attr->name) . ') is required"') .';');
233 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
235 if ( defined( my $init_arg = $attr->init_arg ) ) {
236 push @source => 'if (exists $params->{\'' . $init_arg . '\'}) {';
237 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
238 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
240 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
241 push @source => "} else {";
244 if ( $attr->has_default ) {
245 $default = $self->_generate_default_value($attr, $index);
248 my $builder = $attr->builder;
249 $default = '$instance->' . $builder;
252 push @source => '{'; # wrap this to avoid my $val overwrite warnings
253 push @source => ('my $val = ' . $default . ';');
254 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
256 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
257 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
259 push @source => "}" if defined $attr->init_arg;
261 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
262 push @source => '(exists $params->{\'' . $init_arg . '\'}) && do {';
264 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
265 if ($is_moose && $attr->has_type_constraint) {
266 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
267 push @source => $self->_generate_type_coercion(
269 '$type_constraints[' . $index . ']',
274 push @source => $self->_generate_type_constraint_check(
276 '$type_constraint_bodies[' . $index . ']',
277 '$type_constraints[' . $index . ']',
281 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
286 return join "\n" => @source;
289 sub _generate_slot_assignment {
290 my ($self, $attr, $value, $index) = @_;
294 if ( $attr->has_initializer ) {
298 . ']->set_initial_value($instance, '
302 return $attr->inline_set(
311 sub _generate_type_constraint_and_coercion {
312 my ($self, $attr, $index) = @_;
314 return unless $attr->has_type_constraint;
317 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
318 push @source => $self->_generate_type_coercion(
320 '$type_constraints[' . $index . ']',
325 push @source => $self->_generate_type_constraint_check(
327 ('$type_constraint_bodies[' . $index . ']'),
328 ('$type_constraints[' . $index . ']'),
334 sub _generate_type_coercion {
335 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
336 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
339 sub _generate_type_constraint_check {
340 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
342 $self->_inline_throw_error('"Attribute (' # FIXME add 'dad'
343 . quotemeta( $attr->name )
344 . ') does not pass the type constraint because: " . '
345 . $type_constraint_obj . '->get_message(' . $value_name . ')')
346 . "\n\t unless " . $type_constraint_cv . '->(' . $value_name . ');'
358 Moose::Meta::Method::Constructor - Method Meta Object for constructors
362 This class is a subclass of L<Class::MOP::Method::Constructor> that
363 provides additional Moose-specific functionality
365 To understand this class, you should read the the
366 L<Class::MOP::Method::Constructor> documentation as well.
370 C<Moose::Meta::Method::Constructor> is a subclass of
371 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
375 See L<Moose/BUGS> for details on reporting bugs.
379 Stevan Little E<lt>stevan@iinteractive.comE<gt>
381 =head1 COPYRIGHT AND LICENSE
383 Copyright 2006-2010 by Infinity Interactive, Inc.
385 L<http://www.iinteractive.com>
387 This library is free software; you can redistribute it and/or modify
388 it under the same terms as Perl itself.