2 package Moose::Meta::TypeConstraint::Union;
10 __PACKAGE__->meta->add_attribute('type_constraints' => (
11 accessor => 'type_constraints',
17 my $self = $class->meta->new_object(@_);
21 sub name { join ' | ' => map { $_->name } @{$_[0]->type_constraints} }
24 # this should probably never be used
25 # but we include it here for completeness
28 sub { $self->check($_[0]) };
31 # conform to the TypeConstraint API
37 # not sure what this should actually do here
38 sub coercion { undef }
40 # this should probably be memoized
43 foreach my $type (@{$self->type_constraints}) {
44 return 1 if $type->has_coercion
50 # this feels too simple, and may not always DWIM
51 # correctly, especially in the presence of
52 # close subtype relationships, however it should
53 # work for a fair percentage of the use cases
57 foreach my $type (@{$self->type_constraints}) {
58 if ($type->has_coercion) {
59 my $temp = $type->coerce($value);
60 return $temp if $self->check($temp);
66 sub _compiled_type_constraint {
70 foreach my $type (@{$self->type_constraints}) {
71 return 1 if $type->check($value);
80 $self->_compiled_type_constraint->($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);
120 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
124 This metaclass represents a union of Moose type constraints. More
125 details to be explained later (possibly in a Cookbook::Recipe).
127 This actually used to be part of Moose::Meta::TypeConstraint, but it
128 is now better off in it's own file.
132 This class is not a subclass of Moose::Meta::TypeConstraint,
133 but it does provide the same API
143 =item B<type_constraints>
149 =head2 Overriden methods
159 =item B<is_a_type_of>
161 =item B<is_subtype_of>
165 =head2 Empty or Stub methods
167 These methods tend to not be very relevant in
168 the context of a union. Either that or they are
169 just difficult to specify and not very useful
170 anyway. They are here for completeness.
178 =item B<has_coercion>
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 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
198 =head1 COPYRIGHT AND LICENSE
200 Copyright 2006, 2007 by Infinity Interactive, Inc.
202 L<http://www.iinteractive.com>
204 This library is free software; you can redistribute it and/or modify
205 it under the same terms as Perl itself.