Some error checks (I think it's time for a dedicated type test file)
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Union.pm
CommitLineData
8ee73eeb 1
2package Moose::Meta::TypeConstraint::Union;
3
4use strict;
5use warnings;
6use metaclass;
7
3726f905 8use Moose::Meta::TypeCoercion::Union;
9
d67145ed 10our $VERSION = '0.06';
d44714be 11our $AUTHORITY = 'cpan:STEVAN';
8ee73eeb 12
d67145ed 13use base 'Moose::Meta::TypeConstraint';
14
8ee73eeb 15__PACKAGE__->meta->add_attribute('type_constraints' => (
16 accessor => 'type_constraints',
17 default => sub { [] }
18));
19
20sub new {
3726f905 21 my ($class, %options) = @_;
22 my $self = $class->SUPER::new(
23 name => (join ' | ' => map { $_->name } @{$options{type_constraints}}),
24 parent => undef,
25 message => undef,
26 hand_optimized_type_constraint => undef,
27 compiled_type_constraint => sub {
28 my $value = shift;
29 foreach my $type (@{$options{type_constraints}}) {
30 return 1 if $type->check($value);
31 }
32 return undef;
33 },
34 %options
35 );
36 $self->_set_constraint(sub { $self->check($_[0]) });
37 $self->coercion(Moose::Meta::TypeCoercion::Union->new(
38 type_constraint => $self
39 ));
8ee73eeb 40 return $self;
41}
42
8ee73eeb 43sub validate {
3726f905 44 my ($self, $value) = @_;
8ee73eeb 45 my $message;
46 foreach my $type (@{$self->type_constraints}) {
47 my $err = $type->validate($value);
48 return unless defined $err;
49 $message .= ($message ? ' and ' : '') . $err
50 if defined $err;
51 }
52 return ($message . ' in (' . $self->name . ')') ;
53}
54
55sub is_a_type_of {
56 my ($self, $type_name) = @_;
57 foreach my $type (@{$self->type_constraints}) {
58 return 1 if $type->is_a_type_of($type_name);
59 }
60 return 0;
61}
62
63sub is_subtype_of {
64 my ($self, $type_name) = @_;
65 foreach my $type (@{$self->type_constraints}) {
66 return 1 if $type->is_subtype_of($type_name);
67 }
68 return 0;
69}
70
711;
72
73__END__
74
75=pod
76
39b3bc94 77=head1 NAME
78
ecb59493 79Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
39b3bc94 80
81=head1 DESCRIPTION
82
ecb59493 83This metaclass represents a union of Moose type constraints. More
84details to be explained later (possibly in a Cookbook::Recipe).
85
86This actually used to be part of Moose::Meta::TypeConstraint, but it
87is now better off in it's own file.
88
39b3bc94 89=head1 METHODS
90
ecb59493 91This class is not a subclass of Moose::Meta::TypeConstraint,
92but it does provide the same API
93
39b3bc94 94=over 4
95
ecb59493 96=item B<meta>
39b3bc94 97
ecb59493 98=item B<new>
39b3bc94 99
ecb59493 100=item B<name>
101
102=item B<type_constraints>
39b3bc94 103
104=item B<constraint>
105
ecb59493 106=back
39b3bc94 107
ecb59493 108=head2 Overriden methods
109
110=over 4
111
112=item B<check>
113
114=item B<coerce>
115
116=item B<validate>
39b3bc94 117
118=item B<is_a_type_of>
119
120=item B<is_subtype_of>
121
ecb59493 122=back
39b3bc94 123
ecb59493 124=head2 Empty or Stub methods
39b3bc94 125
ecb59493 126These methods tend to not be very relevant in
127the context of a union. Either that or they are
128just difficult to specify and not very useful
129anyway. They are here for completeness.
39b3bc94 130
ecb59493 131=over 4
39b3bc94 132
133=item B<parent>
134
ecb59493 135=item B<coercion>
39b3bc94 136
ecb59493 137=item B<has_coercion>
138
139=item B<message>
140
141=item B<has_message>
39b3bc94 142
0eec94be 143=item B<hand_optimized_type_constraint>
144
145=item B<has_hand_optimized_type_constraint>
146
39b3bc94 147=back
148
149=head1 BUGS
150
151All complex software has bugs lurking in it, and this module is no
152exception. If you find a bug please either email me, or add the bug
153to cpan-RT.
154
155=head1 AUTHOR
156
157Stevan Little E<lt>stevan@iinteractive.comE<gt>
158
39b3bc94 159=head1 COPYRIGHT AND LICENSE
160
778db3ac 161Copyright 2006-2008 by Infinity Interactive, Inc.
39b3bc94 162
163L<http://www.iinteractive.com>
164
165This library is free software; you can redistribute it and/or modify
166it under the same terms as Perl itself.
167
8ee73eeb 168=cut