Close over $key so we don't need to do an assignment or variable lookup in the constr...
[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
c12edd9a 8 my @attrs = $meta->compute_all_applicable_attributes; # this one is using by evaled code
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
24 warn $code if $ENV{DEBUG};
25
26 local $@;
27 my $res = eval $code;
28 die $@ if $@;
29 $res;
fc1d8369 30}
31
32sub _generate_processattrs {
c12edd9a 33 my ($class, $meta, $attrs) = @_;
fc1d8369 34 my @res;
c12edd9a 35 for my $index (0..scalar(@$attrs)-1) {
36 my $attr = $attrs->[$index];
fc1d8369 37 my $from = $attr->init_arg;
38 my $key = $attr->name;
39 my $part1 = do {
40 my @code;
41 if ($attr->should_coerce) {
42 push @code, "\$args->{\$from} = \$attr->coerce_constraint( \$args->{\$from} );";
43 }
44 if ($attr->has_type_constraint) {
45 push @code, "\$attr->verify_type_constraint( \$args->{\$from} );";
46 }
8dcbf65d 47 push @code, "\$instance->{'$key'} = \$args->{\$from};";
41cdacce 48 if ($attr->is_weak_ref) {
8dcbf65d 49 push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );";
41cdacce 50 }
fc1d8369 51 if ( $attr->has_trigger ) {
52 push @code, "\$attr->trigger->( \$instance, \$args->{\$from}, \$attr );";
53 }
54 join "\n", @code;
55 };
56 my $part2 = do {
57 my @code;
58 if ( $attr->has_default || $attr->has_builder ) {
59 unless ( $attr->is_lazy ) {
60 my $default = $attr->default;
61 my $builder = $attr->builder;
62 if ($attr->has_builder) {
63 push @code, "my \$value = \$instance->$builder;";
64 } elsif (ref($default) eq 'CODE') {
65 push @code, "my \$value = \$attr->default()->();";
66 } else {
67 push @code, "my \$value = \$attr->default();";
68 }
69 if ($attr->should_coerce) {
70 push @code, "\$value = \$attr->coerce_constraint(\$value);";
71 }
72 if ($attr->has_type_constraint) {
73 push @code, "\$attr->verify_type_constraint(\$value);";
74 }
8dcbf65d 75 push @code, "\$instance->{'$key'} = \$value;";
fc1d8369 76 if ($attr->is_weak_ref) {
8dcbf65d 77 push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );";
fc1d8369 78 }
79 }
80 join "\n", @code;
81 }
82 else {
83 if ( $attr->is_required ) {
84 q{Carp::confess("Attribute (} . $attr->name . q{) is required");};
85 } else {
86 ""
87 }
88 }
89 };
90 my $code = <<"...";
91 {
c12edd9a 92 my \$attr = \$attrs[$index];
fc1d8369 93 my \$from = '$from';
fc1d8369 94 if (defined(\$from) && exists(\$args->{\$from})) {
95 $part1;
96 } else {
97 $part2;
98 }
99 }
100...
101 push @res, $code;
102 }
103 return join "\n", @res;
104}
105
106sub _generate_BUILDARGS {
107 <<'...';
108 do {
109 if ( scalar @_ == 1 ) {
110 if ( defined $_[0] ) {
111 ( ref( $_[0] ) eq 'HASH' )
112 || Carp::confess "Single parameters to new() must be a HASH ref";
113 +{ %{ $_[0] } };
114 }
115 else {
116 +{};
117 }
118 }
119 else {
120 +{@_};
121 }
122 };
123...
124}
125
126sub _generate_BUILDALL {
127 my ($class, $meta) = @_;
128 return '' unless $meta->name->can('BUILD');
129
130 my @code = ();
131 push @code, q{no strict 'refs';};
132 push @code, q{no warnings 'once';};
133 no strict 'refs';
134 for my $class ($meta->linearized_isa) {
135 if (*{ $class . '::BUILD' }{CODE}) {
136 push @code, qq{${class}::BUILD->(\$instance, \$args);};
137 }
138 }
139 return join "\n", @code;
140}
141
1421;