2 package Moose::Meta::TypeCoercion::Union;
9 use Scalar::Util 'blessed';
11 our $VERSION = '0.56';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
15 use base 'Moose::Meta::TypeCoercion';
17 sub compile_type_coercion {
19 my $type_constraint = $self->type_constraint;
21 (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Union'))
22 || confess "You can only a Moose::Meta::TypeCoercion::Union for a " .
23 "Moose::Meta::TypeConstraint::Union, not a $type_constraint";
25 $self->_compiled_type_coercion(sub {
27 # go through all the type constraints
28 # in the union, and check em ...
29 foreach my $type (@{$type_constraint->type_constraints}) {
30 # if they have a coercion first
31 if ($type->has_coercion) {
32 # then try to coerce them ...
33 my $temp = $type->coerce($value);
34 # and if they get something
35 # make sure it still fits within
37 return $temp if $type_constraint->check($temp);
44 sub has_coercion_for_type { 0 }
46 sub add_type_coercions {
47 confess "Cannot add additional type coercions to Union types";
58 Moose::Meta::TypeCoercion::Union - The Moose Type Coercion metaclass for Unions
62 For the most part, the only time you will ever encounter an
63 instance of this class is if you are doing some serious deep
64 introspection. This API should not be considered final, but
65 it is B<highly unlikely> that this will matter to a regular
68 If you wish to use features at this depth, please come to the
69 #moose IRC channel on irc.perl.org and we can talk :)
77 =item B<compile_type_coercion>
79 =item B<has_coercion_for_type>
81 =item B<add_type_coercions>
87 All complex software has bugs lurking in it, and this module is no
88 exception. If you find a bug please either email me, or add the bug
93 Stevan Little E<lt>stevan@iinteractive.comE<gt>
95 =head1 COPYRIGHT AND LICENSE
97 Copyright 2006-2008 by Infinity Interactive, Inc.
99 L<http://www.iinteractive.com>
101 This library is free software; you can redistribute it and/or modify
102 it under the same terms as Perl itself.