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