2 package Moose::Meta::TypeConstraint::Union;
9 our $AUTHORITY = 'cpan:STEVAN';
11 __PACKAGE__->meta->add_attribute('type_constraints' => (
12 accessor => 'type_constraints',
18 my $self = $class->meta->new_object(@_);
22 sub name { join ' | ' => map { $_->name } @{$_[0]->type_constraints} }
25 # this should probably never be used
26 # but we include it here for completeness
29 sub { $self->check($_[0]) };
32 # conform to the TypeConstraint API
38 # not sure what this should actually do here
39 sub coercion { undef }
41 # this should probably be memoized
44 foreach my $type (@{$self->type_constraints}) {
45 return 1 if $type->has_coercion
51 # this feels too simple, and may not always DWIM
52 # correctly, especially in the presence of
53 # close subtype relationships, however it should
54 # work for a fair percentage of the use cases
58 foreach my $type (@{$self->type_constraints}) {
59 if ($type->has_coercion) {
60 my $temp = $type->coerce($value);
61 return $temp if $self->check($temp);
67 sub _compiled_type_constraint {
71 foreach my $type (@{$self->type_constraints}) {
72 return 1 if $type->check($value);
81 $self->_compiled_type_constraint->($value);
88 foreach my $type (@{$self->type_constraints}) {
89 my $err = $type->validate($value);
90 return unless defined $err;
91 $message .= ($message ? ' and ' : '') . $err
94 return ($message . ' in (' . $self->name . ')') ;
98 my ($self, $type_name) = @_;
99 foreach my $type (@{$self->type_constraints}) {
100 return 1 if $type->is_a_type_of($type_name);
106 my ($self, $type_name) = @_;
107 foreach my $type (@{$self->type_constraints}) {
108 return 1 if $type->is_subtype_of($type_name);
121 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
125 This metaclass represents a union of Moose type constraints. More
126 details to be explained later (possibly in a Cookbook::Recipe).
128 This actually used to be part of Moose::Meta::TypeConstraint, but it
129 is now better off in it's own file.
133 This class is not a subclass of Moose::Meta::TypeConstraint,
134 but it does provide the same API
144 =item B<type_constraints>
150 =head2 Overriden methods
160 =item B<is_a_type_of>
162 =item B<is_subtype_of>
166 =head2 Empty or Stub methods
168 These methods tend to not be very relevant in
169 the context of a union. Either that or they are
170 just difficult to specify and not very useful
171 anyway. They are here for completeness.
179 =item B<has_coercion>
189 All complex software has bugs lurking in it, and this module is no
190 exception. If you find a bug please either email me, or add the bug
195 Stevan Little E<lt>stevan@iinteractive.comE<gt>
197 =head1 COPYRIGHT AND LICENSE
199 Copyright 2006, 2007 by Infinity Interactive, Inc.
201 L<http://www.iinteractive.com>
203 This library is free software; you can redistribute it and/or modify
204 it under the same terms as Perl itself.