Commit | Line | Data |
fc1d8369 |
1 | package Mouse::Meta::Method::Constructor; |
3821b191 |
2 | use Mouse::Util qw(:meta); # enables strict and warnings |
fc1d8369 |
3 | |
f031f4d5 |
4 | sub _inline_slot{ |
f8cbb121 |
5 | my(undef, $self_var, $attr_name) = @_; |
6 | return sprintf '%s->{q{%s}}', $self_var, $attr_name; |
7 | } |
8 | |
380e1cd7 |
9 | sub _generate_constructor { |
2a464664 |
10 | my ($class, $metaclass, $args) = @_; |
24ad3f66 |
11 | |
f031f4d5 |
12 | my $associated_metaclass_name = $metaclass->name; |
13 | |
7ca5c5fb |
14 | my @attrs = $metaclass->get_all_attributes; |
15 | |
3db78d2a |
16 | my $buildall = $class->_generate_BUILDALL($metaclass); |
f031f4d5 |
17 | my $buildargs = $class->_generate_BUILDARGS($metaclass); |
18 | my $processattrs = $class->_generate_processattrs($metaclass, \@attrs); |
7ca5c5fb |
19 | |
0bfc7290 |
20 | my @checks = map { $_ && $_->_compiled_type_constraint } |
21 | map { $_->type_constraint } @attrs; |
ad087d11 |
22 | |
f031f4d5 |
23 | my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"..."; |
24 | sub \{ |
25 | my \$class = shift; |
26 | return \$class->Mouse::Object::new(\@_) |
27 | if \$class ne q{$associated_metaclass_name}; |
0bfc7290 |
28 | # BUILDARGS |
f031f4d5 |
29 | $buildargs; |
30 | my \$instance = bless {}, \$class; |
0bfc7290 |
31 | # process attributes |
f031f4d5 |
32 | $processattrs; |
0bfc7290 |
33 | # BUILDALL |
f031f4d5 |
34 | $buildall; |
35 | return \$instance; |
2a464664 |
36 | } |
f031f4d5 |
37 | ... |
0bfc7290 |
38 | #warn $source; |
ad087d11 |
39 | my $code; |
40 | my $e = do{ |
41 | local $@; |
42 | $code = eval $source; |
43 | $@; |
44 | }; |
45 | die $e if $e; |
380e1cd7 |
46 | return $code; |
fc1d8369 |
47 | } |
48 | |
49 | sub _generate_processattrs { |
f8cbb121 |
50 | my ($method_class, $metaclass, $attrs) = @_; |
fc1d8369 |
51 | my @res; |
9df6f0cd |
52 | |
2efc0af1 |
53 | my $has_triggers; |
8801a6e6 |
54 | my $strict = $metaclass->__strict_constructor; |
e128626c |
55 | |
8801a6e6 |
56 | if($strict){ |
e128626c |
57 | push @res, 'my $used = 0;'; |
58 | } |
2efc0af1 |
59 | |
9df6f0cd |
60 | for my $index (0 .. @$attrs - 1) { |
0bfc7290 |
61 | my $code = ''; |
62 | |
c12edd9a |
63 | my $attr = $attrs->[$index]; |
fc1d8369 |
64 | my $key = $attr->name; |
9df6f0cd |
65 | |
0bfc7290 |
66 | my $init_arg = $attr->init_arg; |
67 | my $type_constraint = $attr->type_constraint; |
8aba926d |
68 | my $is_weak_ref = $attr->is_weak_ref; |
0bfc7290 |
69 | my $need_coercion; |
c91862e8 |
70 | |
f031f4d5 |
71 | my $instance_slot = $method_class->_inline_slot('$instance', $key); |
0bfc7290 |
72 | my $attr_var = "\$attrs[$index]"; |
73 | my $constraint_var; |
c91862e8 |
74 | |
0bfc7290 |
75 | if(defined $type_constraint){ |
76 | $constraint_var = "$attr_var\->{type_constraint}"; |
77 | $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion); |
78 | } |
c91862e8 |
79 | |
0bfc7290 |
80 | $code .= "# initialize $key\n"; |
81 | |
82 | my $post_process = ''; |
83 | if(defined $type_constraint){ |
84 | $post_process .= "\$checks[$index]->($instance_slot)"; |
da23cd4a |
85 | $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n"; |
0bfc7290 |
86 | } |
8aba926d |
87 | if($is_weak_ref){ |
f031f4d5 |
88 | $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n"; |
0bfc7290 |
89 | } |
c91862e8 |
90 | |
0bfc7290 |
91 | if (defined $init_arg) { |
92 | my $value = "\$args->{q{$init_arg}}"; |
c91862e8 |
93 | |
0bfc7290 |
94 | $code .= "if (exists $value) {\n"; |
95 | |
96 | if($need_coercion){ |
620c3203 |
97 | $value = "$constraint_var->coerce($value)"; |
41cdacce |
98 | } |
c91862e8 |
99 | |
f031f4d5 |
100 | $code .= "$instance_slot = $value;\n"; |
0bfc7290 |
101 | $code .= $post_process; |
102 | |
9df6f0cd |
103 | if ($attr->has_trigger) { |
2efc0af1 |
104 | $has_triggers++; |
620c3203 |
105 | $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n"; |
fc1d8369 |
106 | } |
c91862e8 |
107 | |
8801a6e6 |
108 | if ($strict){ |
109 | $code .= '++$used;' . "\n"; |
e128626c |
110 | } |
111 | |
112 | $code .= "\n} else {\n"; # $value exists |
9df6f0cd |
113 | } |
c91862e8 |
114 | |
9df6f0cd |
115 | if ($attr->has_default || $attr->has_builder) { |
116 | unless ($attr->is_lazy) { |
117 | my $default = $attr->default; |
118 | my $builder = $attr->builder; |
e8ba7b26 |
119 | |
0bfc7290 |
120 | my $value; |
121 | if (defined($builder)) { |
122 | $value = "\$instance->$builder()"; |
ffbbf459 |
123 | } |
124 | elsif (ref($default) eq 'CODE') { |
0bfc7290 |
125 | $value = "$attr_var\->{default}->(\$instance)"; |
ffbbf459 |
126 | } |
0bfc7290 |
127 | elsif (defined($default)) { |
128 | $value = "$attr_var\->{default}"; |
ffbbf459 |
129 | } |
130 | else { |
0bfc7290 |
131 | $value = 'undef'; |
ffbbf459 |
132 | } |
e8ba7b26 |
133 | |
0bfc7290 |
134 | if($need_coercion){ |
135 | $value = "$constraint_var->coerce($value)"; |
9df6f0cd |
136 | } |
9df6f0cd |
137 | |
f031f4d5 |
138 | $code .= "$instance_slot = $value;\n"; |
8aba926d |
139 | if($is_weak_ref){ |
f031f4d5 |
140 | $code .= "Scalar::Util::weaken($instance_slot);\n"; |
8aba926d |
141 | } |
fc1d8369 |
142 | } |
713a2a05 |
143 | } |
9df6f0cd |
144 | elsif ($attr->is_required) { |
7756897f |
145 | $code .= "Carp::confess('Attribute ($key) is required');"; |
9df6f0cd |
146 | } |
147 | |
0bfc7290 |
148 | $code .= "}\n" if defined $init_arg; |
9df6f0cd |
149 | |
fc1d8369 |
150 | push @res, $code; |
151 | } |
9df6f0cd |
152 | |
8801a6e6 |
153 | if($strict){ |
e128626c |
154 | push @res, q{if($used < keys %{$args})} |
8801a6e6 |
155 | . sprintf q{{ %s->_report_unknown_args($metaclass, \@attrs, $args) }}, $method_class; |
e128626c |
156 | } |
157 | |
2efc0af1 |
158 | if($metaclass->is_anon_class){ |
a8391b11 |
159 | push @res, q{$instance->{__METACLASS__} = $metaclass;}; |
2efc0af1 |
160 | } |
161 | |
162 | if($has_triggers){ |
163 | unshift @res, q{my @triggers;}; |
e128626c |
164 | push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;}; |
2efc0af1 |
165 | } |
166 | |
167 | return join "\n", @res; |
fc1d8369 |
168 | } |
169 | |
170 | sub _generate_BUILDARGS { |
0bfc7290 |
171 | my(undef, $metaclass) = @_; |
9dcd7d23 |
172 | |
0bfc7290 |
173 | my $class = $metaclass->name; |
174 | if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) { |
53d4053e |
175 | return 'my $args = $class->BUILDARGS(@_)'; |
9dcd7d23 |
176 | } |
177 | |
178 | return <<'...'; |
53d4053e |
179 | my $args; |
fc1d8369 |
180 | if ( scalar @_ == 1 ) { |
c9aefe26 |
181 | ( ref( $_[0] ) eq 'HASH' ) |
fc1d8369 |
182 | || Carp::confess "Single parameters to new() must be a HASH ref"; |
53d4053e |
183 | $args = +{ %{ $_[0] } }; |
fc1d8369 |
184 | } |
185 | else { |
53d4053e |
186 | $args = +{@_}; |
fc1d8369 |
187 | } |
fc1d8369 |
188 | ... |
189 | } |
190 | |
191 | sub _generate_BUILDALL { |
0bfc7290 |
192 | my (undef, $metaclass) = @_; |
7ca5c5fb |
193 | |
2efc0af1 |
194 | return '' unless $metaclass->name->can('BUILD'); |
fc1d8369 |
195 | |
7ca5c5fb |
196 | my @code; |
197 | for my $class ($metaclass->linearized_isa) { |
a5c683f6 |
198 | if (Mouse::Util::get_code_ref($class, 'BUILD')) { |
7ca5c5fb |
199 | unshift @code, qq{${class}::BUILD(\$instance, \$args);}; |
fc1d8369 |
200 | } |
201 | } |
202 | return join "\n", @code; |
203 | } |
204 | |
e128626c |
205 | sub _report_unknown_args { |
8801a6e6 |
206 | my(undef, $metaclass, $attrs, $args) = @_; |
e128626c |
207 | |
208 | my @unknowns; |
209 | my %init_args; |
210 | foreach my $attr(@{$attrs}){ |
211 | my $init_arg = $attr->init_arg; |
212 | if(defined $init_arg){ |
213 | $init_args{$init_arg}++; |
214 | } |
215 | } |
216 | |
217 | while(my $key = each %{$args}){ |
218 | if(!exists $init_args{$key}){ |
219 | push @unknowns, $key; |
220 | } |
221 | } |
222 | |
223 | $metaclass->throw_error( sprintf |
224 | "Unknown attribute passed to the constructor of %s: %s", |
8801a6e6 |
225 | $metaclass->name, Mouse::Util::english_list(@unknowns), |
e128626c |
226 | ); |
227 | } |
228 | |
fc1d8369 |
229 | 1; |
0126c27c |
230 | __END__ |
a25ca8d6 |
231 | |
232 | =head1 NAME |
233 | |
234 | Mouse::Meta::Method::Constructor - A Mouse method generator for constructors |
235 | |
236 | =head1 VERSION |
237 | |
123fd4f8 |
238 | This document describes Mouse version 0.50_09 |
a25ca8d6 |
239 | |
240 | =head1 SEE ALSO |
241 | |
242 | L<Moose::Meta::Method::Constructor> |
243 | |
244 | =cut |