2 package Moose::Meta::Method::Accessor;
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Method',
14 'Class::MOP::Method::Accessor';
18 return $self->associated_attribute
19 if ref($self) && defined($self->associated_attribute);
20 return $self->SUPER::_error_thrower;
27 $self->SUPER::_compile_code(@args);
31 'Could not create writer for '
32 . "'" . $self->associated_attribute->name . "' "
39 sub _eval_environment {
42 my $attr = $self->associated_attribute;
43 my $type_constraint_obj = $attr->type_constraint;
48 '$type_constraint_obj' => \$type_constraint_obj,
49 '$type_constraint' => \(
51 ? $type_constraint_obj->_compiled_type_constraint
57 sub _generate_accessor_method_inline {
61 my $slot_access = $self->_get_value($inv);
62 my $value = $self->_value_needs_copy ? '$val' : '$_[1]';
65 $self->_compile_code([
67 $self->_inline_pre_body(@_),
68 'if (scalar(@_) >= 2) {',
69 $self->_inline_copy_value($value),
70 $self->_inline_check_required,
71 $self->_inline_tc_code($value),
72 $self->_inline_get_old_value_for_trigger($inv, $old),
73 $self->_inline_store_value($inv, $value),
74 $self->_inline_trigger($inv, $value, $old),
76 $self->_inline_check_lazy($inv),
77 $self->_inline_post_body(@_),
78 $self->_inline_return_auto_deref($slot_access),
83 sub _generate_writer_method_inline {
87 my $value = $self->_value_needs_copy ? '$val' : '$_[1]';
90 $self->_compile_code([
92 $self->_inline_pre_body(@_),
93 $self->_inline_copy_value($value),
94 $self->_inline_check_required,
95 $self->_inline_tc_code($value),
96 $self->_inline_get_old_value_for_trigger($inv, $old),
97 $self->_inline_store_value($inv, $value),
98 $self->_inline_post_body(@_),
99 $self->_inline_trigger($inv, $value, $old),
104 sub _generate_reader_method_inline {
108 my $slot_access = $self->_get_value($inv);
110 $self->_compile_code([
112 $self->_inline_pre_body(@_),
114 $self->_inline_throw_error(
115 '"Cannot assign a value to a read-only accessor"',
119 $self->_inline_check_lazy($inv),
120 $self->_inline_post_body(@_),
121 $self->_inline_return_auto_deref($slot_access),
126 sub _inline_copy_value {
130 return unless $self->_value_needs_copy;
131 return 'my ' . $value . ' = $_[1];'
134 sub _value_needs_copy {
136 return $self->associated_attribute->should_coerce;
139 sub _instance_is_inlinable {
141 return $self->associated_attribute->associated_class->instance_metaclass->is_inlinable;
144 sub _generate_reader_method {
146 $self->_instance_is_inlinable ? $self->_generate_reader_method_inline(@_)
147 : $self->SUPER::_generate_reader_method(@_);
150 sub _generate_writer_method {
152 $self->_instance_is_inlinable ? $self->_generate_writer_method_inline(@_)
153 : $self->SUPER::_generate_writer_method(@_);
156 sub _generate_accessor_method {
158 $self->_instance_is_inlinable ? $self->_generate_accessor_method_inline(@_)
159 : $self->SUPER::_generate_accessor_method(@_);
162 sub _generate_predicate_method {
164 $self->_instance_is_inlinable ? $self->_generate_predicate_method_inline(@_)
165 : $self->SUPER::_generate_predicate_method(@_);
168 sub _generate_clearer_method {
170 $self->_instance_is_inlinable ? $self->_generate_clearer_method_inline(@_)
171 : $self->SUPER::_generate_clearer_method(@_);
174 sub _inline_pre_body { return }
175 sub _inline_post_body { return }
177 sub _inline_tc_code {
180 $self->_inline_check_coercion(@_),
181 $self->_inline_check_constraint(@_),
185 sub _inline_check_constraint {
189 my $attr = $self->associated_attribute;
190 return unless $attr->has_type_constraint;
192 my $attr_name = quotemeta($attr->name);
195 'if (!$type_constraint->(' . $value . ')) {',
196 $self->_inline_throw_error(
197 '"Attribute (' . $attr_name . ') does not pass the type '
198 . 'constraint because: " . '
199 . '$type_constraint_obj->get_message(' . $value . ')',
206 sub _inline_check_coercion {
210 my $attr = $self->associated_attribute;
211 return unless $attr->should_coerce && $attr->type_constraint->has_coercion;
213 return $value . ' = $type_constraint_obj->coerce(' . $value . ');';
216 sub _inline_check_required {
219 my $attr = $self->associated_attribute;
220 return unless $attr->is_required;
222 my $attr_name = quotemeta($attr->name);
226 $self->_inline_throw_error(
227 '"Attribute (' . $attr_name . ') is required, so cannot '
228 . 'be set to undef"' # defined $_[1] is not good enough
234 sub _inline_check_lazy {
236 my ($instance, $default) = @_;
238 my $attr = $self->associated_attribute;
239 return unless $attr->is_lazy;
241 my $slot_exists = $self->_has_value($instance);
244 'if (!' . $slot_exists . ') {',
245 $self->_inline_init_from_default($instance, '$default', 'lazy'),
250 sub _inline_init_from_default {
252 my ($instance, $default, $for_lazy) = @_;
254 my $attr = $self->associated_attribute;
256 if (!($attr->has_default || $attr->has_builder)) {
258 'You cannot have a lazy attribute '
259 . '(' . $attr->name . ') '
260 . 'without specifying a default value for it',
266 $self->_inline_generate_default($instance, $default),
267 # intentionally not using _inline_tc_code, since that can be overridden
268 # to do things like possibly only do member tc checks, which isn't
269 # appropriate for checking the result of a default
270 $attr->has_type_constraint
271 ? ($self->_inline_check_coercion($default, $for_lazy),
272 $self->_inline_check_constraint($default, $for_lazy))
274 $self->_inline_init_slot($attr, $instance, $default),
278 sub _inline_generate_default {
280 my ($instance, $default) = @_;
282 my $attr = $self->associated_attribute;
284 if ($attr->has_default) {
285 return 'my ' . $default . ' = $attr->default(' . $instance . ');';
287 elsif ($attr->has_builder) {
289 'my ' . $default . ';',
290 'if (my $builder = ' . $instance . '->can($attr->builder)) {',
291 $default . ' = ' . $instance . '->$builder;',
294 'my $class = ref(' . $instance . ') || ' . $instance . ';',
295 'my $builder_name = $attr->builder;',
296 'my $attr_name = $attr->name;',
297 $self->_inline_throw_error(
298 '"$class does not support builder method '
299 . '\'$builder_name\' for attribute \'$attr_name\'"'
306 "Can't generate a default for " . $attr->name
307 . " since no default or builder was specified"
312 sub _inline_init_slot {
314 my ($attr, $inv, $value) = @_;
316 if ($attr->has_initializer) {
317 return '$attr->set_initial_value(' . $inv . ', ' . $value . ');';
320 return $self->_inline_store_value($inv, $value);
324 sub _inline_store_value {
326 my ($inv, $value) = @_;
328 return $self->associated_attribute->_inline_set_value($inv, $value);
331 sub _inline_get_old_value_for_trigger {
333 my ($instance, $old) = @_;
335 my $attr = $self->associated_attribute;
336 return unless $attr->has_trigger;
339 'my ' . $old . ' = ' . $self->_has_value($instance),
340 '? ' . $self->_get_value($instance),
345 sub _inline_trigger {
347 my ($instance, $value, $old) = @_;
349 my $attr = $self->associated_attribute;
350 return unless $attr->has_trigger;
352 return '$attr->trigger->(' . $instance . ', ' . $value . ', ' . $old . ');';
355 sub _inline_return_auto_deref {
358 return 'return ' . $self->_auto_deref(@_) . ';';
364 my ($self, $instance) = @_;
366 return $self->associated_attribute->_inline_instance_get($instance);
370 my ($self, $instance) = @_;
372 return $self->associated_attribute->_inline_instance_has($instance);
377 my ($ref_value) = @_;
379 my $attr = $self->associated_attribute;
380 return $ref_value unless $attr->should_auto_deref;
382 my $type_constraint = $attr->type_constraint;
385 if ($type_constraint->is_a_type_of('ArrayRef')) {
388 elsif ($type_constraint->is_a_type_of('HashRef')) {
393 'Can not auto de-reference the type constraint \''
394 . $type_constraint->name
396 type_constraint => $type_constraint,
401 . '? ' . $sigil . '{ (' . $ref_value . ') || return } '
402 . ': (' . $ref_value . ')';
413 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
417 This class is a subclass of L<Class::MOP::Method::Accessor> that
418 provides additional Moose-specific functionality, all of which is
421 To understand this class, you should read the the
422 L<Class::MOP::Method::Accessor> documentation.
426 See L<Moose/BUGS> for details on reporting bugs.
430 Stevan Little E<lt>stevan@iinteractive.comE<gt>
432 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
434 =head1 COPYRIGHT AND LICENSE
436 Copyright 2006-2010 by Infinity Interactive, Inc.
438 L<http://www.iinteractive.com>
440 This library is free software; you can redistribute it and/or modify
441 it under the same terms as Perl itself.