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