From: Yuval Kogman Date: Sat, 12 Apr 2008 15:00:01 +0000 (+0000) Subject: add C method to TypeConstraint, and make use of it int he subtype checks X-Git-Tag: 0_55~235 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d9e17f8049b051a866c22a3c8953f1a2aff7fd91;p=gitmo%2FMoose.git add C method to TypeConstraint, and make use of it int he subtype checks --- diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index 4592a63..10fddf7 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -83,18 +83,34 @@ sub get_message { ## type predicates ... +sub equals { + my ( $self, $type_or_name ) = @_; + + my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + + $self->name eq $type->name; +} + sub is_a_type_of { - my ($self, $type_name) = @_; - ($self->name eq $type_name || $self->is_subtype_of($type_name)); + my ($self, $type_or_name) = @_; + + my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + + ($self->equals($type) || $self->is_subtype_of($type)); } sub is_subtype_of { - my ($self, $type_name) = @_; + my ($self, $type_or_name) = @_; + + my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + my $current = $self; + while (my $parent = $current->parent) { - return 1 if $parent->name eq $type_name; + return 1 if $parent->equals($type); $current = $parent; } + return 0; } @@ -218,12 +234,14 @@ If you wish to use features at this depth, please come to the =item B -=item B +=item B + +=item B This checks the current type name, and if it does not match, checks if it is a subtype of it. -=item B +=item B =item B @@ -249,6 +267,8 @@ the C will be used to construct a custom error message. =item B +=item B + =item B =item B diff --git a/lib/Moose/Meta/TypeConstraint/Class.pm b/lib/Moose/Meta/TypeConstraint/Class.pm index 41cc2c6..ef2970a 100644 --- a/lib/Moose/Meta/TypeConstraint/Class.pm +++ b/lib/Moose/Meta/TypeConstraint/Class.pm @@ -54,17 +54,44 @@ sub hand_optimized_type_constraint { sub has_hand_optimized_type_constraint { 1 } +sub equals { + my ( $self, $type_or_name ) = @_; + + my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + + if ( $type->isa(__PACKAGE__) ) { + return $self->class eq $type->class; + } else { + $self->SUPER::equals($type); + } +} + sub is_a_type_of { - my ($self, $type_name) = @_; + my ($self, $type_or_name) = @_; - return $self->name eq $type_name || $self->is_subtype_of($type_name); + my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + + ($self->equals($type) || $self->is_subtype_of($type_or_name)); } sub is_subtype_of { - my ($self, $type_name) = @_; - - return 1 if $type_name eq 'Object'; - return $self->name->isa( $type_name ); + my ($self, $type_or_name_or_class ) = @_; + + if ( not ref $type_or_name_or_class ) { + # it might be a class + return 1 if $self->class->isa( $type_or_name_or_class ); + } + + my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name_or_class); + + if ( $type->isa(__PACKAGE__) ) { + # if $type_or_name_or_class isn't a class, it might be the TC name of another ::Class type + # or it could also just be a type object in this branch + return $self->class->isa( $type->class ); + } else { + # the only other thing we are a subtype of is Object + $self->SUPER::is_subtype_of($type); + } } 1; @@ -87,6 +114,8 @@ Moose::Meta::TypeConstraint::Class - Class/TypeConstraint parallel hierarchy =item B +=item B + =item B =item B