Convert Moose->throw_error to Moose::Util::throw
[gitmo/Moose.git] / lib / Moose / Meta / TypeCoercion / Union.pm
CommitLineData
3726f905 1
2package Moose::Meta::TypeCoercion::Union;
3
4use strict;
5use warnings;
6use metaclass;
7
3726f905 8use Scalar::Util 'blessed';
9
3726f905 10use base 'Moose::Meta::TypeCoercion';
11
12sub compile_type_coercion {
13 my $self = shift;
14 my $type_constraint = $self->type_constraint;
d03bd989 15
3726f905 16 (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Union'))
b1a85073 17 || Moose::Util::throw("You can only create a Moose::Meta::TypeCoercion::Union for a " .
4c0b3599 18 "Moose::Meta::TypeConstraint::Union, not a $type_constraint");
d03bd989 19
dc6bb63c 20 $self->_compiled_type_coercion(
21 sub {
22 my $value = shift;
23
24 foreach my $type ( grep { $_->has_coercion }
25 @{ $type_constraint->type_constraints } ) {
3726f905 26 my $temp = $type->coerce($value);
3726f905 27 return $temp if $type_constraint->check($temp);
28 }
dc6bb63c 29
30 return $value;
3726f905 31 }
dc6bb63c 32 );
3726f905 33}
34
41e007e4 35sub has_coercion_for_type { 0 }
36
37sub add_type_coercions {
b1a85073 38 Moose::Util::throw("Cannot add additional type coercions to Union types");
41e007e4 39}
40
3726f905 411;
42
ad46f524 43# ABSTRACT: The Moose Type Coercion metaclass for Unions
44
3726f905 45__END__
46
47=pod
48
3726f905 49=head1 DESCRIPTION
50
88463309 51This is a subclass of L<Moose::Meta::TypeCoercion> that is used for
52L<Moose::Meta::TypeConstraint::Union> objects.
3726f905 53=head1 METHODS
54
55=over 4
56
88463309 57=item B<< $coercion->has_coercion_for_type >>
58
59This method always returns false.
60
61=item B<< $coercion->add_type_coercions >>
3726f905 62
88463309 63This method always throws an error. You cannot add coercions to a
c6386282 64union type coercion.
3726f905 65
88463309 66=item B<< $coercion->coerce($value) >>
41e007e4 67
88463309 68This method will coerce by trying the coercions for each type in the
69union.
41e007e4 70
3726f905 71=back
72
73=head1 BUGS
74
d4048ef3 75See L<Moose/BUGS> for details on reporting bugs.
3726f905 76
e606ae5f 77=cut