move eval_environment for constructors to the metaclass
[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 1;
55
56 # ABSTRACT: Method Meta Object for constructors
57
58 __END__
59
60 =pod
61
62 =head1 DESCRIPTION
63
64 This class is a subclass of L<Class::MOP::Method::Constructor> that
65 provides additional Moose-specific functionality
66
67 To understand this class, you should read the the
68 L<Class::MOP::Method::Constructor> documentation as well.
69
70 =head1 INHERITANCE
71
72 C<Moose::Meta::Method::Constructor> is a subclass of
73 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
74
75 =head1 BUGS
76
77 See L<Moose/BUGS> for details on reporting bugs.
78
79 =cut
80