Use dzil Authority plugin - remove $AUTHORITY from code
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Writer.pm
CommitLineData
5df54980 1package Moose::Meta::Method::Accessor::Native::Writer;
2
3use strict;
4use warnings;
5
a6ae7438 6use List::MoreUtils qw( any );
7
8b9641b8 8use Moose::Role;
9
10with 'Moose::Meta::Method::Accessor::Native';
11
12requires '_potential_value';
5df54980 13
14sub _generate_method {
15 my $self = shift;
16
53a4677c 17 my $inv = '$self';
1e2c801e 18 my $slot_access = $self->_get_value($inv);
5df54980 19
53a4677c 20 return (
21 'sub {',
53a4677c 22 'my ' . $inv . ' = shift;',
23 $self->_inline_curried_arguments,
1e2c801e 24 $self->_inline_writer_core($inv, $slot_access),
53a4677c 25 '}',
26 );
e7724627 27}
28
1e2c801e 29sub _inline_writer_core {
53a4677c 30 my $self = shift;
31 my ($inv, $slot_access) = @_;
e7724627 32
53a4677c 33 my $potential = $self->_potential_value($slot_access);
34 my $old = '@old';
5df54980 35
53a4677c 36 my @code;
37 push @code, (
38 $self->_inline_check_argument_count,
39 $self->_inline_process_arguments($inv, $slot_access),
40 $self->_inline_check_arguments('for writer'),
ec86bdff 41 $self->_inline_check_lazy($inv, '$type_constraint', '$type_constraint_obj'),
53a4677c 42 );
5df54980 43
53a4677c 44 if ($self->_return_value($slot_access)) {
7f5ec80d 45 # some writers will save the return value in this variable when they
46 # generate the potential value.
53a4677c 47 push @code, 'my @return;'
7f5ec80d 48 }
49
53a4677c 50 push @code, (
51 $self->_inline_coerce_new_values,
52 $self->_inline_copy_native_value(\$potential),
ec86bdff 53 $self->_inline_tc_code($potential, '$type_constraint', '$type_constraint_obj'),
53a4677c 54 $self->_inline_get_old_value_for_trigger($inv, $old),
55 $self->_inline_capture_return_value($slot_access),
56 $self->_inline_set_new_value($inv, $potential, $slot_access),
57 $self->_inline_trigger($inv, $slot_access, $old),
58 $self->_inline_return_value($slot_access, 'for writer'),
59 );
60
61 return @code;
5df54980 62}
63
53a4677c 64sub _inline_process_arguments { return }
5df54980 65
53a4677c 66sub _inline_check_arguments { return }
5df54980 67
53a4677c 68sub _inline_coerce_new_values { return }
7bf5e58d 69
6e50f7e9 70sub _writer_value_needs_copy {
c302c35a 71 my $self = shift;
72
73 return $self->_constraint_must_be_checked;
74}
5df54980 75
a6ae7438 76sub _constraint_must_be_checked {
77 my $self = shift;
78
79 my $attr = $self->associated_attribute;
80
81 return $attr->has_type_constraint
1e2c801e 82 && (!$self->_is_root_type( $attr->type_constraint )
83 || ( $attr->should_coerce && $attr->type_constraint->has_coercion)
84 );
a6ae7438 85}
86
87sub _is_root_type {
53a4677c 88 my $self = shift;
89 my ($type) = @_;
a6ae7438 90
53a4677c 91 my $name = $type->name;
a6ae7438 92
93 return any { $name eq $_ } @{ $self->root_types };
94}
95
6ff86bed 96sub _inline_copy_native_value {
53a4677c 97 my $self = shift;
98 my ($potential_ref) = @_;
fa072458 99
6e50f7e9 100 return unless $self->_writer_value_needs_copy;
fa072458 101
53a4677c 102 my $code = 'my $potential = ' . ${$potential_ref} . ';';
fa072458 103
104 ${$potential_ref} = '$potential';
105
1e2c801e 106 return $code;
fa072458 107}
108
53a4677c 109around _inline_tc_code => sub {
110 my $orig = shift;
111 my $self = shift;
ec86bdff 112 my ($value, $tc, $tc_obj, $for_lazy) = @_;
8044d617 113
53a4677c 114 return unless $for_lazy || $self->_constraint_must_be_checked;
8044d617 115
53a4677c 116 return $self->$orig(@_);
117};
5df54980 118
e7724627 119sub _inline_check_coercion {
53a4677c 120 my $self = shift;
ec86bdff 121 my ($value, $tc, $tc_obj) = @_;
a6ae7438 122
123 my $attr = $self->associated_attribute;
53a4677c 124 return unless $attr->should_coerce && $attr->type_constraint->has_coercion;
a6ae7438 125
126 # We want to break the aliasing in @_ in case the coercion tries to make a
127 # destructive change to an array member.
ec86bdff 128 return $value . ' = ' . $tc_obj . '->coerce(' . $value . ');';
e7724627 129}
5df54980 130
53a4677c 131around _inline_check_constraint => sub {
132 my $orig = shift;
133 my $self = shift;
ec86bdff 134 my ($value, $tc, $tc_obj, $for_lazy) = @_;
5df54980 135
53a4677c 136 return unless $for_lazy || $self->_constraint_must_be_checked;
5df54980 137
53a4677c 138 return $self->$orig(@_);
8b9641b8 139};
5df54980 140
53a4677c 141sub _inline_capture_return_value { return }
5df54980 142
a486d5ad 143sub _inline_set_new_value {
5df54980 144 my $self = shift;
145
a486d5ad 146 return $self->_inline_store_value(@_)
6e50f7e9 147 if $self->_writer_value_needs_copy
f878e6cf 148 || !$self->_slot_access_can_be_inlined
1e2c801e 149 || !$self->_get_is_lvalue;
e32b7489 150
a486d5ad 151 return $self->_inline_optimized_set_new_value(@_);
f878e6cf 152}
153
1e2c801e 154sub _get_is_lvalue {
f878e6cf 155 my $self = shift;
156
157 return $self->associated_attribute->associated_class->instance_metaclass->inline_get_is_lvalue;
158}
e32b7489 159
a486d5ad 160sub _inline_optimized_set_new_value {
e32b7489 161 my $self = shift;
162
a486d5ad 163 return $self->_inline_store_value(@_);
5df54980 164}
165
7f5ec80d 166sub _return_value {
53a4677c 167 my $self = shift;
168 my ($slot_access) = @_;
7f5ec80d 169
170 return $slot_access;
171}
5df54980 172
8b9641b8 173no Moose::Role;
174
5df54980 1751;