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;
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;
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 } @_;