Commit | Line | Data |
8ee73eeb |
1 | |
2 | package Moose::Meta::Method::Accessor; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
55d05fb1 |
7 | our $VERSION = '1.12'; |
e606ae5f |
8 | $VERSION = eval $VERSION; |
d44714be |
9 | our $AUTHORITY = 'cpan:STEVAN'; |
8ee73eeb |
10 | |
39b3bc94 |
11 | use base 'Moose::Meta::Method', |
d617b644 |
12 | 'Class::MOP::Method::Accessor'; |
13 | |
18748ad6 |
14 | sub _error_thrower { |
15 | my $self = shift; |
16 | ( ref $self && $self->associated_attribute ) || $self->SUPER::_error_thrower(); |
17 | } |
d617b644 |
18 | |
c9183ffc |
19 | sub _eval_code { |
34aab661 |
20 | my ( $self, $source ) = @_; |
c9183ffc |
21 | |
22 | # NOTE: |
23 | # set up the environment |
1aeb1548 |
24 | my $attr = $self->associated_attribute; |
25 | my $type_constraint_obj = $attr->type_constraint; |
26 | my $environment = { |
27 | '$attr' => \$attr, |
1aeb1548 |
28 | '$meta' => \$self, |
29 | '$type_constraint_obj' => \$type_constraint_obj, |
1aeb1548 |
30 | '$type_constraint' => \($type_constraint_obj |
9df136d0 |
31 | ? $type_constraint_obj->_compiled_type_constraint |
1aeb1548 |
32 | : undef), |
33 | }; |
c9183ffc |
34 | |
3dda07f5 |
35 | #warn "code for " . $attr->name . " =>\n" . $source . "\n"; |
34aab661 |
36 | my ( $code, $e ) = $self->_compile_code( environment => $environment, code => $source ); |
37 | |
38 | $self->throw_error( |
39 | "Could not create writer for '${\$self->associated_attribute->name}' because $e \n code: $source", |
40 | error => $e, data => $source ) |
41 | if $e; |
42 | |
43 | return $code; |
c9183ffc |
44 | } |
45 | |
6a4a7c31 |
46 | sub _generate_accessor_method_inline { |
3ccdc84a |
47 | my $self = $_[0]; |
26fbace8 |
48 | my $attr = $self->associated_attribute; |
3ccdc84a |
49 | my $inv = '$_[0]'; |
ac211120 |
50 | my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]'; |
d617b644 |
51 | |
c9183ffc |
52 | $self->_eval_code('sub { ' . "\n" |
e27dfc11 |
53 | . $self->_inline_pre_body(@_) . "\n" |
e7c06c1e |
54 | . 'if (scalar(@_) >= 2) {' . "\n" |
ac211120 |
55 | . $self->_inline_copy_value . "\n" |
e27dfc11 |
56 | . $self->_inline_check_required . "\n" |
641e2264 |
57 | . $self->_inline_check_coercion($value_name) . "\n" |
e27dfc11 |
58 | . $self->_inline_check_constraint($value_name) . "\n" |
3dda07f5 |
59 | . $self->_inline_get_old_value_for_trigger($inv, $value_name) . "\n" |
32dd4a95 |
60 | . $self->_inline_store($inv, $value_name) . "\n" |
3dda07f5 |
61 | . $self->_inline_trigger($inv, $value_name, '@old') . "\n" |
e27dfc11 |
62 | . ' }' . "\n" |
e606ae5f |
63 | . $self->_inline_check_lazy($inv) . "\n" |
e27dfc11 |
64 | . $self->_inline_post_body(@_) . "\n" |
65 | . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n" |
c9183ffc |
66 | . ' }'); |
d617b644 |
67 | } |
68 | |
6a4a7c31 |
69 | sub _generate_writer_method_inline { |
3ccdc84a |
70 | my $self = $_[0]; |
26fbace8 |
71 | my $attr = $self->associated_attribute; |
3ccdc84a |
72 | my $inv = '$_[0]'; |
95d922a2 |
73 | my $slot_access = $self->_inline_get($inv); |
ac211120 |
74 | my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]'; |
3ccdc84a |
75 | |
c9183ffc |
76 | $self->_eval_code('sub { ' |
c350159f |
77 | . $self->_inline_pre_body(@_) |
ac211120 |
78 | . $self->_inline_copy_value |
d617b644 |
79 | . $self->_inline_check_required |
641e2264 |
80 | . $self->_inline_check_coercion($value_name) |
32dd4a95 |
81 | . $self->_inline_check_constraint($value_name) |
3dda07f5 |
82 | . $self->_inline_get_old_value_for_trigger($inv, $value_name) . "\n" |
32dd4a95 |
83 | . $self->_inline_store($inv, $value_name) |
84 | . $self->_inline_post_body(@_) |
3dda07f5 |
85 | . $self->_inline_trigger($inv, $value_name, '@old') |
c9183ffc |
86 | . ' }'); |
d617b644 |
87 | } |
88 | |
6a4a7c31 |
89 | sub _generate_reader_method_inline { |
3ccdc84a |
90 | my $self = $_[0]; |
26fbace8 |
91 | my $attr = $self->associated_attribute; |
3ccdc84a |
92 | my $inv = '$_[0]'; |
95d922a2 |
93 | my $slot_access = $self->_inline_get($inv); |
26fbace8 |
94 | |
c9183ffc |
95 | $self->_eval_code('sub {' |
c350159f |
96 | . $self->_inline_pre_body(@_) |
cee532a1 |
97 | . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;' |
e606ae5f |
98 | . $self->_inline_check_lazy($inv) |
c350159f |
99 | . $self->_inline_post_body(@_) |
3ccdc84a |
100 | . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';' |
c9183ffc |
101 | . '}'); |
d617b644 |
102 | } |
103 | |
ac211120 |
104 | sub _inline_copy_value { |
105 | return '' unless shift->_value_needs_copy; |
106 | return 'my $val = $_[1];' |
107 | } |
108 | |
109 | sub _value_needs_copy { |
110 | my $attr = (shift)->associated_attribute; |
111 | return $attr->should_coerce; |
112 | } |
113 | |
f001c60f |
114 | sub _instance_is_inlinable { |
115 | my $self = shift; |
116 | return $self->associated_attribute->associated_class->instance_metaclass->is_inlinable; |
117 | } |
118 | |
119 | sub _generate_reader_method { |
120 | my $self = shift; |
121 | $self->_instance_is_inlinable ? $self->_generate_reader_method_inline(@_) |
122 | : $self->SUPER::_generate_reader_method(@_); |
123 | } |
124 | |
125 | sub _generate_writer_method { |
126 | my $self = shift; |
127 | $self->_instance_is_inlinable ? $self->_generate_writer_method_inline(@_) |
128 | : $self->SUPER::_generate_writer_method(@_); |
129 | } |
130 | |
131 | sub _generate_accessor_method { |
132 | my $self = shift; |
133 | $self->_instance_is_inlinable ? $self->_generate_accessor_method_inline(@_) |
134 | : $self->SUPER::_generate_accessor_method(@_); |
135 | } |
136 | |
137 | sub _generate_predicate_method { |
138 | my $self = shift; |
139 | $self->_instance_is_inlinable ? $self->_generate_predicate_method_inline(@_) |
140 | : $self->SUPER::_generate_predicate_method(@_); |
141 | } |
142 | |
143 | sub _generate_clearer_method { |
144 | my $self = shift; |
145 | $self->_instance_is_inlinable ? $self->_generate_clearer_method_inline(@_) |
146 | : $self->SUPER::_generate_clearer_method(@_); |
147 | } |
8ecb1fa0 |
148 | |
3ccdc84a |
149 | sub _inline_pre_body { '' } |
c350159f |
150 | sub _inline_post_body { '' } |
151 | |
d617b644 |
152 | sub _inline_check_constraint { |
97e11ef5 |
153 | my ($self, $value) = @_; |
d03bd989 |
154 | |
97e11ef5 |
155 | my $attr = $self->associated_attribute; |
d03bd989 |
156 | |
97e11ef5 |
157 | return '' unless $attr->has_type_constraint; |
d03bd989 |
158 | |
95d922a2 |
159 | my $attr_name = quotemeta( $attr->name ); |
8bb7da15 |
160 | |
cee532a1 |
161 | qq{\$type_constraint->($value) || } . $self->_inline_throw_error(qq{"Attribute ($attr_name) does not pass the type constraint because: " . \$type_constraint_obj->get_message($value)}, "data => $value") . ";"; |
d617b644 |
162 | } |
163 | |
164 | sub _inline_check_coercion { |
641e2264 |
165 | my ($self, $value) = @_; |
166 | |
167 | my $attr = $self->associated_attribute; |
d03bd989 |
168 | |
bb8afe3d |
169 | return '' unless $attr->should_coerce && $attr->type_constraint->has_coercion; |
641e2264 |
170 | return "$value = \$attr->type_constraint->coerce($value);"; |
d617b644 |
171 | } |
172 | |
173 | sub _inline_check_required { |
cee532a1 |
174 | my $self = shift; |
175 | my $attr = $self->associated_attribute; |
8bb7da15 |
176 | |
97e11ef5 |
177 | return '' unless $attr->is_required; |
95d922a2 |
178 | |
179 | my $attr_name = quotemeta( $attr->name ); |
180 | |
cee532a1 |
181 | return qq{(\@_ >= 2) || } . $self->_inline_throw_error(qq{"Attribute ($attr_name) is required, so cannot be set to undef"}) . ';' # defined $_[1] is not good enough |
d617b644 |
182 | } |
183 | |
184 | sub _inline_check_lazy { |
e606ae5f |
185 | my ($self, $instance) = @_; |
186 | |
26fbace8 |
187 | my $attr = $self->associated_attribute; |
188 | |
3822d5ee |
189 | return '' unless $attr->is_lazy; |
26fbace8 |
190 | |
95d922a2 |
191 | my $slot_exists = $self->_inline_has($instance); |
3822d5ee |
192 | |
193 | my $code = 'unless (' . $slot_exists . ') {' . "\n"; |
194 | if ($attr->has_type_constraint) { |
97e11ef5 |
195 | if ($attr->has_default || $attr->has_builder) { |
196 | if ($attr->has_default) { |
e606ae5f |
197 | $code .= ' my $default = $attr->default(' . $instance . ');'."\n"; |
d03bd989 |
198 | } |
97e11ef5 |
199 | elsif ($attr->has_builder) { |
3822d5ee |
200 | $code .= ' my $default;'."\n". |
e606ae5f |
201 | ' if(my $builder = '.$instance.'->can($attr->builder)){ '."\n". |
202 | ' $default = '.$instance.'->$builder; '. "\n } else {\n" . |
203 | ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name') . |
204 | ';'. "\n }"; |
3822d5ee |
205 | } |
641e2264 |
206 | $code .= $self->_inline_check_coercion('$default') . "\n"; |
207 | $code .= $self->_inline_check_constraint('$default') . "\n"; |
909405b8 |
208 | $code .= ' ' . $self->_inline_init_slot($attr, $instance, '$default') . "\n"; |
d03bd989 |
209 | } |
97e11ef5 |
210 | else { |
909405b8 |
211 | $code .= ' ' . $self->_inline_init_slot($attr, $instance, 'undef') . "\n"; |
26fbace8 |
212 | } |
213 | |
3822d5ee |
214 | } else { |
97e11ef5 |
215 | if ($attr->has_default) { |
909405b8 |
216 | $code .= ' ' . $self->_inline_init_slot($attr, $instance, ('$attr->default(' . $instance . ')')) . "\n"; |
d03bd989 |
217 | } |
97e11ef5 |
218 | elsif ($attr->has_builder) { |
d03bd989 |
219 | $code .= ' if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n" |
909405b8 |
220 | . ' ' . $self->_inline_init_slot($attr, $instance, ($instance . '->$builder')) |
e606ae5f |
221 | . "\n } else {\n" |
222 | . ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name') |
223 | . ';'. "\n }"; |
d03bd989 |
224 | } |
97e11ef5 |
225 | else { |
909405b8 |
226 | $code .= ' ' . $self->_inline_init_slot($attr, $instance, 'undef') . "\n"; |
3822d5ee |
227 | } |
228 | } |
229 | $code .= "}\n"; |
230 | return $code; |
d617b644 |
231 | } |
232 | |
9df136d0 |
233 | sub _inline_init_slot { |
909405b8 |
234 | my ($self, $attr, $inv, $value) = @_; |
9df136d0 |
235 | if ($attr->has_initializer) { |
236 | return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');'); |
237 | } |
238 | else { |
909405b8 |
239 | return $self->_inline_store($inv, $value); |
d03bd989 |
240 | } |
9df136d0 |
241 | } |
d617b644 |
242 | |
243 | sub _inline_store { |
97e11ef5 |
244 | my ($self, $instance, $value) = @_; |
245 | my $attr = $self->associated_attribute; |
d03bd989 |
246 | |
97e11ef5 |
247 | my $mi = $attr->associated_class->get_meta_instance; |
d03bd989 |
248 | |
eae37c67 |
249 | my $code = $mi->inline_set_slot_value($instance, $attr->slots, $value) . ";"; |
250 | $code .= $mi->inline_weaken_slot_value($instance, $attr->slots, $value) . ";" |
97e11ef5 |
251 | if $attr->is_weak_ref; |
d617b644 |
252 | return $code; |
253 | } |
254 | |
3dda07f5 |
255 | sub _inline_get_old_value_for_trigger { |
256 | my ( $self, $instance ) = @_; |
257 | |
258 | my $attr = $self->associated_attribute; |
259 | return '' unless $attr->has_trigger; |
260 | |
261 | my $mi = $attr->associated_class->get_meta_instance; |
262 | my $pred = $mi->inline_is_slot_initialized($instance, $attr->name); |
263 | |
264 | return |
265 | 'my @old = ' |
266 | . $pred . q{ ? } |
267 | . $self->_inline_get($instance) . q{ : ()} . ";\n"; |
268 | } |
269 | |
d617b644 |
270 | sub _inline_trigger { |
3dda07f5 |
271 | my ($self, $instance, $value, $old_value) = @_; |
97e11ef5 |
272 | my $attr = $self->associated_attribute; |
273 | return '' unless $attr->has_trigger; |
3dda07f5 |
274 | return sprintf('$attr->trigger->(%s, %s, %s);', $instance, $value, $old_value); |
d617b644 |
275 | } |
276 | |
277 | sub _inline_get { |
97e11ef5 |
278 | my ($self, $instance) = @_; |
279 | my $attr = $self->associated_attribute; |
d03bd989 |
280 | |
97e11ef5 |
281 | my $mi = $attr->associated_class->get_meta_instance; |
d617b644 |
282 | |
eae37c67 |
283 | return $mi->inline_get_slot_value($instance, $attr->slots); |
d617b644 |
284 | } |
285 | |
e27dfc11 |
286 | sub _inline_access { |
97e11ef5 |
287 | my ($self, $instance) = @_; |
288 | my $attr = $self->associated_attribute; |
d03bd989 |
289 | |
97e11ef5 |
290 | my $mi = $attr->associated_class->get_meta_instance; |
e27dfc11 |
291 | |
eae37c67 |
292 | return $mi->inline_slot_access($instance, $attr->slots); |
e27dfc11 |
293 | } |
294 | |
295 | sub _inline_has { |
97e11ef5 |
296 | my ($self, $instance) = @_; |
297 | my $attr = $self->associated_attribute; |
d03bd989 |
298 | |
97e11ef5 |
299 | my $mi = $attr->associated_class->get_meta_instance; |
e27dfc11 |
300 | |
eae37c67 |
301 | return $mi->inline_is_slot_initialized($instance, $attr->slots); |
e27dfc11 |
302 | } |
303 | |
d617b644 |
304 | sub _inline_auto_deref { |
305 | my ( $self, $ref_value ) = @_; |
26fbace8 |
306 | my $attr = $self->associated_attribute; |
d617b644 |
307 | |
39b3bc94 |
308 | return $ref_value unless $attr->should_auto_deref; |
d617b644 |
309 | |
39b3bc94 |
310 | my $type_constraint = $attr->type_constraint; |
d617b644 |
311 | |
312 | my $sigil; |
313 | if ($type_constraint->is_a_type_of('ArrayRef')) { |
314 | $sigil = '@'; |
26fbace8 |
315 | } |
d617b644 |
316 | elsif ($type_constraint->is_a_type_of('HashRef')) { |
317 | $sigil = '%'; |
26fbace8 |
318 | } |
d617b644 |
319 | else { |
95d922a2 |
320 | $self->throw_error( "Can not auto de-reference the type constraint '" |
321 | . quotemeta( $type_constraint->name ) |
322 | . "'", type_constraint => $type_constraint ); |
d617b644 |
323 | } |
324 | |
325 | "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )"; |
326 | } |
8ee73eeb |
327 | |
328 | 1; |
329 | |
330 | __END__ |
331 | |
332 | =pod |
333 | |
39b3bc94 |
334 | =head1 NAME |
335 | |
ecb59493 |
336 | Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors |
39b3bc94 |
337 | |
338 | =head1 DESCRIPTION |
339 | |
ba4dfb8e |
340 | This class is a subclass of L<Class::MOP::Method::Accessor> that |
73f769fc |
341 | provides additional Moose-specific functionality, all of which is |
342 | private. |
ecb59493 |
343 | |
73f769fc |
344 | To understand this class, you should read the the |
ba4dfb8e |
345 | L<Class::MOP::Method::Accessor> documentation. |
39b3bc94 |
346 | |
347 | =head1 BUGS |
348 | |
d4048ef3 |
349 | See L<Moose/BUGS> for details on reporting bugs. |
39b3bc94 |
350 | |
351 | =head1 AUTHOR |
352 | |
353 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
354 | |
355 | Yuval Kogman E<lt>nothingmuch@woobling.comE<gt> |
356 | |
357 | =head1 COPYRIGHT AND LICENSE |
358 | |
7e0492d3 |
359 | Copyright 2006-2010 by Infinity Interactive, Inc. |
39b3bc94 |
360 | |
361 | L<http://www.iinteractive.com> |
362 | |
363 | This library is free software; you can redistribute it and/or modify |
364 | it under the same terms as Perl itself. |
365 | |
51308c23 |
366 | =cut |