Bump version to 1.16
[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
f4b86ac0 10our $VERSION = '1.16';
e606ae5f 11$VERSION = eval $VERSION;
3726f905 12our $AUTHORITY = 'cpan:STEVAN';
13
14use base 'Moose::Meta::TypeCoercion';
15
16sub compile_type_coercion {
17 my $self = shift;
18 my $type_constraint = $self->type_constraint;
d03bd989 19
3726f905 20 (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Union'))
97a7bd99 21 || Moose->throw_error("You can only create a Moose::Meta::TypeCoercion::Union for a " .
4c0b3599 22 "Moose::Meta::TypeConstraint::Union, not a $type_constraint");
d03bd989 23
dc6bb63c 24 $self->_compiled_type_coercion(
25 sub {
26 my $value = shift;
27
28 foreach my $type ( grep { $_->has_coercion }
29 @{ $type_constraint->type_constraints } ) {
3726f905 30 my $temp = $type->coerce($value);
3726f905 31 return $temp if $type_constraint->check($temp);
32 }
dc6bb63c 33
34 return $value;
3726f905 35 }
dc6bb63c 36 );
3726f905 37}
38
41e007e4 39sub has_coercion_for_type { 0 }
40
41sub add_type_coercions {
70ea9161 42 require Moose;
c245d69b 43 Moose->throw_error("Cannot add additional type coercions to Union types");
41e007e4 44}
45
3726f905 461;
47
48__END__
49
50=pod
51
52=head1 NAME
53
54Moose::Meta::TypeCoercion::Union - The Moose Type Coercion metaclass for Unions
55
56=head1 DESCRIPTION
57
88463309 58This is a subclass of L<Moose::Meta::TypeCoercion> that is used for
59L<Moose::Meta::TypeConstraint::Union> objects.
3726f905 60=head1 METHODS
61
62=over 4
63
88463309 64=item B<< $coercion->has_coercion_for_type >>
65
66This method always returns false.
67
68=item B<< $coercion->add_type_coercions >>
3726f905 69
88463309 70This method always throws an error. You cannot add coercions to a
c6386282 71union type coercion.
3726f905 72
88463309 73=item B<< $coercion->coerce($value) >>
41e007e4 74
88463309 75This method will coerce by trying the coercions for each type in the
76union.
41e007e4 77
3726f905 78=back
79
80=head1 BUGS
81
d4048ef3 82See L<Moose/BUGS> for details on reporting bugs.
3726f905 83
84=head1 AUTHOR
85
86Stevan Little E<lt>stevan@iinteractive.comE<gt>
87
88=head1 COPYRIGHT AND LICENSE
89
7e0492d3 90Copyright 2006-2010 by Infinity Interactive, Inc.
3726f905 91
92L<http://www.iinteractive.com>
93
94This library is free software; you can redistribute it and/or modify
d03bd989 95it under the same terms as Perl itself.
3726f905 96
e606ae5f 97=cut