getting-there
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints.pm
index 42c7bef..eb5524d 100644 (file)
@@ -10,6 +10,8 @@ use Scalar::Util 'blessed';
 
 our $VERSION = '0.02';
 
+use Moose::Meta::TypeConstraint;
+
 sub import {
        shift;
        my $pkg = shift || caller();
@@ -31,7 +33,10 @@ sub import {
         my ($type_name, $type_constraint) = @_;
         (not exists $TYPES{$type_name})
             || confess "The type constraint '$type_name' has already been registered";
-        $TYPES{$type_name} = $type_constraint;
+        $TYPES{$type_name} = Moose::Meta::TypeConstraint->new(
+            name            => $type_name,
+            constraint_code => $type_constraint
+        );
     }
     
     sub dump_type_constraints {
@@ -44,23 +49,21 @@ sub import {
         my $pkg = caller();
            no strict 'refs';
        foreach my $constraint (keys %TYPES) {
-               *{"${pkg}::${constraint}"} = $TYPES{$constraint};
+               *{"${pkg}::${constraint}"} = $TYPES{$constraint}->constraint_code;
        }        
     }
-}
 
-{
-    my %COERCIONS;
     sub find_type_coercion { 
         my $type_name = shift;
-        $COERCIONS{$type_name}; 
+        $TYPES{$type_name}->coercion_code; 
     }
 
     sub register_type_coercion { 
         my ($type_name, $type_coercion) = @_;
-        (not exists $COERCIONS{$type_name})
+        my $type = $TYPES{$type_name};
+        (!$type->has_coercion)
             || confess "The type coercion for '$type_name' has already been registered";        
-        $COERCIONS{$type_name} = $type_coercion;
+        $type->set_coercion_code($type_coercion);
     }
 }
 
@@ -79,7 +82,7 @@ sub subtype ($$;$) {
        my ($name, $parent, $check) = @_;
        if (defined $check) {
            my $full_name = caller() . "::${name}";
-               $parent = find_type_constraint($parent) 
+               $parent = find_type_constraint($parent)->constraint_code 
                    unless $parent && ref($parent) eq 'CODE';
                register_type_constraint($name => subname $full_name => sub {                   
                        local $_ = $_[0];
@@ -89,7 +92,7 @@ sub subtype ($$;$) {
        }
        else {
                ($parent, $check) = ($name, $parent);
-               $parent = find_type_constraint($parent) 
+               $parent = find_type_constraint($parent)->constraint_code 
                    unless $parent && ref($parent) eq 'CODE';           
                return subname '__anon_subtype__' => sub {                      
                        local $_ = $_[0];
@@ -106,7 +109,7 @@ sub coerce ($@) {
     my @coercions;
     while (@coercion_map) {
         my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
-        my $constraint = find_type_constraint($constraint_name);
+        my $constraint = find_type_constraint($constraint_name)->constraint_code;
         (defined $constraint)
             || confess "Could not find the type constraint ($constraint_name)";
         push @coercions => [  $constraint, $action ];