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