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