add C<equals> method to TypeConstraint, and make use of it int he subtype checks
Yuval Kogman [Sat, 12 Apr 2008 15:00:01 +0000 (15:00 +0000)]
lib/Moose/Meta/TypeConstraint.pm
lib/Moose/Meta/TypeConstraint/Class.pm

index 4592a63..10fddf7 100644 (file)
@@ -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<new>
 
-=item B<is_a_type_of ($type_name)>
+=item B<equals ($type_name_or_object)>
+
+=item B<is_a_type_of ($type_name_or_object)>
 
 This checks the current type name, and if it does not match,
 checks if it is a subtype of it.
 
-=item B<is_subtype_of ($type_name)>
+=item B<is_subtype_of ($type_name_or_object)>
 
 =item B<compile_type_constraint>
 
@@ -249,6 +267,8 @@ the C<message> will be used to construct a custom error message.
 
 =item B<has_parent>
 
+=item B<parents>
+
 =item B<constraint>
 
 =item B<has_message>
index 41cc2c6..ef2970a 100644 (file)
@@ -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<has_hand_optimized_type_constraint>
 
+=item B<equals>
+
 =item B<is_a_type_of>
 
 =item B<is_subtype_of>