2 package Moose::Meta::TypeConstraint::Union;
8 use Moose::Meta::TypeCoercion::Union;
10 our $VERSION = '0.93';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Moose::Meta::TypeConstraint';
16 __PACKAGE__->meta->add_attribute('type_constraints' => (
17 accessor => 'type_constraints',
22 my ($class, %options) = @_;
24 my $name = join '|' => sort { $a cmp $b }
25 map { $_->name } @{ $options{type_constraints} };
27 my $self = $class->SUPER::new(
32 $self->_set_constraint(sub { $self->check($_[0]) });
33 $self->coercion(Moose::Meta::TypeCoercion::Union->new(
34 type_constraint => $self
39 sub _actually_compile_type_constraint {
42 my @constraints = @{ $self->type_constraints };
46 foreach my $type (@constraints) {
47 return 1 if $type->check($value);
55 my ( $self, $type_or_name ) = @_;
57 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
59 return unless $other->isa(__PACKAGE__);
61 my @self_constraints = @{ $self->type_constraints };
62 my @other_constraints = @{ $other->type_constraints };
64 return unless @self_constraints == @other_constraints;
66 # FIXME presort type constraints for efficiency?
67 constraint: foreach my $constraint ( @self_constraints ) {
68 for ( my $i = 0; $i < @other_constraints; $i++ ) {
69 if ( $constraint->equals($other_constraints[$i]) ) {
70 splice @other_constraints, $i, 1;
76 return @other_constraints == 0;
81 $self->type_constraints;
85 my ($self, $value) = @_;
87 foreach my $type (@{$self->type_constraints}) {
88 my $err = $type->validate($value);
89 return unless defined $err;
90 $message .= ($message ? ' and ' : '') . $err
93 return ($message . ' in (' . $self->name . ')') ;
97 my ($self, $type_name) = @_;
98 foreach my $type (@{$self->type_constraints}) {
99 return 1 if $type->is_a_type_of($type_name);
105 my ($self, $type_name) = @_;
106 foreach my $type (@{$self->type_constraints}) {
107 return 1 if $type->is_subtype_of($type_name);
112 sub create_child_type {
113 my ( $self, %opts ) = @_;
116 = Moose::Meta::TypeConstraint->new( %opts, parent => $self );
118 # if we have a type constraint union, and no
119 # type check, this means we are just aliasing
120 # the union constraint, which means we need to
121 # handle this differently.
123 if ( not( defined $opts{constraint} )
124 && $self->has_coercion ) {
125 $constraint->coercion(
126 Moose::Meta::TypeCoercion::Union->new(
127 type_constraint => $self,
143 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
147 This metaclass represents a union of type constraints. A union takes
148 multiple type constraints, and is true if any one of its member
153 C<Moose::Meta::TypeConstraint::Union> is a subclass of
154 L<Moose::Meta::TypeConstraint>.
158 =item B<< Moose::Meta::TypeConstraint::Union->new(%options) >>
160 This creates a new class type constraint based on the given
163 It takes the same options as its parent. It also requires an
164 additional option, C<type_constraints>. This is an array reference
165 containing the L<Moose::Meta::TypeConstraint> objects that are the
166 members of the union type. The C<name> option defaults to the names
167 all of these member types sorted and then joined by a pipe (|).
169 The constructor sets the implementation of the constraint so that is
170 simply calls C<check> on the newly created object.
172 Finally, the constructor also makes sure that the object's C<coercion>
173 attribute is a L<Moose::Meta::TypeCoercion::Union> object.
175 =item B<< $constraint->type_constraints >>
177 This returns the array reference of C<type_constraints> provided to
180 =item B<< $constraint->parents >>
182 This returns the same constraint as the C<type_constraints> method.
184 =item B<< $constraint->check($value) >>
186 =item B<< $constraint->validate($value) >>
188 These two methods simply call the relevant method on each of the
189 member type constraints in the union. If any type accepts the value,
192 With C<validate> the error message returned includes all of the error
193 messages returned by the member type constraints.
195 =item B<< $constraint->equals($type_name_or_object) >>
197 A type is considered equal if it is also a union type, and the two
198 unions have the same member types.
200 =item B<< $constraint->is_a_type_of($type_name_or_object) >>
202 This returns true if any of the member type constraints return true
203 for the C<is_a_type_of> method.
205 =item B<< $constraint->is_subtype_of >>
207 This returns true if any of the member type constraints return true
208 for the C<is_a_subtype_of> method.
210 =item B<< $constraint->create_child_type(%options) >>
212 This returns a new L<Moose::Meta::TypeConstraint> object with the type
219 See L<Moose/BUGS> for details on reporting bugs.
223 Stevan Little E<lt>stevan@iinteractive.comE<gt>
225 =head1 COPYRIGHT AND LICENSE
227 Copyright 2006-2010 by Infinity Interactive, Inc.
229 L<http://www.iinteractive.com>
231 This library is free software; you can redistribute it and/or modify
232 it under the same terms as Perl itself.