Commit | Line | Data |
5cf3dbcf |
1 | |
2 | package Moose::Meta::Method::Constructor; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | use Scalar::Util 'blessed', 'weaken', 'looks_like_number'; |
9 | |
10 | our $VERSION = '0.01'; |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
13 | use base 'Moose::Meta::Method'; |
14 | |
15 | sub new { |
16 | my $class = shift; |
17 | my %options = @_; |
18 | |
19 | (exists $options{options} && ref $options{options} eq 'HASH') |
20 | || confess "You must pass a hash of options"; |
21 | |
22 | (blessed $options{meta_instance} && $options{meta_instance}->isa('Class::MOP::Instance')) |
23 | || confess "You must supply a meta-instance"; |
24 | |
25 | (exists $options{attributes} && ref $options{attributes} eq 'ARRAY') |
26 | || confess "You must pass an array of options"; |
27 | |
28 | (blessed($_) && $_->isa('Class::MOP::Attribute')) |
29 | || confess "You must supply a list of attributes which is a 'Class::MOP::Attribute' instance" |
30 | for @{$options{attributes}}; |
31 | |
32 | my $self = bless { |
33 | # from our superclass |
34 | '&!body' => undef, |
35 | # specific to this subclass |
36 | '%!options' => $options{options}, |
37 | '$!meta_instance' => $options{meta_instance}, |
38 | '@!attributes' => $options{attributes}, |
39 | # ... |
40 | '$!associated_metaclass' => $options{metaclass}, |
41 | } => $class; |
42 | |
43 | # we don't want this creating |
44 | # a cycle in the code, if not |
45 | # needed |
46 | weaken($self->{'$!meta_instance'}); |
47 | weaken($self->{'$!associated_metaclass'}); |
48 | |
49 | $self->intialize_body; |
50 | |
51 | return $self; |
52 | } |
53 | |
54 | ## accessors |
55 | |
56 | sub options { (shift)->{'%!options'} } |
57 | sub meta_instance { (shift)->{'$!meta_instance'} } |
58 | sub attributes { (shift)->{'@!attributes'} } |
59 | |
60 | sub associated_metaclass { (shift)->{'$!associated_metaclass'} } |
61 | |
62 | ## method |
63 | |
64 | sub intialize_body { |
65 | my $self = shift; |
66 | # TODO: |
67 | # the %options should also include a both |
68 | # a call 'initializer' and call 'SUPER::' |
69 | # options, which should cover approx 90% |
70 | # of the possible use cases (even if it |
71 | # requires some adaption on the part of |
72 | # the author, after all, nothing is free) |
73 | my $source = 'sub {'; |
74 | $source .= "\n" . 'my $class = shift; '; |
75 | $source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;'; |
76 | |
77 | $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class'); |
78 | |
79 | $source .= ";\n" . (join ";\n" => map { |
80 | $self->_generate_slot_initializer($_) |
81 | } 0 .. (@{$self->attributes} - 1)); |
82 | |
83 | $source .= ";\n" . $self->_generate_BUILDALL(); |
84 | |
85 | $source .= ";\n" . 'return $instance'; |
86 | $source .= ";\n" . '}'; |
87 | warn $source if $self->options->{debug}; |
88 | |
89 | my $code; |
90 | { |
91 | # NOTE: |
92 | # create the nessecary lexicals |
93 | # to be picked up in the eval |
94 | my $attrs = $self->attributes; |
95 | |
96 | $code = eval $source; |
97 | confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@; |
98 | } |
99 | $self->{'&!body'} = $code; |
100 | } |
101 | |
102 | sub _generate_BUILDALL { |
103 | my $self = shift; |
104 | my @BUILD_calls; |
105 | foreach my $method ($self->associated_metaclass->find_all_methods_by_name('BUILD')) { |
8ecb1fa0 |
106 | push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)'; |
5cf3dbcf |
107 | } |
108 | return join "\n" => @BUILD_calls; |
109 | } |
110 | |
111 | sub _generate_slot_initializer { |
112 | my $self = shift; |
113 | my $index = shift; |
114 | |
115 | my $attr = $self->attributes->[$index]; |
116 | |
117 | my @source = ('## ' . $attr->name); |
118 | |
119 | if ($attr->is_required && !$attr->has_default) { |
120 | push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' . |
121 | '|| confess "Attribute (' . $attr->name . ') is required";'); |
122 | } |
123 | |
5cf3dbcf |
124 | if ($attr->has_default && !$attr->is_lazy) { |
125 | |
8ecb1fa0 |
126 | push @source => 'if (exists $params{\'' . $attr->init_arg . '\'}) {'; |
127 | |
128 | push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};'); |
129 | if ($attr->has_type_constraint) { |
130 | push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;'); |
131 | |
132 | if ($attr->should_coerce && $attr->type_constraint->has_coercion) { |
133 | push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val'); |
134 | } |
135 | push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val'); |
136 | } |
137 | push @source => $self->_generate_slot_assignment($attr, '$val'); |
138 | |
139 | |
5cf3dbcf |
140 | push @source => "} else {"; |
141 | |
142 | my $default = $self->_generate_default_value($attr, $index); |
143 | |
144 | push @source => ('my $val = ' . $default . ';'); |
145 | push @source => $self->_generate_type_constraint_check( |
146 | $attr, |
147 | ('$attrs->[' . $index . ']->type_constraint'), |
148 | '$val' |
149 | ) if $attr->has_type_constraint; |
150 | push @source => $self->_generate_slot_assignment($attr, $default); |
151 | |
152 | push @source => "}"; |
153 | } |
154 | else { |
8ecb1fa0 |
155 | push @source => '(exists $params{\'' . $attr->init_arg . '\'}) && do {'; |
156 | |
157 | push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};'); |
158 | if ($attr->has_type_constraint) { |
159 | push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;'); |
160 | |
161 | if ($attr->should_coerce && $attr->type_constraint->has_coercion) { |
162 | push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val'); |
163 | } |
164 | push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val'); |
165 | } |
166 | push @source => $self->_generate_slot_assignment($attr, '$val'); |
167 | |
5cf3dbcf |
168 | push @source => "}"; |
169 | } |
170 | |
171 | return join "\n" => @source; |
172 | } |
173 | |
174 | sub _generate_slot_assignment { |
175 | my ($self, $attr, $value) = @_; |
176 | my $source = ( |
177 | $self->meta_instance->inline_set_slot_value( |
178 | '$instance', |
179 | ("'" . $attr->name . "'"), |
180 | $value |
181 | ) . ';' |
182 | ); |
183 | |
184 | if ($attr->is_weak_ref) { |
185 | $source .= ( |
186 | "\n" . |
187 | $self->meta_instance->inline_weaken_slot_value( |
188 | '$instance', |
189 | ("'" . $attr->name . "'") |
190 | ) . |
191 | ' if ref ' . $value . ';' |
192 | ); |
193 | } |
194 | |
195 | return $source; |
196 | } |
197 | |
198 | sub _generate_type_coercion { |
199 | my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_; |
200 | return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');'); |
201 | } |
202 | |
203 | sub _generate_type_constraint_check { |
204 | my ($self, $attr, $type_constraint_name, $value_name) = @_; |
205 | return ( |
206 | 'defined(' . $type_constraint_name . '->_compiled_type_constraint->(' . $value_name . '))' |
207 | . "\n\t" . '|| confess "Attribute (' . $attr->name . ') does not pass the type constraint (' |
208 | . $attr->type_constraint->name . ') with " . (defined() ? "' . $value_name . '" : "undef");' |
209 | ); |
210 | } |
211 | |
212 | sub _generate_default_value { |
213 | my ($self, $attr, $index) = @_; |
214 | # NOTE: |
215 | # default values can either be CODE refs |
216 | # in which case we need to call them. Or |
217 | # they can be scalars (strings/numbers) |
218 | # in which case we can just deal with them |
219 | # in the code we eval. |
220 | if ($attr->is_default_a_coderef) { |
221 | return '$attrs->[' . $index . ']->default($instance)'; |
222 | } |
223 | else { |
224 | my $default = $attr->default; |
225 | # make sure to quote strings ... |
226 | unless (looks_like_number($default)) { |
227 | $default = "'$default'"; |
228 | } |
229 | |
230 | return $default; |
231 | } |
232 | } |
233 | |
234 | 1; |
235 | |
236 | 1; |
237 | |
238 | __END__ |
239 | |
240 | =pod |
241 | |
242 | =head1 NAME |
243 | |
244 | Moose::Meta::Method::Constructor - Method Meta Object for constructors |
245 | |
246 | =head1 SYNOPSIS |
247 | |
248 | =head1 DESCRIPTION |
249 | |
250 | =head1 METHODS |
251 | |
252 | =over 4 |
253 | |
254 | =item B<new> |
255 | |
256 | =item B<attributes> |
257 | |
258 | =item B<meta_instance> |
259 | |
260 | =item B<options> |
261 | |
262 | =item B<intialize_body> |
263 | |
264 | =item B<associated_metaclass> |
265 | |
266 | =back |
267 | |
268 | =head1 AUTHORS |
269 | |
270 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
271 | |
272 | =head1 COPYRIGHT AND LICENSE |
273 | |
274 | Copyright 2006 by Infinity Interactive, Inc. |
275 | |
276 | L<http://www.iinteractive.com> |
277 | |
278 | This library is free software; you can redistribute it and/or modify |
279 | it under the same terms as Perl itself. |
280 | |
281 | =cut |
282 | |