1 package Moose::Meta::Method::Accessor::Native::Collection;
6 our $VERSION = '1.9900';
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
12 requires qw( _adds_members );
14 sub _inline_coerce_new_values {
17 return unless $self->associated_attribute->should_coerce;
19 return unless $self->_tc_member_type_can_coerce;
22 '(' . $self->_new_members . ') = map { $member_tc_obj->coerce($_) }',
23 $self->_new_members . ';',
27 sub _tc_member_type_can_coerce {
30 my $member_tc = $self->_tc_member_type;
32 return $member_tc && $member_tc->has_coercion;
38 my $tc = $self->associated_attribute->type_constraint;
40 return $tc->type_parameter
41 if $tc->can('type_parameter');
48 sub _writer_value_needs_copy {
51 return $self->_constraint_must_be_checked
52 && !$self->_check_new_members_only;
58 return unless $self->_constraint_must_be_checked;
60 if ($self->_check_new_members_only) {
61 return unless $self->_adds_members;
63 return $self->_inline_check_member_constraint($self->_new_members);
67 $self->_inline_check_coercion(@_),
68 $self->_inline_check_constraint(@_),
73 sub _check_new_members_only {
76 my $attr = $self->associated_attribute;
78 my $tc = $attr->type_constraint;
80 # If we have a coercion, we could come up with an entirely new value after
81 # coercing, so we need to check everything,
82 return 0 if $attr->should_coerce && $tc->has_coercion;
84 # If the parent is our root type (ArrayRef, HashRef, etc), that means we
85 # can just check the new members of the collection, because we know that
86 # we will always be generating an appropriate collection type.
88 # However, if this type has its own constraint (it's Parameteriz_able_,
89 # not Paramet_erized_), we don't know what is being checked by the
90 # constraint, so we need to check the whole value, not just the members.
92 if $self->_is_root_type( $tc->parent )
93 && $tc->isa('Moose::Meta::TypeConstraint::Parameterized');
98 sub _inline_check_member_constraint {
100 my ($new_value) = @_;
102 my $attr_name = $self->associated_attribute->name;
105 'for (' . $new_value . ') {',
106 'if (!$member_tc->($_)) {',
107 $self->_inline_throw_error(
108 '"A new member value for ' . $attr_name
109 . ' does not pass its type constraint because: "'
110 . ' . $member_tc_obj->get_message($_)',
118 sub _inline_get_old_value_for_trigger {
120 my ($instance, $old) = @_;
122 my $attr = $self->associated_attribute;
123 return unless $attr->has_trigger;
126 'my ' . $old . ' = ' . $self->_has_value($instance),
127 '? ' . $self->_copy_old_value($self->_get_value($instance)),
132 around _eval_environment => sub {
136 my $env = $self->$orig(@_);
138 my $member_tc = $self->_tc_member_type;
140 return $env unless $member_tc;
142 $env->{'$member_tc_obj'} = \($member_tc);
144 $env->{'$member_tc'} = \( $member_tc->_compiled_type_constraint );