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