2 package Moose::Meta::TypeConstraint::Union;
8 use Moose::Meta::TypeCoercion::Union;
10 our $VERSION = '0.55_03';
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) = @_;
23 my $self = $class->SUPER::new(
24 name => (join ' | ' => map { $_->name } @{$options{type_constraints}}),
27 hand_optimized_type_constraint => undef,
28 compiled_type_constraint => sub {
30 foreach my $type (@{$options{type_constraints}}) {
31 return 1 if $type->check($value);
37 $self->_set_constraint(sub { $self->check($_[0]) });
38 $self->coercion(Moose::Meta::TypeCoercion::Union->new(
39 type_constraint => $self
45 my ( $self, $type_or_name ) = @_;
47 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
49 return unless $other->isa(__PACKAGE__);
51 my @self_constraints = @{ $self->type_constraints };
52 my @other_constraints = @{ $other->type_constraints };
54 return unless @self_constraints == @other_constraints;
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;
66 return @other_constraints == 0;
71 $self->type_constraints;
75 my ($self, $value) = @_;
77 foreach my $type (@{$self->type_constraints}) {
78 my $err = $type->validate($value);
79 return unless defined $err;
80 $message .= ($message ? ' and ' : '') . $err
83 return ($message . ' in (' . $self->name . ')') ;
87 my ($self, $type_name) = @_;
88 foreach my $type (@{$self->type_constraints}) {
89 return 1 if $type->is_a_type_of($type_name);
95 my ($self, $type_name) = @_;
96 foreach my $type (@{$self->type_constraints}) {
97 return 1 if $type->is_subtype_of($type_name);
110 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
114 This metaclass represents a union of Moose type constraints. More
115 details to be explained later (possibly in a Cookbook recipe).
117 This actually used to be part of Moose::Meta::TypeConstraint, but it
118 is now better off in it's own file.
122 This class is not a subclass of Moose::Meta::TypeConstraint,
123 but it does provide the same API
133 =item B<type_constraints>
139 =item B<includes_type>
145 =head2 Overriden methods
155 =item B<is_a_type_of>
157 =item B<is_subtype_of>
161 =head2 Empty or Stub methods
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.
174 =item B<has_coercion>
180 =item B<hand_optimized_type_constraint>
182 =item B<has_hand_optimized_type_constraint>
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
194 Stevan Little E<lt>stevan@iinteractive.comE<gt>
196 =head1 COPYRIGHT AND LICENSE
198 Copyright 2006-2008 by Infinity Interactive, Inc.
200 L<http://www.iinteractive.com>
202 This library is free software; you can redistribute it and/or modify
203 it under the same terms as Perl itself.