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 {
52 $self->{'body'} = $self->_generate_constructor_method_inline;
55 sub _eval_environment {
58 my $attrs = $self->_attributes;
60 my $defaults = [map { $_->default } @$attrs];
62 # We need to check if the attribute ->can('type_constraint')
63 # since we may be trying to immutabilize a Moose meta class,
64 # which in turn has attributes which are Class::MOP::Attribute
65 # objects, rather than Moose::Meta::Attribute. And
66 # Class::MOP::Attribute attributes have no type constraints.
67 # However we need to make sure we leave an undef value there
68 # because the inlined code is using the index of the attributes
69 # to determine where to find the type constraint
71 my @type_constraints = map {
72 $_->can('type_constraint') ? $_->type_constraint : undef
75 my @type_constraint_bodies = map {
76 defined $_ ? $_->_compiled_type_constraint : undef;
82 '$defaults' => \$defaults,
83 '@type_constraints' => \@type_constraints,
84 '@type_constraint_bodies' => \@type_constraint_bodies,
88 sub _generate_constructor_method_inline {
91 # the %options should also include a both
92 # a call 'initializer' and call 'SUPER::'
93 # options, which should cover approx 90%
94 # of the possible use cases (even if it
95 # requires some adaption on the part of
96 # the author, after all, nothing is free)
100 'my $_instance = shift;',
101 'my $class = Scalar::Util::blessed($_instance) || $_instance;',
102 'if ($class ne \'' . $self->associated_metaclass->name . '\') {',
103 'return ' . $self->_generate_fallback_constructor('$class') . ';',
105 $self->_generate_params('$params', '$class'),
106 $self->_generate_instance('$instance', '$class'),
107 $self->_generate_slot_initializers,
108 $self->_generate_triggers,
109 $self->_generate_BUILDALL,
113 warn join("\n", @source) if $self->options->{debug};
116 $self->_compile_code(\@source);
119 my $source = join("\n", @source);
121 "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_",
128 sub _generate_fallback_constructor {
130 my ($class_var) = @_;
131 return $class_var . '->Moose::Object::new(@_)'
134 sub _generate_params {
136 my ($var, $class_var) = @_;
138 'my ' . $var . ' = ',
139 $self->_generate_BUILDARGS($class_var, '@_'),
144 sub _generate_instance {
146 my ($var, $class_var) = @_;
147 my $meta = $self->associated_metaclass;
150 'my ' . $var . ' = ',
151 $meta->inline_create_instance($class_var) . ';',
155 sub _generate_slot_initializers {
157 return map { $self->_generate_slot_initializer($_) }
158 0 .. (@{$self->_attributes} - 1);
161 sub _generate_BUILDARGS {
163 my ($class, $args) = @_;
165 my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS");
168 && (!$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS)) {
173 'if (scalar @_ == 1) {',
174 'if (!defined($_[0]) || ref($_[0]) ne \'HASH\') {',
175 $self->_inline_throw_error(
176 '"Single parameters to new() must be a HASH ref"',
180 '$params = { %{ $_[0] } };',
184 '"The new() method for ' . $class . ' expects a '
185 . 'hash reference or a key/value list. You passed an '
186 . 'odd number of arguments"',
188 '$params = {@_, undef};',
198 return $class . '->BUILDARGS(' . $args . ')';
202 sub _generate_BUILDALL {
205 my @methods = reverse $self->associated_metaclass->find_all_methods_by_name('BUILD');
208 foreach my $method (@methods) {
210 '$instance->' . $method->{class} . '::BUILD($params);';
216 sub _generate_triggers {
220 for my $i (0 .. $#{ $self->_attributes }) {
221 my $attr = $self->_attributes->[$i];
223 next unless $attr->can('has_trigger') && $attr->has_trigger;
225 my $init_arg = $attr->init_arg;
226 next unless defined $init_arg;
229 'if (exists $params->{\'' . $init_arg . '\'}) {',
230 '$attrs->[' . $i . ']->trigger->(',
232 $attr->_inline_instance_get('$instance') . ',',
237 return @trigger_calls;
240 sub _generate_slot_initializer {
244 my $attr = $self->_attributes->[$index];
246 my @source = ('## ' . $attr->name);
248 push @source, $self->_check_required_attr($attr);
250 if (defined $attr->init_arg) {
252 'if (exists $params->{\'' . $attr->init_arg . '\'}) {',
253 $self->_init_attr_from_constructor($attr, $index),
255 if (my @default = $self->_init_attr_from_default($attr, $index)) {
263 if (my @default = $self->_init_attr_from_default($attr, $index)) {
265 '{', # _init_attr_from_default creates variables
274 sub _check_required_attr {
278 return unless defined $attr->init_arg;
279 return unless $attr->can('is_required') && $attr->is_required;
280 return if $attr->has_default || $attr->has_builder;
283 'if (!exists $params->{\'' . $attr->init_arg . '\'}) {',
284 $self->_inline_throw_error(
285 '"Attribute (' . quotemeta($attr->name) . ') is required"'
291 sub _init_attr_from_constructor {
293 my ($attr, $index) = @_;
296 'my $val = $params->{\'' . $attr->init_arg . '\'};',
297 $self->_generate_slot_assignment($attr, $index, '$val'),
301 sub _init_attr_from_default {
303 my ($attr, $index) = @_;
305 my $default = $self->_generate_default_value($attr, $index);
306 return unless $default;
309 'my $val = ' . $default . ';',
310 $self->_generate_slot_assignment($attr, $index, '$val'),
314 sub _generate_slot_assignment {
316 my ($attr, $index, $value) = @_;
320 if ($self->can('_generate_type_constraint_and_coercion')) {
321 push @source, $self->_generate_type_constraint_and_coercion(
322 $attr, $index, $value,
326 if ($attr->has_initializer) {
328 '$attrs->[' . $index . ']->set_initial_value(',
336 $attr->_inline_instance_set('$instance', $value) . ';',
343 sub _generate_type_constraint_and_coercion {
345 my ($attr, $index, $value) = @_;
347 return unless $attr->can('has_type_constraint')
348 && $attr->has_type_constraint;
352 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
353 push @source => $self->_generate_type_coercion(
354 '$type_constraints[' . $index . ']',
360 push @source => $self->_generate_type_constraint_check(
362 '$type_constraint_bodies[' . $index . ']',
363 '$type_constraints[' . $index . ']',
370 sub _generate_type_coercion {
372 my ($tc_obj, $value, $return_value) = @_;
373 return $return_value . ' = ' . $tc_obj . '->coerce(' . $value . ');';
376 sub _generate_type_constraint_check {
378 my ($attr, $tc_body, $tc_obj, $value) = @_;
380 $self->_inline_throw_error(
381 '"Attribute (' . quotemeta($attr->name) . ') '
382 . 'does not pass the type constraint because: " . '
383 . $tc_obj . '->get_message(' . $value . ')'
385 'unless ' . $tc_body . '->(' . $value . ');'
397 Moose::Meta::Method::Constructor - Method Meta Object for constructors
401 This class is a subclass of L<Class::MOP::Method::Constructor> that
402 provides additional Moose-specific functionality
404 To understand this class, you should read the the
405 L<Class::MOP::Method::Constructor> documentation as well.
409 C<Moose::Meta::Method::Constructor> is a subclass of
410 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
414 See L<Moose/BUGS> for details on reporting bugs.
418 Stevan Little E<lt>stevan@iinteractive.comE<gt>
420 =head1 COPYRIGHT AND LICENSE
422 Copyright 2006-2010 by Infinity Interactive, Inc.
424 L<http://www.iinteractive.com>
426 This library is free software; you can redistribute it and/or modify
427 it under the same terms as Perl itself.