1 package Moose::Meta::Method::Accessor::Native::Writer;
6 use List::MoreUtils qw( any );
9 $VERSION = eval $VERSION;
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Moose::Meta::Method::Accessor::Native';
14 sub _generate_method {
19 my $slot_access = $self->_inline_get($inv);
23 $code .= "\n" . $self->_inline_pre_body(@_);
25 $code .= "\n" . 'my $self = shift;';
27 $code .= "\n" . $self->_inline_curried_arguments;
29 $code .= $self->_writer_core( $inv, $slot_access );
31 $code .= "\n" . $self->_inline_post_body(@_);
39 my ( $self, $inv, $slot_access ) = @_;
43 $code .= "\n" . $self->_inline_check_argument_count;
44 $code .= "\n" . $self->_inline_process_arguments( $inv, $slot_access );
45 $code .= "\n" . $self->_inline_check_arguments('for writer');
47 $code .= "\n" . $self->_inline_check_lazy($inv);
49 my $potential_value = $self->_potential_value($slot_access);
51 $code .= "\n" . $self->_inline_copy_native_value( \$potential_value );
53 . $self->_inline_tc_code(
57 $code .= "\n" . $self->_inline_get_old_value_for_trigger($inv);
58 $code .= "\n" . $self->_inline_capture_return_value($slot_access);
60 . $self->_inline_set_new_value(
65 $code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' );
66 $code .= "\n" . $self->_return_value( $slot_access, 'for writer' );
71 sub _inline_process_arguments {q{}}
73 sub _inline_check_arguments {q{}}
75 sub _value_needs_copy {
78 return $self->_constraint_must_be_checked;
81 sub _constraint_must_be_checked {
84 my $attr = $self->associated_attribute;
86 return $attr->has_type_constraint
87 && ( !$self->_is_root_type( $attr->type_constraint )
88 || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) );
92 my ($self, $type) = @_;
94 my $name = $type->name();
96 return any { $name eq $_ } @{ $self->root_types };
99 sub _inline_copy_native_value {
100 my ( $self, $potential_ref ) = @_;
102 return q{} unless $self->_value_needs_copy;
104 my $code = "my \$potential = ${$potential_ref};";
106 ${$potential_ref} = '$potential';
111 sub _inline_tc_code {
112 my ( $self, $potential_value ) = @_;
114 return q{} unless $self->_constraint_must_be_checked;
116 return $self->_inline_check_coercion($potential_value) . "\n"
117 . $self->_inline_check_constraint($potential_value);
120 sub _inline_check_coercion {
121 my ( $self, $value ) = @_;
123 my $attr = $self->associated_attribute;
126 unless $attr->should_coerce && $attr->type_constraint->has_coercion;
128 # We want to break the aliasing in @_ in case the coercion tries to make a
129 # destructive change to an array member.
130 return "$value = \$type_constraint_obj->coerce($value);";
133 sub _inline_check_constraint {
136 return q{} unless $self->_constraint_must_be_checked;
138 return $self->SUPER::_inline_check_constraint( $_[0] );
141 sub _inline_capture_return_value { return q{} }
143 sub _inline_set_new_value {
146 return $self->SUPER::_inline_store(@_)
147 if $self->_value_needs_copy;
149 return $self->_inline_optimized_set_new_value(@_);
152 sub _inline_optimized_set_new_value {
155 return $self->SUPER::_inline_store(@_)
158 sub _return_value { return q{} }