fe22c033061a4144269764eaa5911c35b71122a3
[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 qw(
11     Moose::Meta::Method::Accessor::Native::Array
12     Moose::Meta::Method::Accessor::Native::Writer
13 );
14
15 sub _new_value {'@_'}
16
17 sub _value_needs_copy {
18     my $self = shift;
19
20     return $self->_constraint_must_be_checked
21         && !$self->_check_new_members_only;
22 }
23
24 sub _inline_tc_code {
25     my ( $self, $new_value, $potential_value ) = @_;
26
27     return q{} unless $self->_constraint_must_be_checked;
28
29     if ( $self->_check_new_members_only ) {
30         return q{} unless $self->_adds_members;
31
32         return $self->_inline_check_member_constraint($new_value);
33     }
34     else {
35         return $self->_inline_check_coercion($potential_value) . "\n"
36             . $self->_inline_check_constraint($potential_value);
37     }
38 }
39
40 sub _constraint_must_be_checked {
41     my $self = shift;
42
43     my $attr = $self->associated_attribute;
44
45     return $attr->has_type_constraint
46         && ( $attr->type_constraint->name ne 'ArrayRef'
47         || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) );
48 }
49
50 sub _check_new_members_only {
51     my $self = shift;
52
53     my $attr = $self->associated_attribute;
54
55     my $tc = $attr->type_constraint;
56
57     # If we have a coercion, we could come up with an entirely new value after
58     # coercing, so we need to check everything,
59     return 0 if $attr->should_coerce && $tc->has_coercion;
60
61     # If the parent is ArrayRef, that means we can just check the new members
62     # of the collection, because we know that we will always be generating an
63     # ArrayRef. However, if this type has its own constraint, we don't know
64     # what the constraint checks, so we need to check the whole value, not
65     # just the members.
66     return 1
67         if $tc->parent->name eq 'ArrayRef'
68             && $tc->isa('Moose::Meta::TypeConstraint::Parameterized');
69
70     return 0;
71 }
72
73 sub _inline_check_member_constraint {
74     my ( $self, $new_value ) = @_;
75
76     my $attr_name = $self->associated_attribute->name;
77
78     return '$member_tc->($_) || '
79         . $self->_inline_throw_error(
80         qq{"A new member value for '$attr_name' does not pass its type constraint because: "}
81             . ' . $member_tc->get_message($_)',
82         "data => \$_"
83         ) . " for $new_value;";
84 }
85
86 sub _inline_check_coercion {
87     my ( $self, $value ) = @_;
88
89     my $attr = $self->associated_attribute;
90
91     return ''
92         unless $attr->should_coerce && $attr->type_constraint->has_coercion;
93
94     return "$value = \$type_constraint_obj->coerce($value);";
95 }
96
97 sub _inline_check_constraint {
98     my $self = shift;
99
100     return q{} unless $self->_constraint_must_be_checked;
101
102     return $self->SUPER::_inline_check_constraint( $_[0] );
103 }
104
105 sub _inline_get_old_value_for_trigger {
106     my ( $self, $instance ) = @_;
107
108     my $attr = $self->associated_attribute;
109     return '' unless $attr->has_trigger;
110
111     my $mi = $attr->associated_class->get_meta_instance;
112     my $pred = $mi->inline_is_slot_initialized( $instance, $attr->name );
113
114     return
115           'my @old = '
116         . $pred . q{ ? } . '[ @{'
117         . $self->_inline_get($instance)
118         . '} ] : ()' . ";\n";
119 }
120
121 sub _eval_environment {
122     my $self = shift;
123
124     my $env = $self->SUPER::_eval_environment;
125
126     return $env
127         unless $self->_constraint_must_be_checked
128             and $self->_check_new_members_only;
129
130     $env->{'$member_tc'}
131         = \( $self->associated_attribute->type_constraint->type_parameter
132             ->_compiled_type_constraint );
133
134     return $env;
135 }
136
137 1;