2 package Moose::Meta::TypeCoercion::Union;
9 use Scalar::Util 'blessed';
11 our $VERSION = '0.01';
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 || confess "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);
51 Moose::Meta::TypeCoercion::Union - The Moose Type Coercion metaclass for Unions
55 For the most part, the only time you will ever encounter an
56 instance of this class is if you are doing some serious deep
57 introspection. This API should not be considered final, but
58 it is B<highly unlikely> that this will matter to a regular
61 If you wish to use features at this depth, please come to the
62 #moose IRC channel on irc.perl.org and we can talk :)
70 =item B<compile_type_coercion>
76 All complex software has bugs lurking in it, and this module is no
77 exception. If you find a bug please either email me, or add the bug
82 Stevan Little E<lt>stevan@iinteractive.comE<gt>
84 =head1 COPYRIGHT AND LICENSE
86 Copyright 2006, 2007 by Infinity Interactive, Inc.
88 L<http://www.iinteractive.com>
90 This library is free software; you can redistribute it and/or modify
91 it under the same terms as Perl itself.