X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FUtil%2FTypeConstraints.pm;fp=lib%2FMoose%2FUtil%2FTypeConstraints.pm;h=b3e5b60b36bfdecd31ced337acadd1d010e4ac4a;hb=a46050ae36c5dd561497f9b6a413e41cdfdf8ca4;hp=9344607598e0da24266535581a9d498a2c5636cc;hpb=a0fa555969367b3def4ad2d91689625293d9d938;p=gitmo%2FMoose.git diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 9344607..b3e5b60 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -92,6 +92,36 @@ sub create_type_constraint_union { type_constraints => \@type_constraints ); } +sub create_named_type_constraint_union { + my $name = shift; + my @type_constraint_names; + + if ( scalar @_ == 1 && _detect_type_constraint_union( $_[0] ) ) { + @type_constraint_names = _parse_type_constraint_union( $_[0] ); + } + else { + @type_constraint_names = @_; + } + + ( scalar @type_constraint_names >= 2 ) + || __PACKAGE__->_throw_error( + "You must pass in at least 2 type names to make a union"); + + my @type_constraints = map { + find_or_parse_type_constraint($_) + || __PACKAGE__->_throw_error( + "Could not locate type constraint ($_) for the union"); + } @type_constraint_names; + + my %options = ( + type_constraints => \@type_constraints + ); + $options{name} = $name if defined $name; + + return Moose::Meta::TypeConstraint::Union->new(%options); +} + + sub create_parameterized_type_constraint { my $type_constraint_name = shift; my ( $base_type, $type_parameter ) @@ -412,12 +442,12 @@ sub union { if ( @constraints == 1 && ref $constraints[0] eq 'ARRAY' ) { @constraints = @{ $constraints[0] }; } - my $tc = create_type_constraint_union( @constraints ); if ( defined $type_name ) { - $tc->name( $type_name ); - return register_type_constraint( $tc ); + return register_type_constraint( + create_named_type_constraint_union( $type_name, @constraints ) + ); } - return $tc; + return create_type_constraint_union( @constraints ); } sub create_enum_type_constraint {