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