bump version to 0.93_03
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor.pm
CommitLineData
8ee73eeb 1
2package Moose::Meta::Method::Accessor;
3
4use strict;
5use warnings;
6
7a10df4d 7our $VERSION = '0.93_03';
e606ae5f 8$VERSION = eval $VERSION;
d44714be 9our $AUTHORITY = 'cpan:STEVAN';
8ee73eeb 10
39b3bc94 11use base 'Moose::Meta::Method',
d617b644 12 'Class::MOP::Method::Accessor';
13
18748ad6 14sub _error_thrower {
15 my $self = shift;
16 ( ref $self && $self->associated_attribute ) || $self->SUPER::_error_thrower();
17}
d617b644 18
c9183ffc 19sub _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
3dda07f5 37 #warn "code for " . $attr->name . " =>\n" . $source . "\n";
34aab661 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 48sub _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]';
ac211120 53 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
d617b644 54
c9183ffc 55 $self->_eval_code('sub { ' . "\n"
e27dfc11 56 . $self->_inline_pre_body(@_) . "\n"
e7c06c1e 57 . 'if (scalar(@_) >= 2) {' . "\n"
ac211120 58 . $self->_inline_copy_value . "\n"
e27dfc11 59 . $self->_inline_check_required . "\n"
641e2264 60 . $self->_inline_check_coercion($value_name) . "\n"
e27dfc11 61 . $self->_inline_check_constraint($value_name) . "\n"
3dda07f5 62 . $self->_inline_get_old_value_for_trigger($inv, $value_name) . "\n"
32dd4a95 63 . $self->_inline_store($inv, $value_name) . "\n"
3dda07f5 64 . $self->_inline_trigger($inv, $value_name, '@old') . "\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 72sub _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)
3dda07f5 86 . $self->_inline_get_old_value_for_trigger($inv, $value_name) . "\n"
32dd4a95 87 . $self->_inline_store($inv, $value_name)
88 . $self->_inline_post_body(@_)
3dda07f5 89 . $self->_inline_trigger($inv, $value_name, '@old')
c9183ffc 90 . ' }');
d617b644 91}
92
6a4a7c31 93sub _generate_reader_method_inline {
3ccdc84a 94 my $self = $_[0];
26fbace8 95 my $attr = $self->associated_attribute;
3ccdc84a 96 my $attr_name = $attr->name;
97 my $inv = '$_[0]';
98 my $slot_access = $self->_inline_get($inv, $attr_name);
26fbace8 99
c9183ffc 100 $self->_eval_code('sub {'
c350159f 101 . $self->_inline_pre_body(@_)
cee532a1 102 . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
e606ae5f 103 . $self->_inline_check_lazy($inv)
c350159f 104 . $self->_inline_post_body(@_)
3ccdc84a 105 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
c9183ffc 106 . '}');
d617b644 107}
108
ac211120 109sub _inline_copy_value {
110 return '' unless shift->_value_needs_copy;
111 return 'my $val = $_[1];'
112}
113
114sub _value_needs_copy {
115 my $attr = (shift)->associated_attribute;
116 return $attr->should_coerce;
117}
118
6a4a7c31 119sub _generate_reader_method { shift->_generate_reader_method_inline(@_) }
120sub _generate_writer_method { shift->_generate_writer_method_inline(@_) }
121sub _generate_accessor_method { shift->_generate_accessor_method_inline(@_) }
122sub _generate_predicate_method { shift->_generate_predicate_method_inline(@_) }
123sub _generate_clearer_method { shift->_generate_clearer_method_inline(@_) }
8ecb1fa0 124
3ccdc84a 125sub _inline_pre_body { '' }
c350159f 126sub _inline_post_body { '' }
127
d617b644 128sub _inline_check_constraint {
97e11ef5 129 my ($self, $value) = @_;
d03bd989 130
97e11ef5 131 my $attr = $self->associated_attribute;
8bb7da15 132 my $attr_name = $attr->name;
d03bd989 133
97e11ef5 134 return '' unless $attr->has_type_constraint;
d03bd989 135
8bb7da15 136 my $type_constraint_name = $attr->type_constraint->name;
137
cee532a1 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") . ";";
d617b644 139}
140
141sub _inline_check_coercion {
641e2264 142 my ($self, $value) = @_;
143
144 my $attr = $self->associated_attribute;
d03bd989 145
97e11ef5 146 return '' unless $attr->should_coerce;
641e2264 147 return "$value = \$attr->type_constraint->coerce($value);";
d617b644 148}
149
150sub _inline_check_required {
cee532a1 151 my $self = shift;
152 my $attr = $self->associated_attribute;
8bb7da15 153
154 my $attr_name = $attr->name;
d03bd989 155
97e11ef5 156 return '' unless $attr->is_required;
cee532a1 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
d617b644 158}
159
160sub _inline_check_lazy {
e606ae5f 161 my ($self, $instance) = @_;
162
26fbace8 163 my $attr = $self->associated_attribute;
164
3822d5ee 165 return '' unless $attr->is_lazy;
26fbace8 166
e606ae5f 167 my $slot_exists = $self->_inline_has($instance, $attr->name);
3822d5ee 168
169 my $code = 'unless (' . $slot_exists . ') {' . "\n";
170 if ($attr->has_type_constraint) {
97e11ef5 171 if ($attr->has_default || $attr->has_builder) {
172 if ($attr->has_default) {
e606ae5f 173 $code .= ' my $default = $attr->default(' . $instance . ');'."\n";
d03bd989 174 }
97e11ef5 175 elsif ($attr->has_builder) {
3822d5ee 176 $code .= ' my $default;'."\n".
e606ae5f 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 }";
3822d5ee 181 }
641e2264 182 $code .= $self->_inline_check_coercion('$default') . "\n";
183 $code .= $self->_inline_check_constraint('$default') . "\n";
909405b8 184 $code .= ' ' . $self->_inline_init_slot($attr, $instance, '$default') . "\n";
d03bd989 185 }
97e11ef5 186 else {
909405b8 187 $code .= ' ' . $self->_inline_init_slot($attr, $instance, 'undef') . "\n";
26fbace8 188 }
189
3822d5ee 190 } else {
97e11ef5 191 if ($attr->has_default) {
909405b8 192 $code .= ' ' . $self->_inline_init_slot($attr, $instance, ('$attr->default(' . $instance . ')')) . "\n";
d03bd989 193 }
97e11ef5 194 elsif ($attr->has_builder) {
d03bd989 195 $code .= ' if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n"
909405b8 196 . ' ' . $self->_inline_init_slot($attr, $instance, ($instance . '->$builder'))
e606ae5f 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 }";
d03bd989 200 }
97e11ef5 201 else {
909405b8 202 $code .= ' ' . $self->_inline_init_slot($attr, $instance, 'undef') . "\n";
3822d5ee 203 }
204 }
205 $code .= "}\n";
206 return $code;
d617b644 207}
208
9df136d0 209sub _inline_init_slot {
909405b8 210 my ($self, $attr, $inv, $value) = @_;
9df136d0 211 if ($attr->has_initializer) {
212 return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');');
213 }
214 else {
909405b8 215 return $self->_inline_store($inv, $value);
d03bd989 216 }
9df136d0 217}
d617b644 218
219sub _inline_store {
97e11ef5 220 my ($self, $instance, $value) = @_;
221 my $attr = $self->associated_attribute;
d03bd989 222
97e11ef5 223 my $mi = $attr->associated_class->get_meta_instance;
d03bd989 224
eae37c67 225 my $code = $mi->inline_set_slot_value($instance, $attr->slots, $value) . ";";
226 $code .= $mi->inline_weaken_slot_value($instance, $attr->slots, $value) . ";"
97e11ef5 227 if $attr->is_weak_ref;
d617b644 228 return $code;
229}
230
3dda07f5 231sub _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
d617b644 246sub _inline_trigger {
3dda07f5 247 my ($self, $instance, $value, $old_value) = @_;
97e11ef5 248 my $attr = $self->associated_attribute;
249 return '' unless $attr->has_trigger;
3dda07f5 250 return sprintf('$attr->trigger->(%s, %s, %s);', $instance, $value, $old_value);
d617b644 251}
252
253sub _inline_get {
97e11ef5 254 my ($self, $instance) = @_;
255 my $attr = $self->associated_attribute;
d03bd989 256
97e11ef5 257 my $mi = $attr->associated_class->get_meta_instance;
d617b644 258
eae37c67 259 return $mi->inline_get_slot_value($instance, $attr->slots);
d617b644 260}
261
e27dfc11 262sub _inline_access {
97e11ef5 263 my ($self, $instance) = @_;
264 my $attr = $self->associated_attribute;
d03bd989 265
97e11ef5 266 my $mi = $attr->associated_class->get_meta_instance;
e27dfc11 267
eae37c67 268 return $mi->inline_slot_access($instance, $attr->slots);
e27dfc11 269}
270
271sub _inline_has {
97e11ef5 272 my ($self, $instance) = @_;
273 my $attr = $self->associated_attribute;
d03bd989 274
97e11ef5 275 my $mi = $attr->associated_class->get_meta_instance;
e27dfc11 276
eae37c67 277 return $mi->inline_is_slot_initialized($instance, $attr->slots);
e27dfc11 278}
279
d617b644 280sub _inline_auto_deref {
281 my ( $self, $ref_value ) = @_;
26fbace8 282 my $attr = $self->associated_attribute;
d617b644 283
39b3bc94 284 return $ref_value unless $attr->should_auto_deref;
d617b644 285
39b3bc94 286 my $type_constraint = $attr->type_constraint;
d617b644 287
288 my $sigil;
289 if ($type_constraint->is_a_type_of('ArrayRef')) {
290 $sigil = '@';
26fbace8 291 }
d617b644 292 elsif ($type_constraint->is_a_type_of('HashRef')) {
293 $sigil = '%';
26fbace8 294 }
d617b644 295 else {
46cb090f 296 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", type_constraint => $type_constraint );
d617b644 297 }
298
299 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
300}
8ee73eeb 301
3021;
303
304__END__
305
306=pod
307
39b3bc94 308=head1 NAME
309
ecb59493 310Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
39b3bc94 311
312=head1 DESCRIPTION
313
ba4dfb8e 314This class is a subclass of L<Class::MOP::Method::Accessor> that
73f769fc 315provides additional Moose-specific functionality, all of which is
316private.
ecb59493 317
73f769fc 318To understand this class, you should read the the
ba4dfb8e 319L<Class::MOP::Method::Accessor> documentation.
39b3bc94 320
321=head1 BUGS
322
d4048ef3 323See L<Moose/BUGS> for details on reporting bugs.
39b3bc94 324
325=head1 AUTHOR
326
327Stevan Little E<lt>stevan@iinteractive.comE<gt>
328
329Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
330
331=head1 COPYRIGHT AND LICENSE
332
7e0492d3 333Copyright 2006-2010 by Infinity Interactive, Inc.
39b3bc94 334
335L<http://www.iinteractive.com>
336
337This library is free software; you can redistribute it and/or modify
338it under the same terms as Perl itself.
339
51308c23 340=cut