More replacement of Moose->throw_error with 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'))
97a7bd99 17 || Moose->throw_error("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 {
70ea9161 38 require Moose;
c245d69b 39 Moose->throw_error("Cannot add additional type coercions to Union types");
41e007e4 40}
41
3726f905 421;
43
ad46f524 44# ABSTRACT: The Moose Type Coercion metaclass for Unions
45
3726f905 46__END__
47
48=pod
49
3726f905 50=head1 DESCRIPTION
51
88463309 52This is a subclass of L<Moose::Meta::TypeCoercion> that is used for
53L<Moose::Meta::TypeConstraint::Union> objects.
3726f905 54=head1 METHODS
55
56=over 4
57
88463309 58=item B<< $coercion->has_coercion_for_type >>
59
60This method always returns false.
61
62=item B<< $coercion->add_type_coercions >>
3726f905 63
88463309 64This method always throws an error. You cannot add coercions to a
c6386282 65union type coercion.
3726f905 66
88463309 67=item B<< $coercion->coerce($value) >>
41e007e4 68
88463309 69This method will coerce by trying the coercions for each type in the
70union.
41e007e4 71
3726f905 72=back
73
74=head1 BUGS
75
d4048ef3 76See L<Moose/BUGS> for details on reporting bugs.
3726f905 77
e606ae5f 78=cut