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 $slot_access = $self->_inline_access($inv, $attr_name);
54 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
56 $self->_eval_code('sub { ' . "\n"
57 . $self->_inline_pre_body(@_) . "\n"
58 . 'if (scalar(@_) >= 2) {' . "\n"
59 . $self->_inline_copy_value . "\n"
60 . $self->_inline_check_required . "\n"
61 . $self->_inline_check_coercion($value_name) . "\n"
62 . $self->_inline_check_constraint($value_name) . "\n"
63 . $self->_inline_store($inv, $value_name) . "\n"
64 . $self->_inline_trigger($inv, $value_name) . "\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_store($inv, $value_name)
87 . $self->_inline_post_body(@_)
88 . $self->_inline_trigger($inv, $value_name)
92 sub _generate_reader_method_inline {
94 my $attr = $self->associated_attribute;
95 my $attr_name = $attr->name;
97 my $slot_access = $self->_inline_get($inv, $attr_name);
99 $self->_eval_code('sub {'
100 . $self->_inline_pre_body(@_)
101 . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
102 . $self->_inline_check_lazy($inv)
103 . $self->_inline_post_body(@_)
104 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
108 sub _inline_copy_value {
109 return '' unless shift->_value_needs_copy;
110 return 'my $val = $_[1];'
113 sub _value_needs_copy {
114 my $attr = (shift)->associated_attribute;
115 return $attr->should_coerce;
118 sub _generate_reader_method { shift->_generate_reader_method_inline(@_) }
119 sub _generate_writer_method { shift->_generate_writer_method_inline(@_) }
120 sub _generate_accessor_method { shift->_generate_accessor_method_inline(@_) }
121 sub _generate_predicate_method { shift->_generate_predicate_method_inline(@_) }
122 sub _generate_clearer_method { shift->_generate_clearer_method_inline(@_) }
124 sub _inline_pre_body { '' }
125 sub _inline_post_body { '' }
127 sub _inline_check_constraint {
128 my ($self, $value) = @_;
130 my $attr = $self->associated_attribute;
131 my $attr_name = $attr->name;
133 return '' unless $attr->has_type_constraint;
135 my $type_constraint_name = $attr->type_constraint->name;
137 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") . ";";
140 sub _inline_check_coercion {
141 my ($self, $value) = @_;
143 my $attr = $self->associated_attribute;
145 return '' unless $attr->should_coerce;
146 return "$value = \$attr->type_constraint->coerce($value);";
149 sub _inline_check_required {
151 my $attr = $self->associated_attribute;
153 my $attr_name = $attr->name;
155 return '' unless $attr->is_required;
156 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
159 sub _inline_check_lazy {
160 my ($self, $instance) = @_;
162 my $attr = $self->associated_attribute;
164 return '' unless $attr->is_lazy;
166 my $slot_access = $self->_inline_access($instance, $attr->name);
168 my $slot_exists = $self->_inline_has($instance, $attr->name);
170 my $code = 'unless (' . $slot_exists . ') {' . "\n";
171 if ($attr->has_type_constraint) {
172 if ($attr->has_default || $attr->has_builder) {
173 if ($attr->has_default) {
174 $code .= ' my $default = $attr->default(' . $instance . ');'."\n";
176 elsif ($attr->has_builder) {
177 $code .= ' my $default;'."\n".
178 ' if(my $builder = '.$instance.'->can($attr->builder)){ '."\n".
179 ' $default = '.$instance.'->$builder; '. "\n } else {\n" .
180 ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name') .
183 $code .= $self->_inline_check_coercion('$default') . "\n";
184 $code .= $self->_inline_check_constraint('$default') . "\n";
185 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, '$default') . "\n";
188 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
192 if ($attr->has_default) {
193 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ('$attr->default(' . $instance . ')')) . "\n";
195 elsif ($attr->has_builder) {
196 $code .= ' if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n"
197 . ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ($instance . '->$builder'))
199 . ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name')
203 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
210 sub _inline_init_slot {
211 my ($self, $attr, $inv, $slot_access, $value) = @_;
212 if ($attr->has_initializer) {
213 return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');');
216 return ($slot_access . ' = ' . $value . ';');
221 my ($self, $instance, $value) = @_;
222 my $attr = $self->associated_attribute;
224 my $mi = $attr->associated_class->get_meta_instance;
226 my $code = $mi->inline_set_slot_value($instance, $attr->slots, $value) . ";";
227 $code .= $mi->inline_weaken_slot_value($instance, $attr->slots, $value) . ";"
228 if $attr->is_weak_ref;
232 sub _inline_trigger {
233 my ($self, $instance, $value) = @_;
234 my $attr = $self->associated_attribute;
235 return '' unless $attr->has_trigger;
236 return sprintf('$attr->trigger->(%s, %s);', $instance, $value);
240 my ($self, $instance) = @_;
241 my $attr = $self->associated_attribute;
243 my $mi = $attr->associated_class->get_meta_instance;
245 return $mi->inline_get_slot_value($instance, $attr->slots);
249 my ($self, $instance) = @_;
250 my $attr = $self->associated_attribute;
252 my $mi = $attr->associated_class->get_meta_instance;
254 return $mi->inline_slot_access($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_is_slot_initialized($instance, $attr->slots);
266 sub _inline_auto_deref {
267 my ( $self, $ref_value ) = @_;
268 my $attr = $self->associated_attribute;
270 return $ref_value unless $attr->should_auto_deref;
272 my $type_constraint = $attr->type_constraint;
275 if ($type_constraint->is_a_type_of('ArrayRef')) {
278 elsif ($type_constraint->is_a_type_of('HashRef')) {
282 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", type_constraint => $type_constraint );
285 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
296 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
300 This class is a subclass of L<Class::MOP::Method::Accessor> that
301 provides additional Moose-specific functionality, all of which is
304 To understand this class, you should read the the
305 L<Class::MOP::Method::Accessor> documentation.
309 All complex software has bugs lurking in it, and this module is no
310 exception. If you find a bug please either email me, or add the bug
315 Stevan Little E<lt>stevan@iinteractive.comE<gt>
317 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
319 =head1 COPYRIGHT AND LICENSE
321 Copyright 2006-2009 by Infinity Interactive, Inc.
323 L<http://www.iinteractive.com>
325 This library is free software; you can redistribute it and/or modify
326 it under the same terms as Perl itself.