add definition_context info for inlined constructors and destructors
[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 Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
9 use Try::Tiny;
10
11 use base 'Moose::Meta::Method',
12          'Class::MOP::Method::Constructor';
13
14 sub new {
15     my $class   = shift;
16     my %options = @_;
17
18     my $meta = $options{metaclass};
19
20     (ref $options{options} eq 'HASH')
21         || $class->throw_error("You must pass a hash of options", data => $options{options});
22
23     ($options{package_name} && $options{name})
24         || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
25
26     my $self = bless {
27         'body'          => undef,
28         'package_name'  => $options{package_name},
29         'name'          => $options{name},
30         'options'       => $options{options},
31         'associated_metaclass' => $meta,
32         'definition_context' => $options{definition_context},
33         '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object',
34     } => $class;
35
36     # we don't want this creating
37     # a cycle in the code, if not
38     # needed
39     weaken($self->{'associated_metaclass'});
40
41     $self->_initialize_body;
42
43     return $self;
44 }
45
46 ## method
47
48 sub _initialize_body {
49     my $self = shift;
50     $self->{'body'} = $self->_generate_constructor_method_inline;
51 }
52
53 sub _eval_environment {
54     my $self = shift;
55
56     my $attrs = $self->_attributes;
57
58     my $defaults = [map { $_->default } @$attrs];
59
60     # We need to check if the attribute ->can('type_constraint')
61     # since we may be trying to immutabilize a Moose meta class,
62     # which in turn has attributes which are Class::MOP::Attribute
63     # objects, rather than Moose::Meta::Attribute. And
64     # Class::MOP::Attribute attributes have no type constraints.
65     # However we need to make sure we leave an undef value there
66     # because the inlined code is using the index of the attributes
67     # to determine where to find the type constraint
68
69     my @type_constraints = map {
70         $_->can('type_constraint') ? $_->type_constraint : undef
71     } @$attrs;
72
73     my @type_constraint_bodies = map {
74         defined $_ ? $_->_compiled_type_constraint : undef;
75     } @type_constraints;
76
77     return {
78         '$meta'  => \$self,
79         '$attrs' => \$attrs,
80         '$defaults' => \$defaults,
81         '@type_constraints' => \@type_constraints,
82         '@type_constraint_bodies' => \@type_constraint_bodies,
83     };
84 }
85
86 1;
87
88 # ABSTRACT: Method Meta Object for constructors
89
90 __END__
91
92 =pod
93
94 =head1 DESCRIPTION
95
96 This class is a subclass of L<Class::MOP::Method::Constructor> that
97 provides additional Moose-specific functionality
98
99 To understand this class, you should read the the
100 L<Class::MOP::Method::Constructor> documentation as well.
101
102 =head1 INHERITANCE
103
104 C<Moose::Meta::Method::Constructor> is a subclass of
105 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
106
107 =head1 BUGS
108
109 See L<Moose/BUGS> for details on reporting bugs.
110
111 =cut
112