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" . 'return $class->Moose::Object::new(@_)';
63 $source .= "\n if \$class ne '" . $self->associated_metaclass->name
66 $source .= $self->_generate_params('$params', '$class');
67 $source .= $self->_generate_instance('$instance', '$class');
68 $source .= $self->_generate_slot_initializers;
70 $source .= $self->_generate_triggers();
71 $source .= ";\n" . $self->_generate_BUILDALL();
73 $source .= ";\nreturn \$instance";
74 $source .= ";\n" . '}';
75 warn $source if $self->options->{debug};
77 # We need to check if the attribute ->can('type_constraint')
78 # since we may be trying to immutabilize a Moose meta class,
79 # which in turn has attributes which are Class::MOP::Attribute
80 # objects, rather than Moose::Meta::Attribute. And
81 # Class::MOP::Attribute attributes have no type constraints.
82 # However we need to make sure we leave an undef value there
83 # because the inlined code is using the index of the attributes
84 # to determine where to find the type constraint
86 my $attrs = $self->_attributes;
88 my @type_constraints = map {
89 $_->can('type_constraint') ? $_->type_constraint : undef
92 my @type_constraint_bodies = map {
93 defined $_ ? $_->_compiled_type_constraint : undef;
96 my ( $code, $e ) = $self->_compile_code(
101 '@type_constraints' => \@type_constraints,
102 '@type_constraint_bodies' => \@type_constraint_bodies,
107 "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e",
108 error => $e, data => $source )
111 $self->{'body'} = $code;
114 sub _generate_params {
115 my ( $self, $var, $class_var ) = @_;
116 "my $var = " . $self->_generate_BUILDARGS( $class_var, '@_' ) . ";\n";
119 sub _generate_instance {
120 my ( $self, $var, $class_var ) = @_;
122 . $self->_meta_instance->inline_create_instance($class_var) . ";\n";
125 sub _generate_slot_initializers {
127 return (join ";\n" => map {
128 $self->_generate_slot_initializer($_)
129 } 0 .. (@{$self->_attributes} - 1)) . ";\n";
132 sub _generate_BUILDARGS {
133 my ( $self, $class, $args ) = @_;
135 my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS");
137 if ( $args eq '@_' and ( !$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS ) ) {
140 $self->_inline_throw_error('"Single parameters to new() must be a HASH ref"', 'data => $_[0]'),
141 ' if scalar @_ == 1 && !( defined $_[0] && ref $_[0] eq q{HASH} );',
142 '(scalar @_ == 1) ? {%{$_[0]}} : {@_};',
146 return $class . "->BUILDARGS($args)";
150 sub _generate_BUILDALL {
153 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
154 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD($params)';
156 return join ";\n" => @BUILD_calls;
159 sub _generate_triggers {
162 foreach my $i ( 0 .. $#{ $self->_attributes } ) {
163 my $attr = $self->_attributes->[$i];
165 next unless $attr->can('has_trigger') && $attr->has_trigger;
167 my $init_arg = $attr->init_arg;
169 next unless defined $init_arg;
171 push @trigger_calls => '(exists $params->{\''
179 . $self->_meta_instance->inline_get_slot_value(
187 return join ";\n" => @trigger_calls;
190 sub _generate_slot_initializer {
194 my $attr = $self->_attributes->[$index];
196 my @source = ('## ' . $attr->name);
198 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
200 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
201 push @source => ('(exists $params->{\'' . $attr->init_arg . '\'}) ' .
202 '|| ' . $self->_inline_throw_error('"Attribute (' . quotemeta($attr->name) . ') is required"') .';');
205 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
207 if ( defined( my $init_arg = $attr->init_arg ) ) {
208 push @source => 'if (exists $params->{\'' . $init_arg . '\'}) {';
209 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
210 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
212 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
213 push @source => "} else {";
216 if ( $attr->has_default ) {
217 $default = $self->_generate_default_value($attr, $index);
220 my $builder = $attr->builder;
221 $default = '$instance->' . $builder;
224 push @source => '{'; # wrap this to avoid my $val overwrite warnings
225 push @source => ('my $val = ' . $default . ';');
226 push @source => $self->_generate_type_constraint_and_coercion($attr, $index)
228 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
229 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
231 push @source => "}" if defined $attr->init_arg;
233 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
234 push @source => '(exists $params->{\'' . $init_arg . '\'}) && do {';
236 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
237 if ($is_moose && $attr->has_type_constraint) {
238 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
239 push @source => $self->_generate_type_coercion(
241 '$type_constraints[' . $index . ']',
246 push @source => $self->_generate_type_constraint_check(
248 '$type_constraint_bodies[' . $index . ']',
249 '$type_constraints[' . $index . ']',
253 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
258 return join "\n" => @source;
261 sub _generate_slot_assignment {
262 my ($self, $attr, $value, $index) = @_;
266 if ($attr->has_initializer) {
268 '$attrs->[' . $index . ']->set_initial_value($instance, ' . $value . ');'
273 $self->_meta_instance->inline_set_slot_value(
281 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
283 if ($is_moose && $attr->is_weak_ref) {
286 $self->_meta_instance->inline_weaken_slot_value(
290 ' if ref ' . $value . ';'
297 sub _generate_type_constraint_and_coercion {
298 my ($self, $attr, $index) = @_;
300 return unless $attr->has_type_constraint;
303 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
304 push @source => $self->_generate_type_coercion(
306 '$type_constraints[' . $index . ']',
311 push @source => $self->_generate_type_constraint_check(
313 ('$type_constraint_bodies[' . $index . ']'),
314 ('$type_constraints[' . $index . ']'),
320 sub _generate_type_coercion {
321 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
322 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
325 sub _generate_type_constraint_check {
326 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
328 $self->_inline_throw_error('"Attribute (' # FIXME add 'dad'
329 . quotemeta( $attr->name )
330 . ') does not pass the type constraint because: " . '
331 . $type_constraint_obj . '->get_message(' . $value_name . ')')
332 . "\n\t unless " . $type_constraint_cv . '->(' . $value_name . ');'
336 sub _generate_default_value {
337 my ($self, $attr, $index) = @_;
339 # default values can either be CODE refs
340 # in which case we need to call them. Or
341 # they can be scalars (strings/numbers)
342 # in which case we can just deal with them
343 # in the code we eval.
344 if ($attr->is_default_a_coderef) {
345 return '$attrs->[' . $index . ']->default($instance)';
348 return q{"} . quotemeta( $attr->default ) . q{"};
360 Moose::Meta::Method::Constructor - Method Meta Object for constructors
364 This class is a subclass of L<Class::MOP::Class::Constructor> that
365 provides additional Moose-specific functionality
367 To understand this class, you should read the the
368 L<Class::MOP::Class::Constructor> documentation as well.
372 C<Moose::Meta::Method::Constructor> is a subclass of
373 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
377 See L<Moose/BUGS> for details on reporting bugs.
381 Stevan Little E<lt>stevan@iinteractive.comE<gt>
383 =head1 COPYRIGHT AND LICENSE
385 Copyright 2006-2010 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.