2 package Moose::Meta::Method::Constructor;
7 use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Moose::Meta::Method',
13 'Class::MOP::Method::Constructor';
19 my $meta = $options{metaclass};
21 (ref $options{options} eq 'HASH')
22 || $class->throw_error("You must pass a hash of options", data => $options{options});
24 ($options{package_name} && $options{name})
25 || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
29 'package_name' => $options{package_name},
30 'name' => $options{name},
31 'options' => $options{options},
32 'associated_metaclass' => $meta,
33 '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object',
36 # we don't want this creating
37 # a cycle in the code, if not
39 weaken($self->{'associated_metaclass'});
41 $self->_initialize_body;
48 sub _initialize_body {
51 # the %options should also include a both
52 # a call 'initializer' and call 'SUPER::'
53 # options, which should cover approx 90%
54 # of the possible use cases (even if it
55 # requires some adaption on the part of
56 # the author, after all, nothing is free)
58 $source .= "\n" . 'my $_instance = shift;';
60 $source .= "\n" . 'my $class = Scalar::Util::blessed($_instance) || $_instance;';
62 $source .= "\n" . "if (\$class ne '" . $self->associated_metaclass->name
64 $source .= "\n return "
65 . $self->_generate_fallback_constructor('$class') . ";";
68 $source .= $self->_generate_params('$params', '$class');
69 $source .= $self->_generate_instance('$instance', '$class');
70 $source .= $self->_generate_slot_initializers;
72 $source .= $self->_generate_triggers();
73 $source .= ";\n" . $self->_generate_BUILDALL();
75 $source .= ";\nreturn \$instance";
76 $source .= ";\n" . '}';
77 warn $source if $self->options->{debug};
79 # We need to check if the attribute ->can('type_constraint')
80 # since we may be trying to immutabilize a Moose meta class,
81 # which in turn has attributes which are Class::MOP::Attribute
82 # objects, rather than Moose::Meta::Attribute. And
83 # Class::MOP::Attribute attributes have no type constraints.
84 # However we need to make sure we leave an undef value there
85 # because the inlined code is using the index of the attributes
86 # to determine where to find the type constraint
88 my $attrs = $self->_attributes;
90 my @type_constraints = map {
91 $_->can('type_constraint') ? $_->type_constraint : undef
94 my @type_constraint_bodies = map {
95 defined $_ ? $_->_compiled_type_constraint : undef;
98 my $defaults = [map { $_->default } @$attrs];
100 my ( $code, $e ) = $self->_compile_code(
105 '$defaults' => \$defaults,
106 '@type_constraints' => \@type_constraints,
107 '@type_constraint_bodies' => \@type_constraint_bodies,
112 "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e",
113 error => $e, data => $source )
116 $self->{'body'} = $code;
119 sub _generate_fallback_constructor {
120 my ( $self, $class_var ) = @_;
121 "${class_var}->Moose::Object::new(\@_)";
124 sub _generate_params {
125 my ( $self, $var, $class_var ) = @_;
126 "my $var = " . $self->_generate_BUILDARGS( $class_var, '@_' ) . ";\n";
129 sub _generate_instance {
130 my ( $self, $var, $class_var ) = @_;
132 . $self->associated_metaclass->inline_create_instance($class_var) . ";\n";
135 sub _generate_slot_initializers {
137 return (join ";\n" => map {
138 $self->_generate_slot_initializer($_)
139 } 0 .. (@{$self->_attributes} - 1)) . ";\n";
142 sub _generate_BUILDARGS {
143 my ( $self, $class, $args ) = @_;
145 my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS");
147 if ( $args eq '@_' and ( !$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS ) ) {
150 $self->_inline_throw_error('"Single parameters to new() must be a HASH ref"', 'data => $_[0]'),
151 ' if scalar @_ == 1 && !( defined $_[0] && ref $_[0] eq q{HASH} );',
152 '(scalar @_ == 1) ? {%{$_[0]}} : {@_};',
156 return $class . "->BUILDARGS($args)";
160 sub _generate_BUILDALL {
163 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
164 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD($params)';
166 return join ";\n" => @BUILD_calls;
169 sub _generate_triggers {
172 foreach my $i ( 0 .. $#{ $self->_attributes } ) {
173 my $attr = $self->_attributes->[$i];
175 next unless $attr->can('has_trigger') && $attr->has_trigger;
177 my $init_arg = $attr->init_arg;
179 next unless defined $init_arg;
181 push @trigger_calls => '(exists $params->{\''
189 . $attr->inline_get('$instance')
194 return join ";\n" => @trigger_calls;
197 sub _generate_slot_initializer {
201 my $attr = $self->_attributes->[$index];
203 my @source = ('## ' . $attr->name);
205 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
207 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
208 push @source => ('(exists $params->{\'' . $attr->init_arg . '\'}) ' .
209 '|| ' . $self->_inline_throw_error('"Attribute (' . quotemeta($attr->name) . ') is required"') .';');
212 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
214 if ( defined( my $init_arg = $attr->init_arg ) ) {
215 push @source => 'if (exists $params->{\'' . $init_arg . '\'}) {';
216 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
217 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
219 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
220 push @source => "} else {";
223 if ( $attr->has_default ) {
224 $default = $self->_generate_default_value($attr, $index);
227 my $builder = $attr->builder;
228 $default = '$instance->' . $builder;
231 push @source => '{'; # wrap this to avoid my $val overwrite warnings
232 push @source => ('my $val = ' . $default . ';');
233 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
235 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
236 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
238 push @source => "}" if defined $attr->init_arg;
240 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
241 push @source => '(exists $params->{\'' . $init_arg . '\'}) && do {';
243 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
244 if ($is_moose && $attr->has_type_constraint) {
245 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
246 push @source => $self->_generate_type_coercion(
248 '$type_constraints[' . $index . ']',
253 push @source => $self->_generate_type_constraint_check(
255 '$type_constraint_bodies[' . $index . ']',
256 '$type_constraints[' . $index . ']',
260 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
265 return join "\n" => @source;
268 sub _generate_slot_assignment {
269 my ($self, $attr, $value, $index) = @_;
273 if ( $attr->has_initializer ) {
277 . ']->set_initial_value($instance, '
281 return $attr->inline_set(
290 sub _generate_type_constraint_and_coercion {
291 my ($self, $attr, $index) = @_;
293 return unless $attr->has_type_constraint;
296 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
297 push @source => $self->_generate_type_coercion(
299 '$type_constraints[' . $index . ']',
304 push @source => $self->_generate_type_constraint_check(
306 ('$type_constraint_bodies[' . $index . ']'),
307 ('$type_constraints[' . $index . ']'),
313 sub _generate_type_coercion {
314 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
315 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
318 sub _generate_type_constraint_check {
319 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
321 $self->_inline_throw_error('"Attribute (' # FIXME add 'dad'
322 . quotemeta( $attr->name )
323 . ') does not pass the type constraint because: " . '
324 . $type_constraint_obj . '->get_message(' . $value_name . ')')
325 . "\n\t unless " . $type_constraint_cv . '->(' . $value_name . ');'
337 Moose::Meta::Method::Constructor - Method Meta Object for constructors
341 This class is a subclass of L<Class::MOP::Method::Constructor> that
342 provides additional Moose-specific functionality
344 To understand this class, you should read the the
345 L<Class::MOP::Method::Constructor> documentation as well.
349 C<Moose::Meta::Method::Constructor> is a subclass of
350 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
354 See L<Moose/BUGS> for details on reporting bugs.
358 Stevan Little E<lt>stevan@iinteractive.comE<gt>
360 =head1 COPYRIGHT AND LICENSE
362 Copyright 2006-2010 by Infinity Interactive, Inc.
364 L<http://www.iinteractive.com>
366 This library is free software; you can redistribute it and/or modify
367 it under the same terms as Perl itself.