2 package Moose::Meta::TypeCoercion::Union;
8 use Scalar::Util 'blessed';
10 our $VERSION = '1.08';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Moose::Meta::TypeCoercion';
16 sub compile_type_coercion {
18 my $type_constraint = $self->type_constraint;
20 (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Union'))
21 || Moose->throw_error("You can only a Moose::Meta::TypeCoercion::Union for a " .
22 "Moose::Meta::TypeConstraint::Union, not a $type_constraint");
24 $self->_compiled_type_coercion(sub {
26 # go through all the type constraints
27 # in the union, and check em ...
28 foreach my $type (@{$type_constraint->type_constraints}) {
29 # if they have a coercion first
30 if ($type->has_coercion) {
31 # then try to coerce them ...
32 my $temp = $type->coerce($value);
33 # and if they get something
34 # make sure it still fits within
36 return $temp if $type_constraint->check($temp);
43 sub has_coercion_for_type { 0 }
45 sub add_type_coercions {
47 Moose->throw_error("Cannot add additional type coercions to Union types");
58 Moose::Meta::TypeCoercion::Union - The Moose Type Coercion metaclass for Unions
62 This is a subclass of L<Moose::Meta::TypeCoercion> that is used for
63 L<Moose::Meta::TypeConstraint::Union> objects.
68 =item B<< $coercion->has_coercion_for_type >>
70 This method always returns false.
72 =item B<< $coercion->add_type_coercions >>
74 This method always throws an error. You cannot add coercions to a
77 =item B<< $coercion->coerce($value) >>
79 This method will coerce by trying the coercions for each type in the
86 See L<Moose/BUGS> for details on reporting bugs.
90 Stevan Little E<lt>stevan@iinteractive.comE<gt>
92 =head1 COPYRIGHT AND LICENSE
94 Copyright 2006-2010 by Infinity Interactive, Inc.
96 L<http://www.iinteractive.com>
98 This library is free software; you can redistribute it and/or modify
99 it under the same terms as Perl itself.