uploadin
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints.pm
index f9fa401..42c7bef 100644 (file)
@@ -15,7 +15,7 @@ sub import {
        my $pkg = shift || caller();
        return if $pkg eq ':no_export';
        no strict 'refs';
-       foreach my $export (qw(type subtype as where to coerce)) {
+       foreach my $export (qw(type subtype as where coerce from via)) {
                *{"${pkg}::${export}"} = \&{"${export}"};
        }       
 }
@@ -29,6 +29,8 @@ sub import {
 
     sub register_type_constraint { 
         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;
     }
     
@@ -56,6 +58,8 @@ sub import {
 
     sub register_type_coercion { 
         my ($type_name, $type_coercion) = @_;
+        (not exists $COERCIONS{$type_name})
+            || confess "The type coercion for '$type_name' has already been registered";        
         $COERCIONS{$type_name} = $type_coercion;
     }
 }
@@ -65,7 +69,6 @@ sub type ($$) {
        my ($name, $check) = @_;
        my $full_name = caller() . "::${name}";
        register_type_constraint($name => subname $full_name => sub { 
-               return find_type_constraint($name) unless defined $_[0];
                local $_ = $_[0];
                return undef unless $check->($_[0]);
                $_[0];
@@ -78,8 +81,7 @@ sub subtype ($$;$) {
            my $full_name = caller() . "::${name}";
                $parent = find_type_constraint($parent) 
                    unless $parent && ref($parent) eq 'CODE';
-               register_type_constraint($name => subname $full_name => sub { 
-                       return find_type_constraint($name) unless defined $_[0];                        
+               register_type_constraint($name => subname $full_name => sub {                   
                        local $_ = $_[0];
                        return undef unless defined $parent->($_[0]) && $check->($_[0]);
                        $_[0];
@@ -114,6 +116,7 @@ sub coerce ($@) {
         foreach my $coercion (@coercions) {
             my ($constraint, $converter) = @$coercion;
             if (defined $constraint->($thing)) {
+                           local $_ = $thing;                
                 return $converter->($thing);
             }
         }
@@ -122,8 +125,9 @@ sub coerce ($@) {
 }
 
 sub as    ($) { $_[0] }
+sub from  ($) { $_[0] }
 sub where (&) { $_[0] }
-sub to    (&) { $_[0] }
+sub via   (&) { $_[0] }
 
 # define some basic types
 
@@ -168,6 +172,10 @@ Moose::Util::TypeConstraints - Type constraint system for Moose
   subtype NaturalLessThanTen 
       => as Natural
       => where { $_ < 10 };
+      
+  coerce Num 
+      => from Str
+        => via { 0+$_ }; 
 
 =head1 DESCRIPTION
 
@@ -177,10 +185,6 @@ validation.
 
 This is B<NOT> a type system for Perl 5.
 
-The type and subtype constraints are basically functions which will 
-validate their first argument. If called with no arguments, they will 
-return themselves (this is syntactic sugar for Moose attributes).
-
 This module also provides a simple hierarchy for Perl 5 types, this 
 could probably use some work, but it works for me at the moment.
 
@@ -232,7 +236,9 @@ Suggestions for improvement are welcome.
 
 =item B<coerce>
 
-=item B<to>
+=item B<from>
+
+=item B<via>
 
 =back