be89843918f982987359d2170d713bd6138a2a32
[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   = '0.81';
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         '$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
33                                    ? $type_constraint_obj->_compiled_type_constraint
34                                    : undef),
35     };
36
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;
46 }
47
48 sub _generate_accessor_method_inline {
49     my $self        = $_[0];
50     my $attr        = $self->associated_attribute;
51     my $attr_name   = $attr->name;
52     my $inv         = '$_[0]';
53     my $slot_access = $self->_inline_access($inv, $attr_name);
54     my $value_name  = $self->_value_needs_copy ? '$val' : '$_[1]';
55
56     $self->_eval_code('sub { ' . "\n"
57     . $self->_inline_pre_body(@_) . "\n"
58     . 'if (scalar(@_) >= 2) {' . "\n"
59         . $self->_inline_copy_value . "\n"
60         . $self->_inline_check_required . "\n"
61         . $self->_inline_check_coercion($value_name) . "\n"
62         . $self->_inline_check_constraint($value_name) . "\n"
63         . $self->_inline_store($inv, $value_name) . "\n"
64         . $self->_inline_trigger($inv, $value_name) . "\n"
65     . ' }' . "\n"
66     . $self->_inline_check_lazy($inv) . "\n"
67     . $self->_inline_post_body(@_) . "\n"
68     . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n"
69     . ' }');
70 }
71
72 sub _generate_writer_method_inline {
73     my $self        = $_[0];
74     my $attr        = $self->associated_attribute;
75     my $attr_name   = $attr->name;
76     my $inv         = '$_[0]';
77     my $slot_access = $self->_inline_get($inv, $attr_name);
78     my $value_name  = $self->_value_needs_copy ? '$val' : '$_[1]';
79
80     $self->_eval_code('sub { '
81     . $self->_inline_pre_body(@_)
82     . $self->_inline_copy_value
83     . $self->_inline_check_required
84     . $self->_inline_check_coercion($value_name)
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)
89     . ' }');
90 }
91
92 sub _generate_reader_method_inline {
93     my $self        = $_[0];
94     my $attr        = $self->associated_attribute;
95     my $attr_name   = $attr->name;
96     my $inv         = '$_[0]';
97     my $slot_access = $self->_inline_get($inv, $attr_name);
98
99     $self->_eval_code('sub {'
100     . $self->_inline_pre_body(@_)
101     . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
102     . $self->_inline_check_lazy($inv)
103     . $self->_inline_post_body(@_)
104     . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
105     . '}');
106 }
107
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
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(@_) }
123
124 sub _inline_pre_body  { '' }
125 sub _inline_post_body { '' }
126
127 sub _inline_check_constraint {
128     my ($self, $value) = @_;
129
130     my $attr = $self->associated_attribute;
131     my $attr_name = $attr->name;
132
133     return '' unless $attr->has_type_constraint;
134
135     my $type_constraint_name = $attr->type_constraint->name;
136
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") . ";";
138 }
139
140 sub _inline_check_coercion {
141     my ($self, $value) = @_;
142
143     my $attr = $self->associated_attribute;
144
145     return '' unless $attr->should_coerce;
146     return "$value = \$attr->type_constraint->coerce($value);";
147 }
148
149 sub _inline_check_required {
150     my $self = shift;
151     my $attr = $self->associated_attribute;
152
153     my $attr_name = $attr->name;
154
155     return '' unless $attr->is_required;
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
157 }
158
159 sub _inline_check_lazy {
160     my ($self, $instance) = @_;
161
162     my $attr = $self->associated_attribute;
163
164     return '' unless $attr->is_lazy;
165
166     my $slot_access = $self->_inline_access($instance, $attr->name);
167
168     my $slot_exists = $self->_inline_has($instance, $attr->name);
169
170     my $code = 'unless (' . $slot_exists . ') {' . "\n";
171     if ($attr->has_type_constraint) {
172         if ($attr->has_default || $attr->has_builder) {
173             if ($attr->has_default) {
174                 $code .= '    my $default = $attr->default(' . $instance . ');'."\n";
175             }
176             elsif ($attr->has_builder) {
177                 $code .= '    my $default;'."\n".
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    }";
182             }
183             $code .= $self->_inline_check_coercion('$default') . "\n";
184             $code .= $self->_inline_check_constraint('$default') . "\n";
185             $code .= '    ' . $self->_inline_init_slot($attr, $instance, $slot_access, '$default') . "\n";
186         }
187         else {
188             $code .= '    ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
189         }
190
191     } else {
192         if ($attr->has_default) {
193             $code .= '    ' . $self->_inline_init_slot($attr, $instance, $slot_access, ('$attr->default(' . $instance . ')')) . "\n";
194         }
195         elsif ($attr->has_builder) {
196             $code .= '    if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n"
197                   .  '       ' . $self->_inline_init_slot($attr, $instance, $slot_access, ($instance . '->$builder'))
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    }";
201         }
202         else {
203             $code .= '    ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
204         }
205     }
206     $code .= "}\n";
207     return $code;
208 }
209
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 . ';');
217     }
218 }
219
220 sub _inline_store {
221     my ($self, $instance, $value) = @_;
222     my $attr = $self->associated_attribute;
223
224     my $mi = $attr->associated_class->get_meta_instance;
225
226     my $code = $mi->inline_set_slot_value($instance, $attr->slots, $value)    . ";";
227     $code   .= $mi->inline_weaken_slot_value($instance, $attr->slots, $value) . ";"
228         if $attr->is_weak_ref;
229     return $code;
230 }
231
232 sub _inline_trigger {
233     my ($self, $instance, $value) = @_;
234     my $attr = $self->associated_attribute;
235     return '' unless $attr->has_trigger;
236     return sprintf('$attr->trigger->(%s, %s);', $instance, $value);
237 }
238
239 sub _inline_get {
240     my ($self, $instance) = @_;
241     my $attr = $self->associated_attribute;
242
243     my $mi = $attr->associated_class->get_meta_instance;
244
245     return $mi->inline_get_slot_value($instance, $attr->slots);
246 }
247
248 sub _inline_access {
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_slot_access($instance, $attr->slots);
255 }
256
257 sub _inline_has {
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_is_slot_initialized($instance, $attr->slots);
264 }
265
266 sub _inline_auto_deref {
267     my ( $self, $ref_value ) = @_;
268         my $attr = $self->associated_attribute;
269
270     return $ref_value unless $attr->should_auto_deref;
271
272     my $type_constraint = $attr->type_constraint;
273
274     my $sigil;
275     if ($type_constraint->is_a_type_of('ArrayRef')) {
276         $sigil = '@';
277     }
278     elsif ($type_constraint->is_a_type_of('HashRef')) {
279         $sigil = '%';
280     }
281     else {
282         $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", type_constraint => $type_constraint );
283     }
284
285     "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
286 }
287
288 1;
289
290 __END__
291
292 =pod
293
294 =head1 NAME
295
296 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
297
298 =head1 DESCRIPTION
299
300 This class is a subclass of L<Class::MOP::Method::Accessor> that
301 provides additional Moose-specific functionality, all of which is
302 private.
303
304 To understand this class, you should read the the
305 L<Class::MOP::Method::Accessor> documentation.
306
307 =head1 BUGS
308
309 All complex software has bugs lurking in it, and this module is no
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
321 Copyright 2006-2009 by Infinity Interactive, Inc.
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
328 =cut