1 package Moose::Meta::Method::Accessor::Native::Collection;
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;
57 my ($potential_value) = @_;
59 return unless $self->_constraint_must_be_checked;
61 if ($self->_check_new_members_only) {
62 return unless $self->_adds_members;
64 return $self->_inline_check_member_constraint($self->_new_members);
68 $self->_inline_check_coercion($potential_value),
69 $self->_inline_check_constraint($potential_value),
74 sub _check_new_members_only {
77 my $attr = $self->associated_attribute;
79 my $tc = $attr->type_constraint;
81 # If we have a coercion, we could come up with an entirely new value after
82 # coercing, so we need to check everything,
83 return 0 if $attr->should_coerce && $tc->has_coercion;
85 # If the parent is our root type (ArrayRef, HashRef, etc), that means we
86 # can just check the new members of the collection, because we know that
87 # we will always be generating an appropriate collection type.
89 # However, if this type has its own constraint (it's Parameteriz_able_,
90 # not Paramet_erized_), we don't know what is being checked by the
91 # constraint, so we need to check the whole value, not just the members.
93 if $self->_is_root_type( $tc->parent )
94 && $tc->isa('Moose::Meta::TypeConstraint::Parameterized');
99 sub _inline_check_member_constraint {
101 my ($new_value) = @_;
103 my $attr_name = $self->associated_attribute->name;
106 'for (' . $new_value . ') {',
107 'if (!$member_tc->($_)) {',
108 $self->_inline_throw_error(
109 '"A new member value for ' . $attr_name
110 . ' does not pass its type constraint because: "'
111 . ' . $member_tc->get_message($_)',
119 sub _inline_get_old_value_for_trigger {
121 my ($instance, $old) = @_;
123 my $attr = $self->associated_attribute;
124 return unless $attr->has_trigger;
127 'my ' . $old . ' = ' . $self->_has_value($instance),
128 '? ' . $self->_copy_old_value($self->_get_value($instance)),
133 around _eval_environment => sub {
137 my $env = $self->$orig(@_);
139 my $member_tc = $self->_tc_member_type;
141 return $env unless $member_tc;
143 $env->{'$member_tc_obj'} = \($member_tc);
145 $env->{'$member_tc'} = \( $member_tc->_compiled_type_constraint );