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;
46 # This is here so can_be_inlined can be inherited by MooseX modules.
47 sub _expected_constructor_class {
48 return 'Moose::Object';
53 sub _initialize_body {
56 # the %options should also include a both
57 # a call 'initializer' and call 'SUPER::'
58 # options, which should cover approx 90%
59 # of the possible use cases (even if it
60 # requires some adaption on the part of
61 # the author, after all, nothing is free)
63 $source .= "\n" . 'my $_instance = shift;';
64 $source .= "\n" . 'my $class = Scalar::Util::blessed($_instance) || $_instance;';
66 $source .= "\n" . 'return $class->Moose::Object::new(@_)';
67 $source .= "\n if \$class ne '" . $self->associated_metaclass->name
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 ( $code, $e ) = $self->_compile_code(
105 '@type_constraints' => \@type_constraints,
106 '@type_constraint_bodies' => \@type_constraint_bodies,
111 "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e",
112 error => $e, data => $source )
115 $self->{'body'} = $code;
118 sub _generate_params {
119 my ( $self, $var, $class_var ) = @_;
120 "my $var = " . $self->_generate_BUILDARGS( $class_var, '@_' ) . ";\n";
123 sub _generate_instance {
124 my ( $self, $var, $class_var ) = @_;
126 . $self->_meta_instance->inline_create_instance($class_var) . ";\n";
129 sub _generate_slot_initializers {
131 return (join ";\n" => map {
132 $self->_generate_slot_initializer($_)
133 } 0 .. (@{$self->_attributes} - 1)) . ";\n";
136 sub _generate_BUILDARGS {
137 my ( $self, $class, $args ) = @_;
139 my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS");
141 if ( $args eq '@_' and ( !$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS ) ) {
144 $self->_inline_throw_error('"Single parameters to new() must be a HASH ref"', 'data => $_[0]'),
145 ' if scalar @_ == 1 && !( defined $_[0] && ref $_[0] eq q{HASH} );',
146 '(scalar @_ == 1) ? {%{$_[0]}} : {@_};',
150 return $class . "->BUILDARGS($args)";
154 sub _generate_BUILDALL {
157 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
158 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD($params)';
160 return join ";\n" => @BUILD_calls;
163 sub _generate_triggers {
166 foreach my $i ( 0 .. $#{ $self->_attributes } ) {
167 my $attr = $self->_attributes->[$i];
169 next unless $attr->can('has_trigger') && $attr->has_trigger;
171 my $init_arg = $attr->init_arg;
173 next unless defined $init_arg;
175 push @trigger_calls => '(exists $params->{\''
183 . $self->_meta_instance->inline_get_slot_value(
191 return join ";\n" => @trigger_calls;
194 sub _generate_slot_initializer {
198 my $attr = $self->_attributes->[$index];
200 my @source = ('## ' . $attr->name);
202 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
204 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
205 push @source => ('(exists $params->{\'' . $attr->init_arg . '\'}) ' .
206 '|| ' . $self->_inline_throw_error('"Attribute (' . $attr->name . ') is required"') .';');
209 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
211 if ( defined( my $init_arg = $attr->init_arg ) ) {
212 push @source => 'if (exists $params->{\'' . $init_arg . '\'}) {';
213 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
214 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
216 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
217 push @source => "} else {";
220 if ( $attr->has_default ) {
221 $default = $self->_generate_default_value($attr, $index);
224 my $builder = $attr->builder;
225 $default = '$instance->' . $builder;
228 push @source => '{'; # wrap this to avoid my $val overwrite warnings
229 push @source => ('my $val = ' . $default . ';');
230 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
232 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
233 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
235 push @source => "}" if defined $attr->init_arg;
237 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
238 push @source => '(exists $params->{\'' . $init_arg . '\'}) && do {';
240 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
241 if ($is_moose && $attr->has_type_constraint) {
242 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
243 push @source => $self->_generate_type_coercion(
245 '$type_constraints[' . $index . ']',
250 push @source => $self->_generate_type_constraint_check(
252 '$type_constraint_bodies[' . $index . ']',
253 '$type_constraints[' . $index . ']',
257 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
262 return join "\n" => @source;
265 sub _generate_slot_assignment {
266 my ($self, $attr, $value, $index) = @_;
270 if ($attr->has_initializer) {
272 '$attrs->[' . $index . ']->set_initial_value($instance, ' . $value . ');'
277 $self->_meta_instance->inline_set_slot_value(
285 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
287 if ($is_moose && $attr->is_weak_ref) {
290 $self->_meta_instance->inline_weaken_slot_value(
294 ' if ref ' . $value . ';'
301 sub _generate_type_constraint_and_coercion {
302 my ($self, $attr, $index) = @_;
304 return unless $attr->has_type_constraint;
307 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
308 push @source => $self->_generate_type_coercion(
310 '$type_constraints[' . $index . ']',
315 push @source => $self->_generate_type_constraint_check(
317 ('$type_constraint_bodies[' . $index . ']'),
318 ('$type_constraints[' . $index . ']'),
324 sub _generate_type_coercion {
325 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
326 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
329 sub _generate_type_constraint_check {
330 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
332 $self->_inline_throw_error('"Attribute (' # FIXME add 'dad'
334 . ') does not pass the type constraint because: " . '
335 . $type_constraint_obj . '->get_message(' . $value_name . ')')
336 . "\n\t unless " . $type_constraint_cv . '->(' . $value_name . ');'
340 sub _generate_default_value {
341 my ($self, $attr, $index) = @_;
343 # default values can either be CODE refs
344 # in which case we need to call them. Or
345 # they can be scalars (strings/numbers)
346 # in which case we can just deal with them
347 # in the code we eval.
348 if ($attr->is_default_a_coderef) {
349 return '$attrs->[' . $index . ']->default($instance)';
352 return q{"} . quotemeta( $attr->default ) . q{"};
364 Moose::Meta::Method::Constructor - Method Meta Object for constructors
368 This class is a subclass of L<Class::MOP::Class::Constructor> that
369 provides additional Moose-specific functionality
371 To understand this class, you should read the the
372 L<Class::MOP::Class::Constructor> documentation as well.
376 C<Moose::Meta::Method::Constructor> is a subclass of
377 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
381 Stevan Little E<lt>stevan@iinteractive.comE<gt>
383 =head1 COPYRIGHT AND LICENSE
385 Copyright 2006-2009 by Infinity Interactive, Inc.
387 L<http://www.iinteractive.com>
389 This library is free software; you can redistribute it and/or modify
390 it under the same terms as Perl itself.