2 package Moose::Meta::Method::Accessor;
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
11 use base 'Moose::Meta::Method',
12 'Class::MOP::Method::Accessor';
16 ( ref $self && $self->associated_attribute ) || $self->SUPER::_error_thrower();
20 my ( $self, $code ) = @_;
23 # set up the environment
24 my $attr = $self->associated_attribute;
25 my $type_constraint_obj = $attr->type_constraint;
28 '$attr_name' => \$attr->name,
30 '$type_constraint_obj' => \$type_constraint_obj,
31 '$type_constraint_name' => \($type_constraint_obj && $type_constraint_obj->name),
32 '$type_constraint' => \($type_constraint_obj
33 ? $type_constraint_obj->_compiled_type_constraint
37 #warn "code for $attr_name =>\n" . $code . "\n";
38 $self->_compile_code( environment => $environment, code => $code )
39 or $self->throw_error("Could not create writer for '${\$self->associated_attribute->name}' because $@ \n code: $code", error => $@, data => $code );
42 sub _generate_accessor_method_inline {
44 my $attr = $self->associated_attribute;
45 my $attr_name = $attr->name;
47 my $slot_access = $self->_inline_access($inv, $attr_name);
48 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
50 $self->_eval_code('sub { ' . "\n"
51 . $self->_inline_pre_body(@_) . "\n"
52 . 'if (scalar(@_) >= 2) {' . "\n"
53 . $self->_inline_copy_value . "\n"
54 . $self->_inline_check_required . "\n"
55 . $self->_inline_check_coercion($value_name) . "\n"
56 . $self->_inline_check_constraint($value_name) . "\n"
57 . $self->_inline_store($inv, $value_name) . "\n"
58 . $self->_inline_trigger($inv, $value_name) . "\n"
60 . $self->_inline_check_lazy($inv) . "\n"
61 . $self->_inline_post_body(@_) . "\n"
62 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n"
66 sub _generate_writer_method_inline {
68 my $attr = $self->associated_attribute;
69 my $attr_name = $attr->name;
71 my $slot_access = $self->_inline_get($inv, $attr_name);
72 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
74 $self->_eval_code('sub { '
75 . $self->_inline_pre_body(@_)
76 . $self->_inline_copy_value
77 . $self->_inline_check_required
78 . $self->_inline_check_coercion($value_name)
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)
86 sub _generate_reader_method_inline {
88 my $attr = $self->associated_attribute;
89 my $attr_name = $attr->name;
91 my $slot_access = $self->_inline_get($inv, $attr_name);
93 $self->_eval_code('sub {'
94 . $self->_inline_pre_body(@_)
95 . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
96 . $self->_inline_check_lazy($inv)
97 . $self->_inline_post_body(@_)
98 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
102 sub _inline_copy_value {
103 return '' unless shift->_value_needs_copy;
104 return 'my $val = $_[1];'
107 sub _value_needs_copy {
108 my $attr = (shift)->associated_attribute;
109 return $attr->should_coerce;
112 sub _generate_reader_method { shift->_generate_reader_method_inline(@_) }
113 sub _generate_writer_method { shift->_generate_writer_method_inline(@_) }
114 sub _generate_accessor_method { shift->_generate_accessor_method_inline(@_) }
115 sub _generate_predicate_method { shift->_generate_predicate_method_inline(@_) }
116 sub _generate_clearer_method { shift->_generate_clearer_method_inline(@_) }
118 sub _inline_pre_body { '' }
119 sub _inline_post_body { '' }
121 sub _inline_check_constraint {
122 my ($self, $value) = @_;
124 my $attr = $self->associated_attribute;
125 my $attr_name = $attr->name;
127 return '' unless $attr->has_type_constraint;
129 my $type_constraint_name = $attr->type_constraint->name;
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") . ";";
134 sub _inline_check_coercion {
135 my ($self, $value) = @_;
137 my $attr = $self->associated_attribute;
139 return '' unless $attr->should_coerce;
140 return "$value = \$attr->type_constraint->coerce($value);";
143 sub _inline_check_required {
145 my $attr = $self->associated_attribute;
147 my $attr_name = $attr->name;
149 return '' unless $attr->is_required;
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
153 sub _inline_check_lazy {
154 my ($self, $instance) = @_;
156 my $attr = $self->associated_attribute;
158 return '' unless $attr->is_lazy;
160 my $slot_access = $self->_inline_access($instance, $attr->name);
162 my $slot_exists = $self->_inline_has($instance, $attr->name);
164 my $code = 'unless (' . $slot_exists . ') {' . "\n";
165 if ($attr->has_type_constraint) {
166 if ($attr->has_default || $attr->has_builder) {
167 if ($attr->has_default) {
168 $code .= ' my $default = $attr->default(' . $instance . ');'."\n";
170 elsif ($attr->has_builder) {
171 $code .= ' my $default;'."\n".
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') .
177 $code .= $self->_inline_check_coercion('$default') . "\n";
178 $code .= $self->_inline_check_constraint('$default') . "\n";
179 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, '$default') . "\n";
182 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
186 if ($attr->has_default) {
187 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ('$attr->default(' . $instance . ')')) . "\n";
189 elsif ($attr->has_builder) {
190 $code .= ' if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n"
191 . ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ($instance . '->$builder'))
193 . ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name')
197 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
204 sub _inline_init_slot {
205 my ($self, $attr, $inv, $slot_access, $value) = @_;
206 if ($attr->has_initializer) {
207 return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');');
210 return ($slot_access . ' = ' . $value . ';');
215 my ($self, $instance, $value) = @_;
216 my $attr = $self->associated_attribute;
218 my $mi = $attr->associated_class->get_meta_instance;
220 my $code = $mi->inline_set_slot_value($instance, $attr->slots, $value) . ";";
221 $code .= $mi->inline_weaken_slot_value($instance, $attr->slots, $value) . ";"
222 if $attr->is_weak_ref;
226 sub _inline_trigger {
227 my ($self, $instance, $value) = @_;
228 my $attr = $self->associated_attribute;
229 return '' unless $attr->has_trigger;
230 return sprintf('$attr->trigger->(%s, %s);', $instance, $value);
234 my ($self, $instance) = @_;
235 my $attr = $self->associated_attribute;
237 my $mi = $attr->associated_class->get_meta_instance;
239 return $mi->inline_get_slot_value($instance, $attr->slots);
243 my ($self, $instance) = @_;
244 my $attr = $self->associated_attribute;
246 my $mi = $attr->associated_class->get_meta_instance;
248 return $mi->inline_slot_access($instance, $attr->slots);
252 my ($self, $instance) = @_;
253 my $attr = $self->associated_attribute;
255 my $mi = $attr->associated_class->get_meta_instance;
257 return $mi->inline_is_slot_initialized($instance, $attr->slots);
260 sub _inline_auto_deref {
261 my ( $self, $ref_value ) = @_;
262 my $attr = $self->associated_attribute;
264 return $ref_value unless $attr->should_auto_deref;
266 my $type_constraint = $attr->type_constraint;
269 if ($type_constraint->is_a_type_of('ArrayRef')) {
272 elsif ($type_constraint->is_a_type_of('HashRef')) {
276 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", type_constraint => $type_constraint );
279 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
290 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
294 This class is a subclass of L<Class::MOP::Class::Accessor> that
295 provides additional Moose-specific functionality, all of which is
298 To understand this class, you should read the the
299 L<Class::MOP::Class::Accessor> documentation.
303 All complex software has bugs lurking in it, and this module is no
304 exception. If you find a bug please either email me, or add the bug
309 Stevan Little E<lt>stevan@iinteractive.comE<gt>
311 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
313 =head1 COPYRIGHT AND LICENSE
315 Copyright 2006-2009 by Infinity Interactive, Inc.
317 L<http://www.iinteractive.com>
319 This library is free software; you can redistribute it and/or modify
320 it under the same terms as Perl itself.