more-tweaks
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints.pm
index b37e824..b70c870 100644 (file)
@@ -5,19 +5,19 @@ use strict;
 use warnings;
 
 use Carp         'confess';
-use Sub::Name    'subname';
 use Scalar::Util 'blessed';
 
 our $VERSION = '0.02';
 
 use Moose::Meta::TypeConstraint;
+use Moose::Meta::TypeCoercion;
 
 sub import {
        shift;
        my $pkg = shift || caller();
-       return if $pkg eq ':no_export';
+       return if $pkg eq '-no-export';
        no strict 'refs';
-       foreach my $export (qw(type subtype as where coerce from via)) {
+       foreach my $export (qw(type subtype as where coerce from via find_type_constraint)) {
                *{"${pkg}::${export}"} = \&{"${export}"};
        }       
 }
@@ -26,83 +26,57 @@ sub import {
     my %TYPES;
     sub find_type_constraint { $TYPES{$_[0]} }
 
-    sub create_type_constraint { 
-        my ($name, $parent, $constraint) = @_;
-        (not exists $TYPES{$name})
-            || confess "The type constraint '$name' has already been created";
-        $parent = find_type_constraint($parent) if defined $parent;
-        $TYPES{$name} = Moose::Meta::TypeConstraint->new(
-            name       => $name,
+    sub _create_type_constraint { 
+        my ($name, $parent, $check) = @_;
+        (!exists $TYPES{$name})
+            || confess "The type constraint '$name' has already been created"
+                if defined $name;
+        $parent = $TYPES{$parent} if defined $parent;
+        my $constraint = Moose::Meta::TypeConstraint->new(
+            name       => $name || '__ANON__',
             parent     => $parent,            
-            constraint => $constraint,           
+            constraint => $check,           
         );
+        $TYPES{$name} = $constraint if defined $name;
+        return $constraint;
     }
 
-    sub find_type_coercion { 
-        my $type_name = shift;
-        $TYPES{$type_name}->coercion_code; 
-    }
-
-    sub register_type_coercion { 
-        my ($type_name, $type_coercion) = @_;
+    sub _install_type_coercions { 
+        my ($type_name, $coercion_map) = @_;
         my $type = $TYPES{$type_name};
         (!$type->has_coercion)
             || confess "The type coercion for '$type_name' has already been registered";        
-        $type->set_coercion_code($type_coercion);
+        my $type_coercion = Moose::Meta::TypeCoercion->new(
+            type_coercion_map => $coercion_map,
+            type_constraint   => $type
+        );            
+        $type->coercion($type_coercion);
     }
     
     sub export_type_contstraints_as_functions {
         my $pkg = caller();
            no strict 'refs';
        foreach my $constraint (keys %TYPES) {
-               *{"${pkg}::${constraint}"} = $TYPES{$constraint}->constraint_code;
+               *{"${pkg}::${constraint}"} = $TYPES{$constraint}->_compiled_type_constraint;
        }        
     }    
 }
 
+# type constructors
 
 sub type ($$) {
        my ($name, $check) = @_;
-       create_type_constraint($name, undef, $check);
+       _create_type_constraint($name, undef, $check);
 }
 
 sub subtype ($$;$) {
-       if (scalar @_ == 3) {
-           my ($name, $parent, $check) = @_;
-               create_type_constraint($name, $parent, $check); 
-       }
-       else {
-               my ($parent, $check) = @_;
-               $parent = find_type_constraint($parent);
-        return Moose::Meta::TypeConstraint->new(
-            name       => '__ANON__',
-            parent     => $parent,
-            constraint => $check,
-        );
-       }
+       unshift @_ => undef if scalar @_ == 2;
+       _create_type_constraint(@_);
 }
 
 sub coerce ($@) {
     my ($type_name, @coercion_map) = @_;   
-    my @coercions;
-    while (@coercion_map) {
-        my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
-        my $constraint = find_type_constraint($constraint_name)->constraint_code;
-        (defined $constraint)
-            || confess "Could not find the type constraint ($constraint_name)";
-        push @coercions => [  $constraint, $action ];
-    }
-    register_type_coercion($type_name, sub { 
-        my $thing = shift;
-        foreach my $coercion (@coercions) {
-            my ($constraint, $converter) = @$coercion;
-            if (defined $constraint->($thing)) {
-                           local $_ = $thing;                
-                return $converter->($thing);
-            }
-        }
-        return $thing;
-    });
+    _install_type_coercions($type_name, \@coercion_map);
 }
 
 sub as    ($) { $_[0] }
@@ -191,16 +165,12 @@ Suggestions for improvement are welcome.
 
 =item B<find_type_constraint ($type_name)>
 
-=item B<create_type_constraint ($type_name, $type_constraint)>
-
-=item B<find_type_coercion>
+=item B<_create_type_constraint ($type_name, $type_constraint)>
 
-=item B<register_type_coercion>
+=item B<_install_type_coercions>
 
 =item B<export_type_contstraints_as_functions>
 
-=item B<dump_type_constraints>
-
 =back
 
 =head2 Type Constraint Constructors