Commit | Line | Data |
fc1d8369 |
1 | package Mouse::Meta::Method::Constructor; |
3821b191 |
2 | use Mouse::Util qw(:meta); # enables strict and warnings |
fc1d8369 |
3 | |
f8cbb121 |
4 | sub _inline_slot{ |
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 | |
2efc0af1 |
12 | my $associated_metaclass_name = $metaclass->name; |
0bfc7290 |
13 | |
7ca5c5fb |
14 | my @attrs = $metaclass->get_all_attributes; |
15 | |
16 | my $buildall = $class->_generate_BUILDALL($metaclass); |
17 | my $buildargs = $class->_generate_BUILDARGS($metaclass); |
18 | my $processattrs = $class->_generate_processattrs($metaclass, \@attrs); |
19 | |
0bfc7290 |
20 | my @checks = map { $_ && $_->_compiled_type_constraint } |
21 | map { $_->type_constraint } @attrs; |
ad087d11 |
22 | |
23 | my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"..."; |
24 | sub \{ |
2a464664 |
25 | my \$class = shift; |
26 | return \$class->Mouse::Object::new(\@_) |
27 | if \$class ne q{$associated_metaclass_name}; |
0bfc7290 |
28 | # BUILDARGS |
2a464664 |
29 | $buildargs; |
30 | my \$instance = bless {}, \$class; |
0bfc7290 |
31 | # process attributes |
2a464664 |
32 | $processattrs; |
0bfc7290 |
33 | # BUILDALL |
2a464664 |
34 | $buildall; |
35 | return \$instance; |
36 | } |
fc1d8369 |
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; |
54 | |
9df6f0cd |
55 | for my $index (0 .. @$attrs - 1) { |
0bfc7290 |
56 | my $code = ''; |
57 | |
c12edd9a |
58 | my $attr = $attrs->[$index]; |
fc1d8369 |
59 | my $key = $attr->name; |
9df6f0cd |
60 | |
0bfc7290 |
61 | my $init_arg = $attr->init_arg; |
62 | my $type_constraint = $attr->type_constraint; |
8aba926d |
63 | my $is_weak_ref = $attr->is_weak_ref; |
0bfc7290 |
64 | my $need_coercion; |
c91862e8 |
65 | |
f8cbb121 |
66 | my $instance_slot = $method_class->_inline_slot('$instance', $key); |
0bfc7290 |
67 | my $attr_var = "\$attrs[$index]"; |
68 | my $constraint_var; |
c91862e8 |
69 | |
0bfc7290 |
70 | if(defined $type_constraint){ |
71 | $constraint_var = "$attr_var\->{type_constraint}"; |
72 | $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion); |
73 | } |
c91862e8 |
74 | |
0bfc7290 |
75 | $code .= "# initialize $key\n"; |
76 | |
77 | my $post_process = ''; |
78 | if(defined $type_constraint){ |
79 | $post_process .= "\$checks[$index]->($instance_slot)"; |
da23cd4a |
80 | $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n"; |
0bfc7290 |
81 | } |
8aba926d |
82 | if($is_weak_ref){ |
0bfc7290 |
83 | $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n"; |
84 | } |
c91862e8 |
85 | |
0bfc7290 |
86 | if (defined $init_arg) { |
87 | my $value = "\$args->{q{$init_arg}}"; |
c91862e8 |
88 | |
0bfc7290 |
89 | $code .= "if (exists $value) {\n"; |
90 | |
91 | if($need_coercion){ |
620c3203 |
92 | $value = "$constraint_var->coerce($value)"; |
41cdacce |
93 | } |
c91862e8 |
94 | |
0bfc7290 |
95 | $code .= "$instance_slot = $value;\n"; |
96 | $code .= $post_process; |
97 | |
9df6f0cd |
98 | if ($attr->has_trigger) { |
2efc0af1 |
99 | $has_triggers++; |
620c3203 |
100 | $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n"; |
fc1d8369 |
101 | } |
c91862e8 |
102 | |
7756897f |
103 | $code .= "\n} else {\n"; |
9df6f0cd |
104 | } |
c91862e8 |
105 | |
9df6f0cd |
106 | if ($attr->has_default || $attr->has_builder) { |
107 | unless ($attr->is_lazy) { |
108 | my $default = $attr->default; |
109 | my $builder = $attr->builder; |
e8ba7b26 |
110 | |
0bfc7290 |
111 | my $value; |
112 | if (defined($builder)) { |
113 | $value = "\$instance->$builder()"; |
ffbbf459 |
114 | } |
115 | elsif (ref($default) eq 'CODE') { |
0bfc7290 |
116 | $value = "$attr_var\->{default}->(\$instance)"; |
ffbbf459 |
117 | } |
0bfc7290 |
118 | elsif (defined($default)) { |
119 | $value = "$attr_var\->{default}"; |
ffbbf459 |
120 | } |
121 | else { |
0bfc7290 |
122 | $value = 'undef'; |
ffbbf459 |
123 | } |
e8ba7b26 |
124 | |
0bfc7290 |
125 | if($need_coercion){ |
126 | $value = "$constraint_var->coerce($value)"; |
9df6f0cd |
127 | } |
9df6f0cd |
128 | |
0bfc7290 |
129 | $code .= "$instance_slot = $value;\n"; |
8aba926d |
130 | if($is_weak_ref){ |
131 | $code .= "Scalar::Util::weaken($instance_slot);\n"; |
132 | } |
fc1d8369 |
133 | } |
713a2a05 |
134 | } |
9df6f0cd |
135 | elsif ($attr->is_required) { |
7756897f |
136 | $code .= "Carp::confess('Attribute ($key) is required');"; |
9df6f0cd |
137 | } |
138 | |
0bfc7290 |
139 | $code .= "}\n" if defined $init_arg; |
9df6f0cd |
140 | |
fc1d8369 |
141 | push @res, $code; |
142 | } |
9df6f0cd |
143 | |
2efc0af1 |
144 | if($metaclass->is_anon_class){ |
a8391b11 |
145 | push @res, q{$instance->{__METACLASS__} = $metaclass;}; |
2efc0af1 |
146 | } |
147 | |
148 | if($has_triggers){ |
149 | unshift @res, q{my @triggers;}; |
150 | push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;}; |
151 | } |
152 | |
153 | return join "\n", @res; |
fc1d8369 |
154 | } |
155 | |
156 | sub _generate_BUILDARGS { |
0bfc7290 |
157 | my(undef, $metaclass) = @_; |
9dcd7d23 |
158 | |
0bfc7290 |
159 | my $class = $metaclass->name; |
160 | if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) { |
53d4053e |
161 | return 'my $args = $class->BUILDARGS(@_)'; |
9dcd7d23 |
162 | } |
163 | |
164 | return <<'...'; |
53d4053e |
165 | my $args; |
fc1d8369 |
166 | if ( scalar @_ == 1 ) { |
c9aefe26 |
167 | ( ref( $_[0] ) eq 'HASH' ) |
fc1d8369 |
168 | || Carp::confess "Single parameters to new() must be a HASH ref"; |
53d4053e |
169 | $args = +{ %{ $_[0] } }; |
fc1d8369 |
170 | } |
171 | else { |
53d4053e |
172 | $args = +{@_}; |
fc1d8369 |
173 | } |
fc1d8369 |
174 | ... |
175 | } |
176 | |
177 | sub _generate_BUILDALL { |
0bfc7290 |
178 | my (undef, $metaclass) = @_; |
7ca5c5fb |
179 | |
2efc0af1 |
180 | return '' unless $metaclass->name->can('BUILD'); |
fc1d8369 |
181 | |
7ca5c5fb |
182 | my @code; |
183 | for my $class ($metaclass->linearized_isa) { |
a5c683f6 |
184 | if (Mouse::Util::get_code_ref($class, 'BUILD')) { |
7ca5c5fb |
185 | unshift @code, qq{${class}::BUILD(\$instance, \$args);}; |
fc1d8369 |
186 | } |
187 | } |
188 | return join "\n", @code; |
189 | } |
190 | |
191 | 1; |
0126c27c |
192 | __END__ |
a25ca8d6 |
193 | |
194 | =head1 NAME |
195 | |
196 | Mouse::Meta::Method::Constructor - A Mouse method generator for constructors |
197 | |
198 | =head1 VERSION |
199 | |
8c235ea7 |
200 | This document describes Mouse version 0.46 |
a25ca8d6 |
201 | |
202 | =head1 SEE ALSO |
203 | |
204 | L<Moose::Meta::Method::Constructor> |
205 | |
206 | =cut |