Beginning of dzilization
[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 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::Method',
14          'Class::MOP::Method::Constructor';
15
16 sub new {
17     my $class   = shift;
18     my %options = @_;
19
20     my $meta = $options{metaclass};
21
22     (ref $options{options} eq 'HASH')
23         || $class->throw_error("You must pass a hash of options", data => $options{options});
24
25     ($options{package_name} && $options{name})
26         || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
27
28     my $self = bless {
29         'body'          => undef,
30         'package_name'  => $options{package_name},
31         'name'          => $options{name},
32         'options'       => $options{options},
33         'associated_metaclass' => $meta,
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
61     # We need to check if the attribute ->can('type_constraint')
62     # since we may be trying to immutabilize a Moose meta class,
63     # which in turn has attributes which are Class::MOP::Attribute
64     # objects, rather than Moose::Meta::Attribute. And
65     # Class::MOP::Attribute attributes have no type constraints.
66     # However we need to make sure we leave an undef value there
67     # because the inlined code is using the index of the attributes
68     # to determine where to find the type constraint
69
70     my @type_constraints = map {
71         $_->can('type_constraint') ? $_->type_constraint : undef
72     } @$attrs;
73
74     my @type_constraint_bodies = map {
75         defined $_ ? $_->_compiled_type_constraint : undef;
76     } @type_constraints;
77
78     return {
79         '$meta'  => \$self,
80         '$attrs' => \$attrs,
81         '$defaults' => \$defaults,
82         '@type_constraints' => \@type_constraints,
83         '@type_constraint_bodies' => \@type_constraint_bodies,
84     };
85 }
86
87 1;
88
89 # ABSTRACT: Method Meta Object for constructors
90
91 __END__
92
93 =pod
94
95 =head1 DESCRIPTION
96
97 This class is a subclass of L<Class::MOP::Method::Constructor> that
98 provides additional Moose-specific functionality
99
100 To understand this class, you should read the the
101 L<Class::MOP::Method::Constructor> documentation as well.
102
103 =head1 INHERITANCE
104
105 C<Moose::Meta::Method::Constructor> is a subclass of
106 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
107
108 =head1 BUGS
109
110 See L<Moose/BUGS> for details on reporting bugs.
111
112 =cut
113