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