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 {
53 # the %options should also include a both
54 # a call 'initializer' and call 'SUPER::'
55 # options, which should cover approx 90%
56 # of the possible use cases (even if it
57 # requires some adaption on the part of
58 # the author, after all, nothing is free)
62 'my $_instance = shift;',
63 'my $class = Scalar::Util::blessed($_instance) || $_instance;',
64 'if ($class ne \'' . $self->associated_metaclass->name . '\') {',
65 'return ' . $self->_generate_fallback_constructor('$class') . ';',
67 $self->_generate_params('$params', '$class'),
68 $self->_generate_instance('$instance', '$class'),
69 $self->_generate_slot_initializers,
70 $self->_generate_triggers,
71 $self->_generate_BUILDALL,
75 warn join("\n", @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
87 $self->_compile_code(\@source);
90 my $source = join("\n", @source);
92 "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_",
98 $self->{'body'} = $code;
101 sub _eval_environment {
104 my $attrs = $self->_attributes;
106 my $defaults = [map { $_->default } @$attrs];
108 my @type_constraints = map {
109 $_->can('type_constraint') ? $_->type_constraint : undef
112 my @type_constraint_bodies = map {
113 defined $_ ? $_->_compiled_type_constraint : undef;
119 '$defaults' => \$defaults,
120 '@type_constraints' => \@type_constraints,
121 '@type_constraint_bodies' => \@type_constraint_bodies,
125 sub _generate_fallback_constructor {
127 my ($class_var) = @_;
128 return $class_var . '->Moose::Object::new(@_)'
131 sub _generate_params {
133 my ($var, $class_var) = @_;
135 'my ' . $var . ' = ',
136 $self->_generate_BUILDARGS($class_var, '@_'),
141 sub _generate_instance {
143 my ($var, $class_var) = @_;
144 my $meta = $self->associated_metaclass;
147 'my ' . $var . ' = ',
148 $meta->inline_create_instance($class_var) . ';',
152 sub _generate_slot_initializers {
154 return map { $self->_generate_slot_initializer($_) }
155 0 .. (@{$self->_attributes} - 1);
158 sub _generate_BUILDARGS {
160 my ($class, $args) = @_;
162 my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS");
165 && (!$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS)) {
170 'if (scalar @_ == 1) {',
171 'if (!defined($_[0]) || ref($_[0]) ne \'HASH\') {',
172 $self->_inline_throw_error(
173 '"Single parameters to new() must be a HASH ref"',
177 '$params = { %{ $_[0] } };',
181 '"The new() method for ' . $class . ' expects a '
182 . 'hash reference or a key/value list. You passed an '
183 . 'odd number of arguments"',
185 '$params = {@_, undef};',
195 return $class . '->BUILDARGS(' . $args . ')';
199 sub _generate_BUILDALL {
202 my @methods = reverse $self->associated_metaclass->find_all_methods_by_name('BUILD');
205 foreach my $method (@methods) {
207 '$instance->' . $method->{class} . '::BUILD($params);';
213 sub _generate_triggers {
217 for my $i (0 .. $#{ $self->_attributes }) {
218 my $attr = $self->_attributes->[$i];
220 next unless $attr->can('has_trigger') && $attr->has_trigger;
222 my $init_arg = $attr->init_arg;
223 next unless defined $init_arg;
226 'if (exists $params->{\'' . $init_arg . '\'}) {',
227 '$attrs->[' . $i . ']->trigger->(',
229 $attr->_inline_instance_get('$instance') . ',',
234 return @trigger_calls;
237 sub _generate_slot_initializer {
241 my $attr = $self->_attributes->[$index];
243 my @source = ('## ' . $attr->name);
245 push @source, $self->_check_required_attr($attr);
247 if (defined $attr->init_arg) {
249 'if (exists $params->{\'' . $attr->init_arg . '\'}) {',
250 $self->_init_attr_from_constructor($attr, $index),
252 if (my @default = $self->_init_attr_from_default($attr, $index)) {
260 if (my @default = $self->_init_attr_from_default($attr, $index)) {
262 '{', # _init_attr_from_default creates variables
271 sub _check_required_attr {
275 return unless defined $attr->init_arg;
276 return unless $attr->can('is_required') && $attr->is_required;
277 return if $attr->has_default || $attr->has_builder;
280 'if (!exists $params->{\'' . $attr->init_arg . '\'}) {',
281 $self->_inline_throw_error(
282 '"Attribute (' . quotemeta($attr->name) . ') is required"'
288 sub _init_attr_from_constructor {
290 my ($attr, $index) = @_;
293 'my $val = $params->{\'' . $attr->init_arg . '\'};',
294 $self->_generate_slot_assignment($attr, $index, '$val'),
298 sub _init_attr_from_default {
300 my ($attr, $index) = @_;
302 my $default = $self->_generate_default_value($attr, $index);
303 return unless $default;
306 'my $val = ' . $default . ';',
307 $self->_generate_slot_assignment($attr, $index, '$val'),
311 sub _generate_slot_assignment {
313 my ($attr, $index, $value) = @_;
317 if ($self->can('_generate_type_constraint_and_coercion')) {
318 push @source, $self->_generate_type_constraint_and_coercion(
319 $attr, $index, $value,
323 if ($attr->has_initializer) {
325 '$attrs->[' . $index . ']->set_initial_value(',
333 $attr->_inline_instance_set('$instance', $value) . ';',
340 sub _generate_type_constraint_and_coercion {
342 my ($attr, $index, $value) = @_;
344 return unless $attr->can('has_type_constraint')
345 && $attr->has_type_constraint;
349 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
350 push @source => $self->_generate_type_coercion(
351 '$type_constraints[' . $index . ']',
357 push @source => $self->_generate_type_constraint_check(
359 '$type_constraint_bodies[' . $index . ']',
360 '$type_constraints[' . $index . ']',
367 sub _generate_type_coercion {
369 my ($tc_obj, $value, $return_value) = @_;
370 return $return_value . ' = ' . $tc_obj . '->coerce(' . $value . ');';
373 sub _generate_type_constraint_check {
375 my ($attr, $tc_body, $tc_obj, $value) = @_;
377 $self->_inline_throw_error(
378 '"Attribute (' . quotemeta($attr->name) . ') '
379 . 'does not pass the type constraint because: " . '
380 . $tc_obj . '->get_message(' . $value . ')'
382 'unless ' . $tc_body . '->(' . $value . ');'
394 Moose::Meta::Method::Constructor - Method Meta Object for constructors
398 This class is a subclass of L<Class::MOP::Method::Constructor> that
399 provides additional Moose-specific functionality
401 To understand this class, you should read the the
402 L<Class::MOP::Method::Constructor> documentation as well.
406 C<Moose::Meta::Method::Constructor> is a subclass of
407 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
411 See L<Moose/BUGS> for details on reporting bugs.
415 Stevan Little E<lt>stevan@iinteractive.comE<gt>
417 =head1 COPYRIGHT AND LICENSE
419 Copyright 2006-2010 by Infinity Interactive, Inc.
421 L<http://www.iinteractive.com>
423 This library is free software; you can redistribute it and/or modify
424 it under the same terms as Perl itself.