Test for init_arg => undef
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
CommitLineData
fc1d8369 1package Mouse::Meta::Method::Constructor;
2use strict;
3use warnings;
4
5sub generate_constructor_method_inline {
41cdacce 6 my ($class, $meta) = @_;
24ad3f66 7
a4d5249c 8 my @attrs = $meta->compute_all_applicable_attributes;
fc1d8369 9 my $buildall = $class->_generate_BUILDALL($meta);
10 my $buildargs = $class->_generate_BUILDARGS();
c12edd9a 11 my $processattrs = $class->_generate_processattrs($meta, \@attrs);
fc1d8369 12
24ad3f66 13 my $code = <<"...";
fc1d8369 14 sub {
15 my \$class = shift;
16 my \$args = $buildargs;
24ad3f66 17 my \$instance = bless {}, \$class;
fc1d8369 18 $processattrs;
19 $buildall;
20 return \$instance;
21 }
22...
24ad3f66 23
24ad3f66 24 local $@;
25 my $res = eval $code;
26 die $@ if $@;
27 $res;
fc1d8369 28}
29
30sub _generate_processattrs {
c12edd9a 31 my ($class, $meta, $attrs) = @_;
fc1d8369 32 my @res;
c12edd9a 33 for my $index (0..scalar(@$attrs)-1) {
34 my $attr = $attrs->[$index];
fc1d8369 35 my $from = $attr->init_arg;
36 my $key = $attr->name;
c91862e8 37
f262f14e 38 my $set_value = do {
fc1d8369 39 my @code;
c91862e8 40
b3b74cc6 41 if ($attr->should_coerce && $attr->type_constraint) {
42 push @code, "my \$value = Mouse::TypeRegistry->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, \$args->{'$from'});";
7bf66a9b 43 }
44 else {
45 push @code, "my \$value = \$args->{'$from'};";
fc1d8369 46 }
c91862e8 47
fc1d8369 48 if ($attr->has_type_constraint) {
b3b74cc6 49 push @code, "{local \$_ = \$value; unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {";
50 push @code, "\$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)}}";
fc1d8369 51 }
c91862e8 52
53 push @code, "\$instance->{'$key'} = \$value;";
54
41cdacce 55 if ($attr->is_weak_ref) {
f2560fde 56 push @code, "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );";
41cdacce 57 }
c91862e8 58
fc1d8369 59 if ( $attr->has_trigger ) {
b3b74cc6 60 push @code, "\$attrs[$index]->{trigger}->( \$instance, \$value, \$attrs[$index] );";
fc1d8369 61 }
c91862e8 62
fc1d8369 63 join "\n", @code;
64 };
c91862e8 65
f262f14e 66 my $make_default_value = do {
fc1d8369 67 my @code;
e8ba7b26 68
fc1d8369 69 if ( $attr->has_default || $attr->has_builder ) {
70 unless ( $attr->is_lazy ) {
71 my $default = $attr->default;
72 my $builder = $attr->builder;
e8ba7b26 73
74 push @code, "my \$value = ";
75
b3b74cc6 76 if ($attr->should_coerce && $attr->type_constraint) {
77 push @code, "Mouse::TypeRegistry->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, ";
fc1d8369 78 }
e8ba7b26 79 if ($attr->has_builder) {
80 push @code, "\$instance->$builder";
81 }
82 elsif (ref($default) eq 'CODE') {
b3b74cc6 83 push @code, "\$attrs[$index]->{default}->(\$instance)";
e8ba7b26 84 }
713a2a05 85 elsif (!defined($default)) {
86 push @code, 'undef';
87 }
88 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
89 push @code, $default;
90 }
e8ba7b26 91 else {
713a2a05 92 push @code, "'$default'";
e8ba7b26 93 }
94
fc1d8369 95 if ($attr->should_coerce) {
e8ba7b26 96 push @code, ");";
97 }
98 else {
99 push @code, ";";
fc1d8369 100 }
e8ba7b26 101
fc1d8369 102 if ($attr->has_type_constraint) {
b3b74cc6 103 push @code, "{local \$_ = \$value; unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {";
104 push @code, "\$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)}}";
fc1d8369 105 }
e8ba7b26 106
8dcbf65d 107 push @code, "\$instance->{'$key'} = \$value;";
e8ba7b26 108
fc1d8369 109 if ($attr->is_weak_ref) {
f2560fde 110 push @code, "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );";
fc1d8369 111 }
112 }
113 join "\n", @code;
114 }
115 else {
116 if ( $attr->is_required ) {
95e0838c 117 qq{Carp::confess("Attribute ($key) is required");};
fc1d8369 118 } else {
119 ""
120 }
121 }
122 };
123 my $code = <<"...";
124 {
95e0838c 125 if (exists(\$args->{'$from'})) {
f262f14e 126 $set_value;
713a2a05 127...
128 if ($make_default_value) {
129 $code .= <<"...";
fc1d8369 130 } else {
f262f14e 131 $make_default_value;
713a2a05 132...
133 }
134 $code .= <<"...";
fc1d8369 135 }
136 }
137...
138 push @res, $code;
139 }
140 return join "\n", @res;
141}
142
143sub _generate_BUILDARGS {
144 <<'...';
145 do {
146 if ( scalar @_ == 1 ) {
147 if ( defined $_[0] ) {
148 ( ref( $_[0] ) eq 'HASH' )
149 || Carp::confess "Single parameters to new() must be a HASH ref";
150 +{ %{ $_[0] } };
151 }
152 else {
153 +{};
154 }
155 }
156 else {
157 +{@_};
158 }
159 };
160...
161}
162
163sub _generate_BUILDALL {
164 my ($class, $meta) = @_;
165 return '' unless $meta->name->can('BUILD');
166
167 my @code = ();
168 push @code, q{no strict 'refs';};
169 push @code, q{no warnings 'once';};
170 no strict 'refs';
ca3bebbd 171 for my $klass ($meta->linearized_isa) {
172 if (*{ $klass . '::BUILD' }{CODE}) {
63d74d7a 173 push @code, qq{${klass}::BUILD(\$instance, \$args);};
fc1d8369 174 }
175 }
176 return join "\n", @code;
177}
178
1791;