1 package Moose::Meta::Method::Accessor::Native::Array::Writer;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
11 Moose::Meta::Method::Accessor::Native::Array
12 Moose::Meta::Method::Accessor::Native::Writer
15 sub _inline_process_arguments {q{}}
17 sub _inline_check_arguments {q{}}
21 sub _inline_copy_value {
22 my ( $self, $potential_ref ) = @_;
24 return q{} unless $self->_value_needs_copy;
26 my $code = "my \$potential = ${$potential_ref};";
28 ${$potential_ref} = '$potential';
33 sub _value_needs_copy {
36 return $self->_constraint_must_be_checked
37 && !$self->_check_new_members_only;
41 my ( $self, $new_value, $potential_value ) = @_;
43 return q{} unless $self->_constraint_must_be_checked;
45 if ( $self->_check_new_members_only ) {
46 return q{} unless $self->_adds_members;
48 return $self->_inline_check_member_constraint($new_value);
51 return $self->_inline_check_coercion($potential_value) . "\n"
52 . $self->_inline_check_constraint($potential_value);
56 sub _constraint_must_be_checked {
59 my $attr = $self->associated_attribute;
61 return $attr->has_type_constraint
62 && ( $attr->type_constraint->name ne 'ArrayRef'
63 || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) );
66 sub _check_new_members_only {
69 my $attr = $self->associated_attribute;
71 my $tc = $attr->type_constraint;
73 # If we have a coercion, we could come up with an entirely new value after
74 # coercing, so we need to check everything,
75 return 0 if $attr->should_coerce && $tc->has_coercion;
77 # If the parent is ArrayRef, that means we can just check the new members
78 # of the collection, because we know that we will always be generating an
79 # ArrayRef. However, if this type has its own constraint, we don't know
80 # what the constraint checks, so we need to check the whole value, not
83 if $tc->parent->name eq 'ArrayRef'
84 && $tc->isa('Moose::Meta::TypeConstraint::Parameterized');
89 sub _inline_check_member_constraint {
90 my ( $self, $new_value ) = @_;
92 my $attr_name = $self->associated_attribute->name;
94 return '$member_tc->($_) || '
95 . $self->_inline_throw_error(
96 qq{"A new member value for '$attr_name' does not pass its type constraint because: "}
97 . ' . $member_tc->get_message($_)',
99 ) . " for $new_value;";
102 sub _inline_check_coercion {
103 my ( $self, $value ) = @_;
105 my $attr = $self->associated_attribute;
108 unless $attr->should_coerce && $attr->type_constraint->has_coercion;
110 return "$value = \$type_constraint_obj->coerce($value);";
113 sub _inline_check_constraint {
116 return q{} unless $self->_constraint_must_be_checked;
118 return $self->SUPER::_inline_check_constraint( $_[0] );
121 sub _inline_get_old_value_for_trigger {
122 my ( $self, $instance ) = @_;
124 my $attr = $self->associated_attribute;
125 return '' unless $attr->has_trigger;
127 my $mi = $attr->associated_class->get_meta_instance;
128 my $pred = $mi->inline_is_slot_initialized( $instance, $attr->name );
132 . $pred . q{ ? } . '[ @{'
133 . $self->_inline_get($instance)
134 . '} ] : ()' . ";\n";
137 sub _return_value { return q{} }
139 sub _eval_environment {
142 my $env = $self->SUPER::_eval_environment;
145 unless $self->_constraint_must_be_checked
146 and $self->_check_new_members_only;
149 = \( $self->associated_attribute->type_constraint->type_parameter
150 ->_compiled_type_constraint );