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