Simplistic implementation of type intersections, modeled after the implementation...
[gitmo/Moose.git] / lib / Moose / Meta / TypeCoercion / Intersection.pm
CommitLineData
8aab053a 1
2package Moose::Meta::TypeCoercion::Intersection;
3
4use strict;
5use warnings;
6use metaclass;
7
8use Scalar::Util 'blessed';
9
10our $VERSION = '0.70';
11$VERSION = eval $VERSION;
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;
19
20 (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Intersection'))
21 || Moose->throw_error("You can only a Moose::Meta::TypeCoercion::Intersection for a " .
22 "Moose::Meta::TypeConstraint::Intersection, not a $type_constraint");
23
24 $self->_compiled_type_coercion(sub {
25 my $value = shift;
26 # go through all the type constraints
27 # in the intersection, 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
35 # the intersection type ...
36 return $temp if $type_constraint->check($temp);
37 }
38 }
39 return undef;
40 });
41}
42
43sub has_coercion_for_type { 0 }
44
45sub add_type_coercions {
46 Moose->throw_error("Cannot add additional type coercions to Intersection types");
47}
48
491;
50
51__END__
52
53=pod
54
55=head1 NAME
56
57Moose::Meta::TypeCoercion::Intersection - The Moose Type Coercion metaclass for intersections
58
59=head1 DESCRIPTION
60
61For the most part, the only time you will ever encounter an
62instance of this class is if you are doing some serious deep
63introspection. This API should not be considered final, but
64it is B<highly unlikely> that this will matter to a regular
65Moose user.
66
67If you wish to use features at this depth, please come to the
68#moose IRC channel on irc.perl.org and we can talk :)
69
70=head1 METHODS
71
72=over 4
73
74=item B<meta>
75
76=item B<compile_type_coercion>
77
78=item B<has_coercion_for_type>
79
80=item B<add_type_coercions>
81
82=back
83
84=head1 BUGS
85
86All complex software has bugs lurking in it, and this module is no
87exception. If you find a bug please either email me, or add the bug
88to cpan-RT.
89
90=head1 AUTHOR
91
92Stevan Little E<lt>stevan@iinteractive.comE<gt> and
93Adam Foxson E<lt>afoxson@pobox.comE<gt>
94
95=head1 COPYRIGHT AND LICENSE
96
97Copyright 2006-2009 by Infinity Interactive, Inc.
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
104=cut