2 package Moose::Meta::Method::Constructor;
8 use List::MoreUtils 'any';
9 use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
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 'definition_context' => $options{definition_context},
34 '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object',
37 # we don't want this creating
38 # a cycle in the code, if not
40 weaken($self->{'associated_metaclass'});
42 $self->_initialize_body;
49 sub _initialize_body {
51 $self->{'body'} = $self->_generate_constructor_method_inline;
54 sub _eval_environment {
57 my $attrs = $self->_attributes;
59 my $defaults = [map { $_->default } @$attrs];
61 map { $_->can('has_trigger') && $_->has_trigger ? $_->trigger : undef }
65 # We need to check if the attribute ->can('type_constraint')
66 # since we may be trying to immutabilize a Moose meta class,
67 # which in turn has attributes which are Class::MOP::Attribute
68 # objects, rather than Moose::Meta::Attribute. And
69 # Class::MOP::Attribute attributes have no type constraints.
70 # However we need to make sure we leave an undef value there
71 # because the inlined code is using the index of the attributes
72 # to determine where to find the type constraint
74 my @type_constraints = map {
75 $_->can('type_constraint') ? $_->type_constraint : undef
78 my @type_constraint_bodies = map {
79 defined $_ ? $_->_compiled_type_constraint : undef;
82 my @type_coercions = map {
83 defined $_ && $_->has_coercion
84 ? $_->coercion->_compiled_type_coercion
88 my @type_constraint_messages = map {
90 ? ($_->has_message ? $_->message : $_->_default_message)
95 ((any { defined && $_->has_initializer } @$attrs)
96 ? ('$attrs' => \$attrs)
98 '$defaults' => \$defaults,
99 '$triggers' => \$triggers,
100 '@type_coercions' => \@type_coercions,
101 '@type_constraint_bodies' => \@type_constraint_bodies,
102 '@type_constraint_messages' => \@type_constraint_messages,
103 ( map { defined($_) ? %{ $_->inline_environment } : () }
105 # pretty sure this is only going to be closed over if you use a custom
106 # error class at this point, but we should still get rid of this
108 '$meta' => \($self->associated_metaclass),
114 # ABSTRACT: Method Meta Object for constructors
122 This class is a subclass of L<Class::MOP::Method::Constructor> that
123 provides additional Moose-specific functionality
125 To understand this class, you should read the the
126 L<Class::MOP::Method::Constructor> documentation as well.
130 C<Moose::Meta::Method::Constructor> is a subclass of
131 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
135 See L<Moose/BUGS> for details on reporting bugs.