bump version to 0.59
[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
2351f08e 10our $VERSION = '0.59';
75b95414 11$VERSION = eval $VERSION;
d44714be 12our $AUTHORITY = 'cpan:STEVAN';
8ee73eeb 13
d67145ed 14use base 'Moose::Meta::TypeConstraint';
15
8ee73eeb 16__PACKAGE__->meta->add_attribute('type_constraints' => (
17 accessor => 'type_constraints',
18 default => sub { [] }
19));
20
21sub new {
3726f905 22 my ($class, %options) = @_;
23 my $self = $class->SUPER::new(
84a9c64c 24 name => (join '|' => sort map { $_->name } @{$options{type_constraints}}),
3726f905 25 parent => undef,
26 message => undef,
27 hand_optimized_type_constraint => undef,
28 compiled_type_constraint => sub {
29 my $value = shift;
30 foreach my $type (@{$options{type_constraints}}) {
31 return 1 if $type->check($value);
32 }
33 return undef;
34 },
35 %options
36 );
37 $self->_set_constraint(sub { $self->check($_[0]) });
38 $self->coercion(Moose::Meta::TypeCoercion::Union->new(
39 type_constraint => $self
40 ));
8ee73eeb 41 return $self;
42}
43
dabed765 44sub equals {
45 my ( $self, $type_or_name ) = @_;
46
47 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
48
49 return unless $other->isa(__PACKAGE__);
50
51 my @self_constraints = @{ $self->type_constraints };
52 my @other_constraints = @{ $other->type_constraints };
53
54 return unless @self_constraints == @other_constraints;
55
56 # FIXME presort type constraints for efficiency?
57 constraint: foreach my $constraint ( @self_constraints ) {
58 for ( my $i = 0; $i < @other_constraints; $i++ ) {
59 if ( $constraint->equals($other_constraints[$i]) ) {
60 splice @other_constraints, $i, 1;
61 next constraint;
62 }
63 }
64 }
65
66 return @other_constraints == 0;
67}
68
69sub parents {
70 my $self = shift;
71 $self->type_constraints;
72}
73
8ee73eeb 74sub validate {
3726f905 75 my ($self, $value) = @_;
8ee73eeb 76 my $message;
77 foreach my $type (@{$self->type_constraints}) {
78 my $err = $type->validate($value);
79 return unless defined $err;
80 $message .= ($message ? ' and ' : '') . $err
81 if defined $err;
82 }
83 return ($message . ' in (' . $self->name . ')') ;
84}
85
86sub is_a_type_of {
87 my ($self, $type_name) = @_;
88 foreach my $type (@{$self->type_constraints}) {
89 return 1 if $type->is_a_type_of($type_name);
90 }
91 return 0;
92}
93
94sub is_subtype_of {
95 my ($self, $type_name) = @_;
96 foreach my $type (@{$self->type_constraints}) {
97 return 1 if $type->is_subtype_of($type_name);
98 }
99 return 0;
100}
101
1021;
103
104__END__
105
106=pod
107
39b3bc94 108=head1 NAME
109
ecb59493 110Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
39b3bc94 111
112=head1 DESCRIPTION
113
ecb59493 114This metaclass represents a union of Moose type constraints. More
5cfe3805 115details to be explained later (possibly in a Cookbook recipe).
ecb59493 116
117This actually used to be part of Moose::Meta::TypeConstraint, but it
118is now better off in it's own file.
119
39b3bc94 120=head1 METHODS
121
ecb59493 122This class is not a subclass of Moose::Meta::TypeConstraint,
123but it does provide the same API
124
39b3bc94 125=over 4
126
ecb59493 127=item B<meta>
39b3bc94 128
ecb59493 129=item B<new>
39b3bc94 130
ecb59493 131=item B<name>
132
133=item B<type_constraints>
39b3bc94 134
dabed765 135=item B<parents>
136
39b3bc94 137=item B<constraint>
138
1b58cb9f 139=item B<includes_type>
140
dabed765 141=item B<equals>
142
ecb59493 143=back
39b3bc94 144
ecb59493 145=head2 Overriden methods
146
147=over 4
148
149=item B<check>
150
151=item B<coerce>
152
153=item B<validate>
39b3bc94 154
155=item B<is_a_type_of>
156
157=item B<is_subtype_of>
158
ecb59493 159=back
39b3bc94 160
ecb59493 161=head2 Empty or Stub methods
39b3bc94 162
ecb59493 163These methods tend to not be very relevant in
164the context of a union. Either that or they are
165just difficult to specify and not very useful
166anyway. They are here for completeness.
39b3bc94 167
ecb59493 168=over 4
39b3bc94 169
170=item B<parent>
171
ecb59493 172=item B<coercion>
39b3bc94 173
ecb59493 174=item B<has_coercion>
175
176=item B<message>
177
178=item B<has_message>
39b3bc94 179
0eec94be 180=item B<hand_optimized_type_constraint>
181
182=item B<has_hand_optimized_type_constraint>
183
39b3bc94 184=back
185
186=head1 BUGS
187
188All complex software has bugs lurking in it, and this module is no
189exception. If you find a bug please either email me, or add the bug
190to cpan-RT.
191
192=head1 AUTHOR
193
194Stevan Little E<lt>stevan@iinteractive.comE<gt>
195
39b3bc94 196=head1 COPYRIGHT AND LICENSE
197
778db3ac 198Copyright 2006-2008 by Infinity Interactive, Inc.
39b3bc94 199
200L<http://www.iinteractive.com>
201
202This library is free software; you can redistribute it and/or modify
203it under the same terms as Perl itself.
204
8ee73eeb 205=cut