stop closing over the method metaobject
[gitmo/Moose.git] / lib / Moose / Meta / Method / Constructor.pm
1
2 package Moose::Meta::Method::Constructor;
3
4 use strict;
5 use warnings;
6
7 use Carp ();
8 use List::MoreUtils 'any';
9 use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
10 use Try::Tiny;
11
12 use base 'Moose::Meta::Method',
13          'Class::MOP::Method::Constructor';
14
15 sub new {
16     my $class   = shift;
17     my %options = @_;
18
19     my $meta = $options{metaclass};
20
21     (ref $options{options} eq 'HASH')
22         || $class->throw_error("You must pass a hash of options", data => $options{options});
23
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");
26
27     my $self = bless {
28         'body'          => undef,
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',
35     } => $class;
36
37     # we don't want this creating
38     # a cycle in the code, if not
39     # needed
40     weaken($self->{'associated_metaclass'});
41
42     $self->_initialize_body;
43
44     return $self;
45 }
46
47 ## method
48
49 sub _initialize_body {
50     my $self = shift;
51     $self->{'body'} = $self->_generate_constructor_method_inline;
52 }
53
54 sub _eval_environment {
55     my $self = shift;
56
57     my $attrs = $self->_attributes;
58
59     my $defaults = [map { $_->default } @$attrs];
60     my $triggers = [
61         map { $_->can('has_trigger') && $_->has_trigger ? $_->trigger : undef }
62             @$attrs
63     ];
64
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
73
74     my @type_constraints = map {
75         $_->can('type_constraint') ? $_->type_constraint : undef
76     } @$attrs;
77
78     my @type_constraint_bodies = map {
79         defined $_ ? $_->_compiled_type_constraint : undef;
80     } @type_constraints;
81
82     my @type_coercions = map {
83         defined $_ && $_->has_coercion
84             ? $_->coercion->_compiled_type_coercion
85             : undef
86     } @type_constraints;
87
88     return {
89         ((any { defined && $_->has_initializer } @$attrs)
90             ? ('$attrs' => \$attrs)
91             : ()),
92         '$defaults' => \$defaults,
93         '$triggers' => \$triggers,
94         '@type_constraints' => \@type_constraints,
95         '@type_coercions' => \@type_coercions,
96         '@type_constraint_bodies' => \@type_constraint_bodies,
97         ( map { defined($_) ? %{ $_->inline_environment } : () }
98               @type_constraints ),
99         # pretty sure this is only going to be closed over if you use a custom
100         # error class at this point, but we should still get rid of this
101         # at some point
102         '$meta'  => \($self->associated_metaclass),
103     };
104 }
105
106 1;
107
108 # ABSTRACT: Method Meta Object for constructors
109
110 __END__
111
112 =pod
113
114 =head1 DESCRIPTION
115
116 This class is a subclass of L<Class::MOP::Method::Constructor> that
117 provides additional Moose-specific functionality
118
119 To understand this class, you should read the the
120 L<Class::MOP::Method::Constructor> documentation as well.
121
122 =head1 INHERITANCE
123
124 C<Moose::Meta::Method::Constructor> is a subclass of
125 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
126
127 =head1 BUGS
128
129 See L<Moose/BUGS> for details on reporting bugs.
130
131 =cut
132