close over the coercion sub separately
[gitmo/Moose.git] / lib / Moose / Meta / Method / Constructor.pm
CommitLineData
5cf3dbcf 1
2package Moose::Meta::Method::Constructor;
3
4use strict;
5use warnings;
6
0d922627 7use Carp ();
0fa70d03 8use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr';
55c361dc 9use Try::Tiny;
5cf3dbcf 10
badb7e89 11use base 'Moose::Meta::Method',
bc89e9b5 12 'Class::MOP::Method::Constructor';
5cf3dbcf 13
14sub new {
15 my $class = shift;
16 my %options = @_;
7a5b07b3 17
3e504337 18 my $meta = $options{metaclass};
19
20 (ref $options{options} eq 'HASH')
a9538ac9 21 || $class->throw_error("You must pass a hash of options", data => $options{options});
7a5b07b3 22
1b2aea39 23 ($options{package_name} && $options{name})
a9538ac9 24 || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
1b2aea39 25
5cf3dbcf 26 my $self = bless {
d03bd989 27 'body' => undef,
e606ae5f 28 'package_name' => $options{package_name},
29 'name' => $options{name},
e606ae5f 30 'options' => $options{options},
e606ae5f 31 'associated_metaclass' => $meta,
0f1a71fc 32 'definition_context' => $options{definition_context},
0fa70d03 33 '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object',
5cf3dbcf 34 } => $class;
35
7a5b07b3 36 # we don't want this creating
37 # a cycle in the code, if not
5cf3dbcf 38 # needed
e606ae5f 39 weaken($self->{'associated_metaclass'});
5cf3dbcf 40
f5b0af77 41 $self->_initialize_body;
5cf3dbcf 42
7a5b07b3 43 return $self;
5cf3dbcf 44}
45
5cf3dbcf 46## method
47
f5b0af77 48sub _initialize_body {
5cf3dbcf 49 my $self = shift;
e247d17c 50 $self->{'body'} = $self->_generate_constructor_method_inline;
51}
52
53sub _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
c40e4359 77 my @type_coercions = map {
78 defined $_ && $_->has_coercion
79 ? $_->coercion->_compiled_type_coercion
80 : undef
81 } @type_constraints;
82
e247d17c 83 return {
84 '$meta' => \$self,
85 '$attrs' => \$attrs,
86 '$defaults' => \$defaults,
87 '@type_constraints' => \@type_constraints,
c40e4359 88 '@type_coercions' => \@type_coercions,
e247d17c 89 '@type_constraint_bodies' => \@type_constraint_bodies,
78fa4d7d 90 ( map { defined($_) ? %{ $_->inline_environment } : () }
91 @type_constraints ),
e247d17c 92 };
93}
94
5cf3dbcf 951;
96
ad46f524 97# ABSTRACT: Method Meta Object for constructors
98
5cf3dbcf 99__END__
100
101=pod
102
5cf3dbcf 103=head1 DESCRIPTION
104
cec39889 105This class is a subclass of L<Class::MOP::Method::Constructor> that
cefc9e36 106provides additional Moose-specific functionality
107
108To understand this class, you should read the the
cec39889 109L<Class::MOP::Method::Constructor> documentation as well.
d44714be 110
bc89e9b5 111=head1 INHERITANCE
112
113C<Moose::Meta::Method::Constructor> is a subclass of
114L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>.
115
c5fc2c21 116=head1 BUGS
117
118See L<Moose/BUGS> for details on reporting bugs.
119
5cf3dbcf 120=cut
121