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