2 package Moose::Meta::TypeConstraint::Union;
8 use Moose::Meta::TypeCoercion::Union;
10 our $VERSION = '0.72';
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 Moose type constraints. More
148 details to be explained later (possibly in a Cookbook recipe).
150 This actually used to be part of Moose::Meta::TypeConstraint, but it
151 is now better off in it's own file.
155 This class is not a subclass of Moose::Meta::TypeConstraint,
156 but it does provide the same API
166 =item B<type_constraints>
172 =item B<includes_type>
178 =head2 Overridden methods
188 =item B<is_a_type_of>
190 =item B<is_subtype_of>
194 =head2 Empty or Stub methods
196 These methods tend to not be very relevant in
197 the context of a union. Either that or they are
198 just difficult to specify and not very useful
199 anyway. They are here for completeness.
207 =item B<has_coercion>
213 =item B<hand_optimized_type_constraint>
215 =item B<has_hand_optimized_type_constraint>
217 =item B<create_child_type>
223 All complex software has bugs lurking in it, and this module is no
224 exception. If you find a bug please either email me, or add the bug
229 Stevan Little E<lt>stevan@iinteractive.comE<gt>
231 =head1 COPYRIGHT AND LICENSE
233 Copyright 2006-2009 by Infinity Interactive, Inc.
235 L<http://www.iinteractive.com>
237 This library is free software; you can redistribute it and/or modify
238 it under the same terms as Perl itself.