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, $source ) = @_;
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" . $source . "\n";
38 my ( $code, $e ) = $self->_compile_code( environment => $environment, code => $source );
41 "Could not create writer for '${\$self->associated_attribute->name}' because $e \n code: $source",
42 error => $e, data => $source )
48 sub _generate_accessor_method_inline {
50 my $attr = $self->associated_attribute;
51 my $attr_name = $attr->name;
53 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
55 $self->_eval_code('sub { ' . "\n"
56 . $self->_inline_pre_body(@_) . "\n"
57 . 'if (scalar(@_) >= 2) {' . "\n"
58 . $self->_inline_copy_value . "\n"
59 . $self->_inline_check_required . "\n"
60 . $self->_inline_check_coercion($value_name) . "\n"
61 . $self->_inline_check_constraint($value_name) . "\n"
62 . $self->_inline_get_old_value_for_trigger($inv, $value_name) . "\n"
63 . $self->_inline_store($inv, $value_name) . "\n"
64 . $self->_inline_trigger($inv, $value_name, '@old') . "\n"
66 . $self->_inline_check_lazy($inv) . "\n"
67 . $self->_inline_post_body(@_) . "\n"
68 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n"
72 sub _generate_writer_method_inline {
74 my $attr = $self->associated_attribute;
75 my $attr_name = $attr->name;
77 my $slot_access = $self->_inline_get($inv, $attr_name);
78 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
80 $self->_eval_code('sub { '
81 . $self->_inline_pre_body(@_)
82 . $self->_inline_copy_value
83 . $self->_inline_check_required
84 . $self->_inline_check_coercion($value_name)
85 . $self->_inline_check_constraint($value_name)
86 . $self->_inline_get_old_value_for_trigger($inv, $value_name) . "\n"
87 . $self->_inline_store($inv, $value_name)
88 . $self->_inline_post_body(@_)
89 . $self->_inline_trigger($inv, $value_name, '@old')
93 sub _generate_reader_method_inline {
95 my $attr = $self->associated_attribute;
96 my $attr_name = $attr->name;
98 my $slot_access = $self->_inline_get($inv, $attr_name);
100 $self->_eval_code('sub {'
101 . $self->_inline_pre_body(@_)
102 . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
103 . $self->_inline_check_lazy($inv)
104 . $self->_inline_post_body(@_)
105 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
109 sub _inline_copy_value {
110 return '' unless shift->_value_needs_copy;
111 return 'my $val = $_[1];'
114 sub _value_needs_copy {
115 my $attr = (shift)->associated_attribute;
116 return $attr->should_coerce;
119 sub _generate_reader_method { shift->_generate_reader_method_inline(@_) }
120 sub _generate_writer_method { shift->_generate_writer_method_inline(@_) }
121 sub _generate_accessor_method { shift->_generate_accessor_method_inline(@_) }
122 sub _generate_predicate_method { shift->_generate_predicate_method_inline(@_) }
123 sub _generate_clearer_method { shift->_generate_clearer_method_inline(@_) }
125 sub _inline_pre_body { '' }
126 sub _inline_post_body { '' }
128 sub _inline_check_constraint {
129 my ($self, $value) = @_;
131 my $attr = $self->associated_attribute;
132 my $attr_name = $attr->name;
134 return '' unless $attr->has_type_constraint;
136 my $type_constraint_name = $attr->type_constraint->name;
138 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") . ";";
141 sub _inline_check_coercion {
142 my ($self, $value) = @_;
144 my $attr = $self->associated_attribute;
146 return '' unless $attr->should_coerce;
147 return "$value = \$attr->type_constraint->coerce($value);";
150 sub _inline_check_required {
152 my $attr = $self->associated_attribute;
154 my $attr_name = $attr->name;
156 return '' unless $attr->is_required;
157 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
160 sub _inline_check_lazy {
161 my ($self, $instance) = @_;
163 my $attr = $self->associated_attribute;
165 return '' unless $attr->is_lazy;
167 my $slot_exists = $self->_inline_has($instance, $attr->name);
169 my $code = 'unless (' . $slot_exists . ') {' . "\n";
170 if ($attr->has_type_constraint) {
171 if ($attr->has_default || $attr->has_builder) {
172 if ($attr->has_default) {
173 $code .= ' my $default = $attr->default(' . $instance . ');'."\n";
175 elsif ($attr->has_builder) {
176 $code .= ' my $default;'."\n".
177 ' if(my $builder = '.$instance.'->can($attr->builder)){ '."\n".
178 ' $default = '.$instance.'->$builder; '. "\n } else {\n" .
179 ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name') .
182 $code .= $self->_inline_check_coercion('$default') . "\n";
183 $code .= $self->_inline_check_constraint('$default') . "\n";
184 $code .= ' ' . $self->_inline_init_slot($attr, $instance, '$default') . "\n";
187 $code .= ' ' . $self->_inline_init_slot($attr, $instance, 'undef') . "\n";
191 if ($attr->has_default) {
192 $code .= ' ' . $self->_inline_init_slot($attr, $instance, ('$attr->default(' . $instance . ')')) . "\n";
194 elsif ($attr->has_builder) {
195 $code .= ' if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n"
196 . ' ' . $self->_inline_init_slot($attr, $instance, ($instance . '->$builder'))
198 . ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name')
202 $code .= ' ' . $self->_inline_init_slot($attr, $instance, 'undef') . "\n";
209 sub _inline_init_slot {
210 my ($self, $attr, $inv, $value) = @_;
211 if ($attr->has_initializer) {
212 return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');');
215 return $self->_inline_store($inv, $value);
220 my ($self, $instance, $value) = @_;
221 my $attr = $self->associated_attribute;
223 my $mi = $attr->associated_class->get_meta_instance;
225 my $code = $mi->inline_set_slot_value($instance, $attr->slots, $value) . ";";
226 $code .= $mi->inline_weaken_slot_value($instance, $attr->slots, $value) . ";"
227 if $attr->is_weak_ref;
231 sub _inline_get_old_value_for_trigger {
232 my ( $self, $instance ) = @_;
234 my $attr = $self->associated_attribute;
235 return '' unless $attr->has_trigger;
237 my $mi = $attr->associated_class->get_meta_instance;
238 my $pred = $mi->inline_is_slot_initialized($instance, $attr->name);
243 . $self->_inline_get($instance) . q{ : ()} . ";\n";
246 sub _inline_trigger {
247 my ($self, $instance, $value, $old_value) = @_;
248 my $attr = $self->associated_attribute;
249 return '' unless $attr->has_trigger;
250 return sprintf('$attr->trigger->(%s, %s, %s);', $instance, $value, $old_value);
254 my ($self, $instance) = @_;
255 my $attr = $self->associated_attribute;
257 my $mi = $attr->associated_class->get_meta_instance;
259 return $mi->inline_get_slot_value($instance, $attr->slots);
263 my ($self, $instance) = @_;
264 my $attr = $self->associated_attribute;
266 my $mi = $attr->associated_class->get_meta_instance;
268 return $mi->inline_slot_access($instance, $attr->slots);
272 my ($self, $instance) = @_;
273 my $attr = $self->associated_attribute;
275 my $mi = $attr->associated_class->get_meta_instance;
277 return $mi->inline_is_slot_initialized($instance, $attr->slots);
280 sub _inline_auto_deref {
281 my ( $self, $ref_value ) = @_;
282 my $attr = $self->associated_attribute;
284 return $ref_value unless $attr->should_auto_deref;
286 my $type_constraint = $attr->type_constraint;
289 if ($type_constraint->is_a_type_of('ArrayRef')) {
292 elsif ($type_constraint->is_a_type_of('HashRef')) {
296 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", type_constraint => $type_constraint );
299 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
310 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
314 This class is a subclass of L<Class::MOP::Method::Accessor> that
315 provides additional Moose-specific functionality, all of which is
318 To understand this class, you should read the the
319 L<Class::MOP::Method::Accessor> documentation.
323 All complex software has bugs lurking in it, and this module is no
324 exception. If you find a bug please either email me, or add the bug
329 Stevan Little E<lt>stevan@iinteractive.comE<gt>
331 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
333 =head1 COPYRIGHT AND LICENSE
335 Copyright 2006-2009 by Infinity Interactive, Inc.
337 L<http://www.iinteractive.com>
339 This library is free software; you can redistribute it and/or modify
340 it under the same terms as Perl itself.