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;
29 '$type_constraint_obj' => \$type_constraint_obj,
30 '$type_constraint' => \($type_constraint_obj
31 ? $type_constraint_obj->_compiled_type_constraint
35 #warn "code for " . $attr->name . " =>\n" . $source . "\n";
36 my ( $code, $e ) = $self->_compile_code( environment => $environment, code => $source );
39 "Could not create writer for '${\$self->associated_attribute->name}' because $e \n code: $source",
40 error => $e, data => $source )
46 sub _generate_accessor_method_inline {
48 my $attr = $self->associated_attribute;
50 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
52 $self->_eval_code('sub { ' . "\n"
53 . $self->_inline_pre_body(@_) . "\n"
54 . 'if (scalar(@_) >= 2) {' . "\n"
55 . $self->_inline_copy_value . "\n"
56 . $self->_inline_check_required . "\n"
57 . $self->_inline_check_coercion($value_name) . "\n"
58 . $self->_inline_check_constraint($value_name) . "\n"
59 . $self->_inline_get_old_value_for_trigger($inv, $value_name) . "\n"
60 . $self->_inline_store($inv, $value_name) . "\n"
61 . $self->_inline_trigger($inv, $value_name, '@old') . "\n"
63 . $self->_inline_check_lazy($inv) . "\n"
64 . $self->_inline_post_body(@_) . "\n"
65 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n"
69 sub _generate_writer_method_inline {
71 my $attr = $self->associated_attribute;
73 my $slot_access = $self->_inline_get($inv);
74 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
76 $self->_eval_code('sub { '
77 . $self->_inline_pre_body(@_)
78 . $self->_inline_copy_value
79 . $self->_inline_check_required
80 . $self->_inline_check_coercion($value_name)
81 . $self->_inline_check_constraint($value_name)
82 . $self->_inline_get_old_value_for_trigger($inv, $value_name) . "\n"
83 . $self->_inline_store($inv, $value_name)
84 . $self->_inline_post_body(@_)
85 . $self->_inline_trigger($inv, $value_name, '@old')
89 sub _generate_reader_method_inline {
91 my $attr = $self->associated_attribute;
93 my $slot_access = $self->_inline_get($inv);
95 $self->_eval_code('sub {'
96 . $self->_inline_pre_body(@_)
97 . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
98 . $self->_inline_check_lazy($inv)
99 . $self->_inline_post_body(@_)
100 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
104 sub _inline_copy_value {
105 return '' unless shift->_value_needs_copy;
106 return 'my $val = $_[1];'
109 sub _value_needs_copy {
110 my $attr = (shift)->associated_attribute;
111 return $attr->should_coerce;
114 sub _generate_reader_method { shift->_generate_reader_method_inline(@_) }
115 sub _generate_writer_method { shift->_generate_writer_method_inline(@_) }
116 sub _generate_accessor_method { shift->_generate_accessor_method_inline(@_) }
117 sub _generate_predicate_method { shift->_generate_predicate_method_inline(@_) }
118 sub _generate_clearer_method { shift->_generate_clearer_method_inline(@_) }
120 sub _inline_pre_body { '' }
121 sub _inline_post_body { '' }
123 sub _inline_check_constraint {
124 my ($self, $value) = @_;
126 my $attr = $self->associated_attribute;
128 return '' unless $attr->has_type_constraint;
130 my $attr_name = quotemeta( $attr->name );
132 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") . ";";
135 sub _inline_check_coercion {
136 my ($self, $value) = @_;
138 my $attr = $self->associated_attribute;
140 return '' unless $attr->should_coerce;
141 return "$value = \$attr->type_constraint->coerce($value);";
144 sub _inline_check_required {
146 my $attr = $self->associated_attribute;
148 return '' unless $attr->is_required;
150 my $attr_name = quotemeta( $attr->name );
152 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
155 sub _inline_check_lazy {
156 my ($self, $instance) = @_;
158 my $attr = $self->associated_attribute;
160 return '' unless $attr->is_lazy;
162 my $slot_exists = $self->_inline_has($instance);
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, '$default') . "\n";
182 $code .= ' ' . $self->_inline_init_slot($attr, $instance, 'undef') . "\n";
186 if ($attr->has_default) {
187 $code .= ' ' . $self->_inline_init_slot($attr, $instance, ('$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, ($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, 'undef') . "\n";
204 sub _inline_init_slot {
205 my ($self, $attr, $inv, $value) = @_;
206 if ($attr->has_initializer) {
207 return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');');
210 return $self->_inline_store($inv, $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_get_old_value_for_trigger {
227 my ( $self, $instance ) = @_;
229 my $attr = $self->associated_attribute;
230 return '' unless $attr->has_trigger;
232 my $mi = $attr->associated_class->get_meta_instance;
233 my $pred = $mi->inline_is_slot_initialized($instance, $attr->name);
238 . $self->_inline_get($instance) . q{ : ()} . ";\n";
241 sub _inline_trigger {
242 my ($self, $instance, $value, $old_value) = @_;
243 my $attr = $self->associated_attribute;
244 return '' unless $attr->has_trigger;
245 return sprintf('$attr->trigger->(%s, %s, %s);', $instance, $value, $old_value);
249 my ($self, $instance) = @_;
250 my $attr = $self->associated_attribute;
252 my $mi = $attr->associated_class->get_meta_instance;
254 return $mi->inline_get_slot_value($instance, $attr->slots);
258 my ($self, $instance) = @_;
259 my $attr = $self->associated_attribute;
261 my $mi = $attr->associated_class->get_meta_instance;
263 return $mi->inline_slot_access($instance, $attr->slots);
267 my ($self, $instance) = @_;
268 my $attr = $self->associated_attribute;
270 my $mi = $attr->associated_class->get_meta_instance;
272 return $mi->inline_is_slot_initialized($instance, $attr->slots);
275 sub _inline_auto_deref {
276 my ( $self, $ref_value ) = @_;
277 my $attr = $self->associated_attribute;
279 return $ref_value unless $attr->should_auto_deref;
281 my $type_constraint = $attr->type_constraint;
284 if ($type_constraint->is_a_type_of('ArrayRef')) {
287 elsif ($type_constraint->is_a_type_of('HashRef')) {
291 $self->throw_error( "Can not auto de-reference the type constraint '"
292 . quotemeta( $type_constraint->name )
293 . "'", type_constraint => $type_constraint );
296 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
307 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
311 This class is a subclass of L<Class::MOP::Method::Accessor> that
312 provides additional Moose-specific functionality, all of which is
315 To understand this class, you should read the the
316 L<Class::MOP::Method::Accessor> documentation.
320 See L<Moose/BUGS> for details on reporting bugs.
324 Stevan Little E<lt>stevan@iinteractive.comE<gt>
326 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
328 =head1 COPYRIGHT AND LICENSE
330 Copyright 2006-2010 by Infinity Interactive, Inc.
332 L<http://www.iinteractive.com>
334 This library is free software; you can redistribute it and/or modify
335 it under the same terms as Perl itself.