From: Dave Rolsky Date: Sat, 17 Sep 2011 14:46:03 +0000 (-0500) Subject: Fix Union->parent to return the nearest common ancestor X-Git-Tag: 2.0300~42 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c0841d0c2cb7adbe152c01f63fa6e830ee4fbe71;p=gitmo%2FMoose.git Fix Union->parent to return the nearest common ancestor This lets us revert an earlier change to TC->is_subtype_of --- diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index e135a38..f5dd147 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -289,7 +289,7 @@ sub is_subtype_of { my $current = $self; while (my $parent = $current->parent) { - return 1 if $parent->is_a_type_of($type); + return 1 if $parent->equals($type); $current = $parent; } @@ -420,6 +420,11 @@ sub _collect_all_parents { return @parents; } +sub _ancestor_count { + my $self = shift; + return scalar $self->_collect_all_parents; +} + sub create_child_type { my ($self, %opts) = @_; my $class = ref $self; diff --git a/lib/Moose/Meta/TypeConstraint/Union.pm b/lib/Moose/Meta/TypeConstraint/Union.pm index b604fe9..410c622 100644 --- a/lib/Moose/Meta/TypeConstraint/Union.pm +++ b/lib/Moose/Meta/TypeConstraint/Union.pm @@ -128,9 +128,17 @@ sub equals { return @other_constraints == 0; } -sub parents { +sub parent { my $self = shift; - $self->type_constraints; + + my @tcs = @{ $self->type_constraints }; + + my $deepest + = ( sort { $a->_ancestor_count <=> $b->_ancestor_count } @tcs )[-1]; + + for my $parent ( $deepest->_collect_all_parents ) { + return $parent if all { $_->is_a_type_of($parent) } @tcs; + } } sub validate { @@ -229,9 +237,9 @@ attribute is a L object. This returns the array reference of C provided to the constructor. -=item B<< $constraint->parents >> +=item B<< $constraint->parent >> -This returns the same constraint as the C method. +This returns the nearest common ancestor of all the components of the union. =item B<< $constraint->check($value) >>