Update error message to match Moose's
[gitmo/Mouse.git] / lib / Mouse / Util / TypeConstraints.pm
index 885788a..6410243 100644 (file)
@@ -90,7 +90,13 @@ sub subtype {
     if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) {
         Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
     };
-    my $constraint = $conf{where} || do { $TYPE{delete $conf{as} || 'Any' } };
+    my $constraint = $conf{where} || do {
+        my $as = delete $conf{as} || 'Any';
+        if (! exists $TYPE{$as}) { # Perhaps it's a parameterized source?
+            Mouse::Meta::Attribute::_build_type_constraint($as);
+        }
+        $TYPE{$as};
+    };
     my $as         = $conf{as} || '';
 
     $TYPE_SOURCE{$name} = $pkg;
@@ -116,8 +122,14 @@ sub coerce {
         Carp::croak "A coercion action already exists for '$type'"
             if $COERCE{$name}->{$type};
 
-        Carp::croak "Could not find the type constraint ($type) to coerce from"
-            unless $TYPE{$type};
+        if (! $TYPE{$type}) {
+            # looks parameterized
+            if ($type =~ /^[^\[]+\[.+\]$/) {
+                Mouse::Meta::Attribute::_build_type_constraint($type);
+            } else {
+                Carp::croak "Could not find the type constraint ($type) to coerce from"
+            }
+        }
 
         push @{ $COERCE_KEYS{$name} }, $type;
         $COERCE{$name}->{$type} = $code;
@@ -161,7 +173,19 @@ sub typecast_constraints {
     return $value;
 }
 
+my $serial_enum = 0;
 sub enum {
+    # enum ['small', 'medium', 'large']
+    if (ref($_[0]) eq 'ARRAY') {
+        my @elements = @{ shift @_ };
+
+        my $name = 'Mouse::Util::TypeConstaints::Enum::Serial::'
+                 . ++$serial_enum;
+        enum($name, @elements);
+        return $name;
+    }
+
+    # enum size => 'small', 'medium', 'large'
     my $name = shift;
     my %is_valid = map { $_ => 1 } @_;