bump version and update changes for release later today (I hope)
[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
8use Carp 'confess';
9use Scalar::Util 'blessed';
10
fb4fcfee 11our $VERSION = '0.57';
75b95414 12$VERSION = eval $VERSION;
3726f905 13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Moose::Meta::TypeCoercion';
16
17sub compile_type_coercion {
18 my $self = shift;
19 my $type_constraint = $self->type_constraint;
20
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";
24
25 $self->_compiled_type_coercion(sub {
26 my $value = shift;
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
8de73ff1 31 if ($type->has_coercion) {
3726f905 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
36 # the union type ...
37 return $temp if $type_constraint->check($temp);
38 }
39 }
40 return undef;
41 });
42}
43
41e007e4 44sub has_coercion_for_type { 0 }
45
46sub add_type_coercions {
47 confess "Cannot add additional type coercions to Union types";
48}
49
3726f905 501;
51
52__END__
53
54=pod
55
56=head1 NAME
57
58Moose::Meta::TypeCoercion::Union - The Moose Type Coercion metaclass for Unions
59
60=head1 DESCRIPTION
61
62For the most part, the only time you will ever encounter an
63instance of this class is if you are doing some serious deep
64introspection. This API should not be considered final, but
65it is B<highly unlikely> that this will matter to a regular
66Moose user.
67
68If you wish to use features at this depth, please come to the
69#moose IRC channel on irc.perl.org and we can talk :)
70
71=head1 METHODS
72
73=over 4
74
75=item B<meta>
76
77=item B<compile_type_coercion>
78
41e007e4 79=item B<has_coercion_for_type>
80
81=item B<add_type_coercions>
82
3726f905 83=back
84
85=head1 BUGS
86
87All complex software has bugs lurking in it, and this module is no
88exception. If you find a bug please either email me, or add the bug
89to cpan-RT.
90
91=head1 AUTHOR
92
93Stevan Little E<lt>stevan@iinteractive.comE<gt>
94
95=head1 COPYRIGHT AND LICENSE
96
778db3ac 97Copyright 2006-2008 by Infinity Interactive, Inc.
3726f905 98
99L<http://www.iinteractive.com>
100
101This library is free software; you can redistribute it and/or modify
102it under the same terms as Perl itself.
103
07b0f1a5 104=cut