2 package Moose::Meta::TypeConstraint::Union;
8 use Moose::Meta::TypeCoercion::Union;
10 our $VERSION = '0.54';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::TypeConstraint';
15 __PACKAGE__->meta->add_attribute('type_constraints' => (
16 accessor => 'type_constraints',
21 my ($class, %options) = @_;
22 my $self = $class->SUPER::new(
23 name => (join ' | ' => map { $_->name } @{$options{type_constraints}}),
26 hand_optimized_type_constraint => undef,
27 compiled_type_constraint => sub {
29 foreach my $type (@{$options{type_constraints}}) {
30 return 1 if $type->check($value);
36 $self->_set_constraint(sub { $self->check($_[0]) });
37 $self->coercion(Moose::Meta::TypeCoercion::Union->new(
38 type_constraint => $self
44 my ( $self, $type_or_name ) = @_;
46 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
48 return unless $other->isa(__PACKAGE__);
50 my @self_constraints = @{ $self->type_constraints };
51 my @other_constraints = @{ $other->type_constraints };
53 return unless @self_constraints == @other_constraints;
55 # FIXME presort type constraints for efficiency?
56 constraint: foreach my $constraint ( @self_constraints ) {
57 for ( my $i = 0; $i < @other_constraints; $i++ ) {
58 if ( $constraint->equals($other_constraints[$i]) ) {
59 splice @other_constraints, $i, 1;
65 return @other_constraints == 0;
70 $self->type_constraints;
74 my ($self, $value) = @_;
76 foreach my $type (@{$self->type_constraints}) {
77 my $err = $type->validate($value);
78 return unless defined $err;
79 $message .= ($message ? ' and ' : '') . $err
82 return ($message . ' in (' . $self->name . ')') ;
86 my ($self, $type_name) = @_;
87 foreach my $type (@{$self->type_constraints}) {
88 return 1 if $type->is_a_type_of($type_name);
94 my ($self, $type_name) = @_;
95 foreach my $type (@{$self->type_constraints}) {
96 return 1 if $type->is_subtype_of($type_name);
109 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
113 This metaclass represents a union of Moose type constraints. More
114 details to be explained later (possibly in a Cookbook::Recipe).
116 This actually used to be part of Moose::Meta::TypeConstraint, but it
117 is now better off in it's own file.
121 This class is not a subclass of Moose::Meta::TypeConstraint,
122 but it does provide the same API
132 =item B<type_constraints>
138 =item B<includes_type>
144 =head2 Overriden methods
154 =item B<is_a_type_of>
156 =item B<is_subtype_of>
160 =head2 Empty or Stub methods
162 These methods tend to not be very relevant in
163 the context of a union. Either that or they are
164 just difficult to specify and not very useful
165 anyway. They are here for completeness.
173 =item B<has_coercion>
179 =item B<hand_optimized_type_constraint>
181 =item B<has_hand_optimized_type_constraint>
187 All complex software has bugs lurking in it, and this module is no
188 exception. If you find a bug please either email me, or add the bug
193 Stevan Little E<lt>stevan@iinteractive.comE<gt>
195 =head1 COPYRIGHT AND LICENSE
197 Copyright 2006-2008 by Infinity Interactive, Inc.
199 L<http://www.iinteractive.com>
201 This library is free software; you can redistribute it and/or modify
202 it under the same terms as Perl itself.