1 package Moose::Meta::Method::Accessor::Native::Collection;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
12 requires qw( _adds_members );
14 around _value_needs_copy => sub {
18 return $self->_constraint_must_be_checked
19 && !$self->_check_new_members_only;
22 around _inline_tc_code => sub {
24 my ( $self, $potential_value ) = @_;
26 return q{} unless $self->_constraint_must_be_checked;
28 if ( $self->_check_new_members_only ) {
29 return q{} unless $self->_adds_members;
31 return $self->_inline_check_member_constraint( $self->_new_members );
34 return $self->_inline_check_coercion($potential_value) . "\n"
35 . $self->_inline_check_constraint($potential_value);
39 sub _check_new_members_only {
42 my $attr = $self->associated_attribute;
44 my $tc = $attr->type_constraint;
46 # If we have a coercion, we could come up with an entirely new value after
47 # coercing, so we need to check everything,
48 return 0 if $attr->should_coerce && $tc->has_coercion;
50 # If the parent is our root type (ArrayRef, HashRef, etc), that means we
51 # can just check the new members of the collection, because we know that
52 # we will always be generating an appropriate collection type.
54 # However, if this type has its own constraint (it's Parameteriz_able_,
55 # not Paramet_erized_), we don't know what is being checked by the
56 # constraint, so we need to check the whole value, not just the members.
58 if $self->_is_root_type( $tc->parent )
59 && $tc->isa('Moose::Meta::TypeConstraint::Parameterized');
64 sub _inline_check_member_constraint {
65 my ( $self, $new_value ) = @_;
67 my $attr_name = $self->associated_attribute->name;
69 return '$member_tc->($_) || '
70 . $self->_inline_throw_error(
71 qq{"A new member value for '$attr_name' does not pass its type constraint because: "}
72 . ' . $member_tc->get_message($_)',
74 ) . " for $new_value;";
77 around _inline_check_constraint => sub {
81 return q{} unless $self->_constraint_must_be_checked;
83 return $self->$orig( $_[0] );
86 around _inline_get_old_value_for_trigger => sub {
88 my ( $self, $instance ) = @_;
90 my $attr = $self->associated_attribute;
91 return '' unless $attr->has_trigger;
95 . $self->_inline_has($instance) . q{ ? }
96 . $self->_inline_copy_old_value( $self->_inline_get($instance) )
100 around _eval_environment => sub {
104 my $env = $self->$orig(@_);
107 unless $self->_constraint_must_be_checked
108 && $self->_check_new_members_only;
111 = \( $self->associated_attribute->type_constraint->type_parameter
112 ->_compiled_type_constraint );