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