1 package Moose::Meta::Method::Accessor::Native::Collection;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
10 sub _value_needs_copy {
14 return $self->_constraint_must_be_checked
15 && !$self->_check_new_members_only;
20 my ( $self, $potential_value ) = @_;
22 return q{} unless $self->_constraint_must_be_checked;
24 if ( $self->_check_new_members_only ) {
25 return q{} unless $self->_adds_members;
27 return $self->_inline_check_member_constraint( $self->_new_members );
30 return $self->_inline_check_coercion($potential_value) . "\n"
31 . $self->_inline_check_constraint($potential_value);
35 sub _check_new_members_only {
38 my $attr = $self->associated_attribute;
40 my $tc = $attr->type_constraint;
42 # If we have a coercion, we could come up with an entirely new value after
43 # coercing, so we need to check everything,
44 return 0 if $attr->should_coerce && $tc->has_coercion;
46 # If the parent is our root type (ArrayRef, HashRef, etc), that means we
47 # can just check the new members of the collection, because we know that
48 # we will always be generating an appropriate collection type.
50 # However, if this type has its own constraint (it's Parameteriz_able_,
51 # not Paramet_erized_), we don't know what is being checked by the
52 # constraint, so we need to check the whole value, not just the members.
54 if $self->_is_root_type( $tc->parent )
55 && $tc->isa('Moose::Meta::TypeConstraint::Parameterized');
60 sub _inline_check_member_constraint {
61 my ( $self, $new_value ) = @_;
63 my $attr_name = $self->associated_attribute->name;
65 return '$member_tc->($_) || '
66 . $self->_inline_throw_error(
67 qq{"A new member value for '$attr_name' does not pass its type constraint because: "}
68 . ' . $member_tc->get_message($_)',
70 ) . " for $new_value;";
73 sub _inline_check_constraint {
77 return q{} unless $self->_constraint_must_be_checked;
79 return $self->$orig( $_[0] );
82 sub _inline_get_old_value_for_trigger {
84 my ( $self, $instance ) = @_;
86 my $attr = $self->associated_attribute;
87 return '' unless $attr->has_trigger;
89 my $mi = $attr->associated_class->get_meta_instance;
90 my $pred = $mi->inline_is_slot_initialized( $instance, $attr->name );
95 . $self->_inline_copy_old_value( $self->_inline_get($instance) )
99 sub _eval_environment {
103 my $env = $self->$orig(@_);
106 unless $self->_constraint_must_be_checked
107 && $self->_check_new_members_only;
110 = \( $self->associated_attribute->type_constraint->type_parameter
111 ->_compiled_type_constraint );