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);
113 ## hand optimized constraints
116 # it will just use all the hand optimized
117 # type constraints from it's list of type
118 # constraints automatically, but there is
119 # no simple way to optimize it even more
120 # (without B::Deparse or something). So
124 sub has_hand_optimized_type_constraint { 0 }
125 sub hand_optimized_type_constraint { undef }
135 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
139 This metaclass represents a union of Moose type constraints. More
140 details to be explained later (possibly in a Cookbook::Recipe).
142 This actually used to be part of Moose::Meta::TypeConstraint, but it
143 is now better off in it's own file.
147 This class is not a subclass of Moose::Meta::TypeConstraint,
148 but it does provide the same API
158 =item B<type_constraints>
164 =head2 Overriden methods
174 =item B<is_a_type_of>
176 =item B<is_subtype_of>
180 =head2 Empty or Stub methods
182 These methods tend to not be very relevant in
183 the context of a union. Either that or they are
184 just difficult to specify and not very useful
185 anyway. They are here for completeness.
193 =item B<has_coercion>
203 All complex software has bugs lurking in it, and this module is no
204 exception. If you find a bug please either email me, or add the bug
209 Stevan Little E<lt>stevan@iinteractive.comE<gt>
211 =head1 COPYRIGHT AND LICENSE
213 Copyright 2006, 2007 by Infinity Interactive, Inc.
215 L<http://www.iinteractive.com>
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself.