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