0.99 ???
+ [NEW FEATURES]
+
+ * New method find_type_for in Moose::Meta::TypeConstraint::Union, for finding
+ which member of the union a given value validates for. (Cory Watson)
+
[NEW DOCUMENTATION]
* Added Moose::Manual::Support that defines the support, compatiblity, and
use Moose::Meta::TypeCoercion::Union;
+use List::Util qw(first);
+
our $VERSION = '0.98';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
return ($message . ' in (' . $self->name . ')') ;
}
+sub find_type_for {
+ my ($self, $value) = @_;
+
+ return first { $_->check($value) } @{ $self->type_constraints };
+}
+
sub is_a_type_of {
my ($self, $type_name) = @_;
foreach my $type (@{$self->type_constraints}) {
A type is considered equal if it is also a union type, and the two
unions have the same member types.
+=item B<< $constraint->find_type_for($value) >>
+
+This returns the first member type constraint for which C<check($value)> is
+true, allowing you to determine which of the Union's member type constraints
+a given value matches.
+
=item B<< $constraint->is_a_type_of($type_name_or_object) >>
This returns true if any of the member type constraints return true
ok($Str_or_Undef->is_a_type_of($Str), "subtype of Str");
ok($Str_or_Undef->is_a_type_of($Undef), "subtype of Undef");
+cmp_ok($Str_or_Undef->find_type_for('String'), 'eq', 'Str', 'find_type_for Str');
+cmp_ok($Str_or_Undef->find_type_for(undef), 'eq', 'Undef', 'find_type_for Undef');
+ok(!defined($Str_or_Undef->find_type_for(sub { })), 'no find_type_for CodeRef');
+
ok( !$Str_or_Undef->equals($Str), "not equal to Str" );
ok( $Str_or_Undef->equals($Str_or_Undef), "equal to self" );
ok( $Str_or_Undef->equals(Moose::Meta::TypeConstraint::Union->new(type_constraints => [ $Str, $Undef ])), "equal to clone" );