Commit | Line | Data |
5cf3dbcf |
1 | |
2 | package Moose::Meta::Method::Constructor; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
0d922627 |
7 | use Carp (); |
0fa70d03 |
8 | use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr'; |
55c361dc |
9 | use Try::Tiny; |
5cf3dbcf |
10 | |
badb7e89 |
11 | use base 'Moose::Meta::Method', |
bc89e9b5 |
12 | 'Class::MOP::Method::Constructor'; |
5cf3dbcf |
13 | |
14 | sub 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, |
0fa70d03 |
32 | '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object', |
5cf3dbcf |
33 | } => $class; |
34 | |
7a5b07b3 |
35 | # we don't want this creating |
36 | # a cycle in the code, if not |
5cf3dbcf |
37 | # needed |
e606ae5f |
38 | weaken($self->{'associated_metaclass'}); |
5cf3dbcf |
39 | |
f5b0af77 |
40 | $self->_initialize_body; |
5cf3dbcf |
41 | |
7a5b07b3 |
42 | return $self; |
5cf3dbcf |
43 | } |
44 | |
5cf3dbcf |
45 | ## method |
46 | |
f5b0af77 |
47 | sub _initialize_body { |
5cf3dbcf |
48 | my $self = shift; |
e247d17c |
49 | $self->{'body'} = $self->_generate_constructor_method_inline; |
50 | } |
51 | |
52 | sub _eval_environment { |
53 | my $self = shift; |
54 | |
55 | my $attrs = $self->_attributes; |
56 | |
57 | my $defaults = [map { $_->default } @$attrs]; |
58 | |
59 | # We need to check if the attribute ->can('type_constraint') |
60 | # since we may be trying to immutabilize a Moose meta class, |
61 | # which in turn has attributes which are Class::MOP::Attribute |
62 | # objects, rather than Moose::Meta::Attribute. And |
63 | # Class::MOP::Attribute attributes have no type constraints. |
64 | # However we need to make sure we leave an undef value there |
65 | # because the inlined code is using the index of the attributes |
66 | # to determine where to find the type constraint |
67 | |
68 | my @type_constraints = map { |
69 | $_->can('type_constraint') ? $_->type_constraint : undef |
70 | } @$attrs; |
71 | |
72 | my @type_constraint_bodies = map { |
73 | defined $_ ? $_->_compiled_type_constraint : undef; |
74 | } @type_constraints; |
75 | |
76 | return { |
77 | '$meta' => \$self, |
78 | '$attrs' => \$attrs, |
79 | '$defaults' => \$defaults, |
80 | '@type_constraints' => \@type_constraints, |
81 | '@type_constraint_bodies' => \@type_constraint_bodies, |
82 | }; |
83 | } |
84 | |
5cf3dbcf |
85 | 1; |
86 | |
ad46f524 |
87 | # ABSTRACT: Method Meta Object for constructors |
88 | |
5cf3dbcf |
89 | __END__ |
90 | |
91 | =pod |
92 | |
5cf3dbcf |
93 | =head1 DESCRIPTION |
94 | |
cec39889 |
95 | This class is a subclass of L<Class::MOP::Method::Constructor> that |
cefc9e36 |
96 | provides additional Moose-specific functionality |
97 | |
98 | To understand this class, you should read the the |
cec39889 |
99 | L<Class::MOP::Method::Constructor> documentation as well. |
d44714be |
100 | |
bc89e9b5 |
101 | =head1 INHERITANCE |
102 | |
103 | C<Moose::Meta::Method::Constructor> is a subclass of |
104 | L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Constructor>. |
105 | |
c5fc2c21 |
106 | =head1 BUGS |
107 | |
108 | See L<Moose/BUGS> for details on reporting bugs. |
109 | |
5cf3dbcf |
110 | =cut |
111 | |