3280cf1a172185f97e6ab72bf2a069d3e7d49a15
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Array / Writer.pm
1 package Moose::Meta::Method::Accessor::Native::Array::Writer;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '1.13';
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 use base 'Moose::Meta::Method::Accessor::Native::Array';
11
12 sub _generate_method {
13     my $self = shift;
14
15     my $inv = '$self';
16
17     my $slot_access = $self->_inline_get($inv);
18
19     my $code = 'sub {';
20     $code .= "\n" . $self->_inline_pre_body(@_);
21
22     $code .= "\n" . 'my $self = shift;';
23
24     $code .= "\n" . $self->_inline_check_lazy($inv);
25
26     $code .= "\n" . $self->_inline_curried_arguments;
27
28     $code .= "\n" . $self->_inline_check_argument_count;
29
30     $code .= "\n" . $self->_inline_process_arguments;
31
32     $code .= "\n" . $self->_inline_check_arguments;
33
34     my $new_values      = $self->_new_values($slot_access);
35     my $potential_value = $self->_potential_value($slot_access);
36
37     $code .= "\n"
38         . $self->_inline_tc_code(
39         $new_values,
40         $potential_value
41         );
42
43     $code .= "\n" . $self->_inline_get_old_value_for_trigger($inv);
44     $code .= "\n" . $self->_capture_old_value($slot_access);
45
46     $code .= "\n" . $self->_inline_store( $inv, '[' . $potential_value . ']' );
47
48     $code .= "\n" . $self->_inline_post_body(@_);
49     $code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' );
50
51     $code .= "\n" . $self->_return_value( $inv, '@old' );
52
53     $code .= "\n}";
54
55     return $code;
56 }
57
58 sub _inline_process_arguments { q{} }
59
60 sub _inline_check_arguments { q{} }
61
62 sub _new_values { '@_' }
63
64 sub _inline_tc_code {
65     my ( $self, $new_value, $potential_value ) = @_;
66
67     return q{} unless $self->_constraint_must_be_checked;
68
69     if ( $self->_check_new_members_only ) {
70         return q{} unless $self->_adds_members;
71
72         return $self->_inline_check_member_constraint($new_value);
73     }
74     else {
75         return $self->_inline_check_coercion($potential_value) . "\n"
76             . $self->_inline_check_constraint($potential_value);
77     }
78 }
79
80 sub _constraint_must_be_checked {
81     my $self = shift;
82
83     my $attr = $self->associated_attribute;
84
85     return $attr->has_type_constraint
86         && ( $attr->type_constraint->name ne 'ArrayRef'
87         || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) );
88 }
89
90 sub _check_new_members_only {
91     my $self = shift;
92
93     my $attr = $self->associated_attribute;
94
95     my $tc = $attr->type_constraint;
96
97     # If we have a coercion, we could come up with an entirely new value after
98     # coercing, so we need to check everything,
99     return 0 if $attr->should_coerce && $tc->has_coercion;
100
101     # If the parent is ArrayRef, that means we can just check the new members
102     # of the collection, because we know that we will always be generating an
103     # ArrayRef.
104     return 1
105         if $tc->parent->name eq 'ArrayRef'
106             && $tc->isa('Moose::Meta::TypeConstraint::Parameterized');
107
108     # If our parent is something else ( subtype 'Foo' as 'ArrayRef[Str]' )
109     # then there may be additional constraints on the whole value, as opposed
110     # to constraints just on the members.
111     return 0;
112 }
113
114 sub _inline_check_member_constraint {
115     my ( $self, $new_value ) = @_;
116
117     my $attr_name = $self->associated_attribute->name;
118
119     return '$member_tc->($_) || '
120         . $self->_inline_throw_error(
121         qq{"A new member value for '$attr_name' does not pass its type constraint because: "}
122             . ' . $member_tc->get_message($_)',
123         "data => \$_"
124         ) . " for $new_value;";
125 }
126
127 sub _inline_check_coercion {
128     my ( $self, $value ) = @_;
129
130     my $attr = $self->associated_attribute;
131
132     return ''
133         unless $attr->should_coerce && $attr->type_constraint->has_coercion;
134
135     # We want to break the aliasing in @_ in case the coercion tries to make a
136     # destructive change to an array member.
137     my $code = 'my @copy = @{ $value }';
138     return '@_ = @{ $attr->type_constraint->coerce(\@copy) };';
139 }
140
141 sub _inline_check_constraint {
142     my $self = shift;
143
144     return q{} unless $self->_constraint_must_be_checked;
145
146     return $self->SUPER::_inline_check_constraint(@_);
147 }
148
149 sub _capture_old_value { return q{} }
150 sub _return_value { return q{} }
151
152 sub _eval_environment {
153     my $self = shift;
154
155     my $env = $self->SUPER::_eval_environment;
156
157     return $env
158         unless $self->_constraint_must_be_checked
159             and $self->_check_new_members_only;
160
161     $env->{'$member_tc'}
162         = \( $self->associated_attribute->type_constraint->type_parameter
163             ->_compiled_type_constraint );
164
165     return $env;
166 }
167
168 1;