Commit | Line | Data |
8ee73eeb |
1 | |
2 | package Moose::Meta::TypeConstraint::Union; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | use metaclass; |
7 | |
3726f905 |
8 | use Moose::Meta::TypeCoercion::Union; |
9 | |
3d24a30e |
10 | our $VERSION = '0.56'; |
75b95414 |
11 | $VERSION = eval $VERSION; |
d44714be |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
8ee73eeb |
13 | |
d67145ed |
14 | use base 'Moose::Meta::TypeConstraint'; |
15 | |
8ee73eeb |
16 | __PACKAGE__->meta->add_attribute('type_constraints' => ( |
17 | accessor => 'type_constraints', |
18 | default => sub { [] } |
19 | )); |
20 | |
21 | sub new { |
3726f905 |
22 | my ($class, %options) = @_; |
23 | my $self = $class->SUPER::new( |
24 | name => (join ' | ' => map { $_->name } @{$options{type_constraints}}), |
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 |
44 | sub 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 | |
69 | sub parents { |
70 | my $self = shift; |
71 | $self->type_constraints; |
72 | } |
73 | |
8ee73eeb |
74 | sub 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 | |
86 | sub 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 | |
94 | sub 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 | |
102 | 1; |
103 | |
104 | __END__ |
105 | |
106 | =pod |
107 | |
39b3bc94 |
108 | =head1 NAME |
109 | |
ecb59493 |
110 | Moose::Meta::TypeConstraint::Union - A union of Moose type constraints |
39b3bc94 |
111 | |
112 | =head1 DESCRIPTION |
113 | |
ecb59493 |
114 | This metaclass represents a union of Moose type constraints. More |
5cfe3805 |
115 | details to be explained later (possibly in a Cookbook recipe). |
ecb59493 |
116 | |
117 | This actually used to be part of Moose::Meta::TypeConstraint, but it |
118 | is now better off in it's own file. |
119 | |
39b3bc94 |
120 | =head1 METHODS |
121 | |
ecb59493 |
122 | This class is not a subclass of Moose::Meta::TypeConstraint, |
123 | but 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 |
163 | These methods tend to not be very relevant in |
164 | the context of a union. Either that or they are |
165 | just difficult to specify and not very useful |
166 | anyway. 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 | |
188 | All complex software has bugs lurking in it, and this module is no |
189 | exception. If you find a bug please either email me, or add the bug |
190 | to cpan-RT. |
191 | |
192 | =head1 AUTHOR |
193 | |
194 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
195 | |
39b3bc94 |
196 | =head1 COPYRIGHT AND LICENSE |
197 | |
778db3ac |
198 | Copyright 2006-2008 by Infinity Interactive, Inc. |
39b3bc94 |
199 | |
200 | L<http://www.iinteractive.com> |
201 | |
202 | This library is free software; you can redistribute it and/or modify |
203 | it under the same terms as Perl itself. |
204 | |
8ee73eeb |
205 | =cut |