Commit | Line | Data |
fc1d8369 |
1 | package Mouse::Meta::Method::Constructor; |
2 | use strict; |
3 | use warnings; |
4 | |
5 | sub 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 | |
32 | sub _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; |
c91862e8 |
39 | |
f262f14e |
40 | my $set_value = do { |
fc1d8369 |
41 | my @code; |
c91862e8 |
42 | |
fc1d8369 |
43 | if ($attr->should_coerce) { |
4a1986d0 |
44 | push @code, "my \$value = \$attrs[$index]->coerce_constraint( \$args->{'$from'});"; |
7bf66a9b |
45 | } |
46 | else { |
47 | push @code, "my \$value = \$args->{'$from'};"; |
fc1d8369 |
48 | } |
c91862e8 |
49 | |
fc1d8369 |
50 | if ($attr->has_type_constraint) { |
4a1986d0 |
51 | push @code, "\$attrs[$index]->verify_type_constraint( \$value );"; |
fc1d8369 |
52 | } |
c91862e8 |
53 | |
54 | push @code, "\$instance->{'$key'} = \$value;"; |
55 | |
41cdacce |
56 | if ($attr->is_weak_ref) { |
c91862e8 |
57 | push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );"; |
41cdacce |
58 | } |
c91862e8 |
59 | |
fc1d8369 |
60 | if ( $attr->has_trigger ) { |
4a1986d0 |
61 | push @code, "\$attrs[$index]->trigger->( \$instance, \$value, \$attrs[$index] );"; |
fc1d8369 |
62 | } |
c91862e8 |
63 | |
fc1d8369 |
64 | join "\n", @code; |
65 | }; |
c91862e8 |
66 | |
f262f14e |
67 | my $make_default_value = do { |
fc1d8369 |
68 | my @code; |
e8ba7b26 |
69 | |
fc1d8369 |
70 | if ( $attr->has_default || $attr->has_builder ) { |
71 | unless ( $attr->is_lazy ) { |
72 | my $default = $attr->default; |
73 | my $builder = $attr->builder; |
e8ba7b26 |
74 | |
75 | push @code, "my \$value = "; |
76 | |
77 | if ($attr->should_coerce) { |
4a1986d0 |
78 | push @code, "\$attrs[$index]->coerce_constraint("; |
fc1d8369 |
79 | } |
e8ba7b26 |
80 | if ($attr->has_builder) { |
81 | push @code, "\$instance->$builder"; |
82 | } |
83 | elsif (ref($default) eq 'CODE') { |
0e503bd9 |
84 | push @code, "\$attrs[$index]->default()->(\$instance)"; |
e8ba7b26 |
85 | } |
713a2a05 |
86 | elsif (!defined($default)) { |
87 | push @code, 'undef'; |
88 | } |
89 | elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) { |
90 | push @code, $default; |
91 | } |
e8ba7b26 |
92 | else { |
713a2a05 |
93 | push @code, "'$default'"; |
e8ba7b26 |
94 | } |
95 | |
fc1d8369 |
96 | if ($attr->should_coerce) { |
e8ba7b26 |
97 | push @code, ");"; |
98 | } |
99 | else { |
100 | push @code, ";"; |
fc1d8369 |
101 | } |
e8ba7b26 |
102 | |
fc1d8369 |
103 | if ($attr->has_type_constraint) { |
4a1986d0 |
104 | push @code, "\$attrs[$index]->verify_type_constraint(\$value);"; |
fc1d8369 |
105 | } |
e8ba7b26 |
106 | |
8dcbf65d |
107 | push @code, "\$instance->{'$key'} = \$value;"; |
e8ba7b26 |
108 | |
fc1d8369 |
109 | if ($attr->is_weak_ref) { |
e8ba7b26 |
110 | push @code, "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 | |
143 | sub _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 | |
163 | sub _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 | |
179 | 1; |