1 package Moose::Meta::Method::Accessor::Native::Writer;
6 use List::MoreUtils qw( any );
9 $VERSION = eval $VERSION;
10 our $AUTHORITY = 'cpan:STEVAN';
14 with 'Moose::Meta::Method::Accessor::Native';
16 requires '_potential_value';
18 sub _generate_method {
23 my $slot_access = $self->_inline_get($inv);
27 $code .= "\n" . $self->_inline_pre_body(@_);
29 $code .= "\n" . 'my $self = shift;';
31 $code .= "\n" . $self->_inline_curried_arguments;
33 $code .= $self->_writer_core( $inv, $slot_access );
35 $code .= "\n" . $self->_inline_post_body(@_);
43 my ( $self, $inv, $slot_access ) = @_;
47 $code .= "\n" . $self->_inline_check_argument_count;
48 $code .= "\n" . $self->_inline_process_arguments( $inv, $slot_access );
49 $code .= "\n" . $self->_inline_check_arguments('for writer');
51 $code .= "\n" . $self->_inline_check_lazy($inv);
53 my $potential_value = $self->_potential_value($slot_access);
55 if ( $self->_return_value($slot_access) ) {
56 # some writers will save the return value in this variable when they
57 # generate the potential value.
58 $code .= "\n" . 'my @return;';
61 # This is only needed by collections.
62 $code .= "\n" . $self->_inline_coerce_new_values;
63 $code .= "\n" . $self->_inline_copy_native_value( \$potential_value );
65 . $self->_inline_tc_code(
69 $code .= "\n" . $self->_inline_get_old_value_for_trigger($inv);
70 $code .= "\n" . $self->_inline_capture_return_value($slot_access);
72 . $self->_inline_set_new_value(
77 $code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' );
78 $code .= "\n" . $self->_return_value( $slot_access, 'for writer' );
83 sub _inline_process_arguments {q{}}
85 sub _inline_check_arguments {q{}}
87 sub _inline_coerce_new_values {q{}}
89 sub _value_needs_copy {
92 return $self->_constraint_must_be_checked;
95 sub _constraint_must_be_checked {
98 my $attr = $self->associated_attribute;
100 return $attr->has_type_constraint
101 && ( !$self->_is_root_type( $attr->type_constraint )
102 || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) );
106 my ($self, $type) = @_;
108 my $name = $type->name();
110 return any { $name eq $_ } @{ $self->root_types };
113 sub _inline_copy_native_value {
114 my ( $self, $potential_ref ) = @_;
116 return q{} unless $self->_value_needs_copy;
118 my $code = "my \$potential = ${$potential_ref};";
120 ${$potential_ref} = '$potential';
125 sub _inline_tc_code {
126 my ( $self, $potential_value ) = @_;
128 return q{} unless $self->_constraint_must_be_checked;
130 return $self->_inline_check_coercion($potential_value) . "\n"
131 . $self->_inline_check_constraint($potential_value);
134 sub _inline_check_coercion {
135 my ( $self, $value ) = @_;
137 my $attr = $self->associated_attribute;
140 unless $attr->should_coerce
141 && $attr->type_constraint->has_coercion;
143 # We want to break the aliasing in @_ in case the coercion tries to make a
144 # destructive change to an array member.
145 return "$value = \$type_constraint_obj->coerce($value);";
148 override _inline_check_constraint => sub {
149 my ( $self, $value, $for_lazy ) = @_;
151 return q{} unless $for_lazy || $self->_constraint_must_be_checked;
156 sub _inline_capture_return_value { return q{} }
158 sub _inline_set_new_value {
161 return $self->_inline_store(@_)
162 if $self->_value_needs_copy
163 || !$self->_slot_access_can_be_inlined
164 || !$self->_inline_get_is_lvalue;
166 return $self->_inline_optimized_set_new_value(@_);
169 sub _inline_get_is_lvalue {
172 return $self->associated_attribute->associated_class->instance_metaclass->inline_get_is_lvalue;
175 sub _inline_optimized_set_new_value {
178 return $self->_inline_store(@_);
182 my ( $self, $slot_access ) = @_;