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