2 package Moose::Meta::TypeConstraint::Union;
9 our $AUTHORITY = 'cpan:STEVAN';
12 # this is not really correct, but
13 # I think it shoul be here anyway.
14 # In truth, this should implement
15 # the same abstract base/interface
18 use base 'Moose::Meta::TypeConstraint';
20 __PACKAGE__->meta->add_attribute('type_constraints' => (
21 accessor => 'type_constraints',
27 my $self = $class->meta->new_object(@_);
31 sub name { join ' | ' => map { $_->name } @{$_[0]->type_constraints} }
34 # this should probably never be used
35 # but we include it here for completeness
38 sub { $self->check($_[0]) };
41 # conform to the TypeConstraint API
47 # not sure what this should actually do here
48 sub coercion { undef }
50 # this should probably be memoized
53 foreach my $type (@{$self->type_constraints}) {
54 return 1 if $type->has_coercion
60 # this feels too simple, and may not always DWIM
61 # correctly, especially in the presence of
62 # close subtype relationships, however it should
63 # work for a fair percentage of the use cases
67 foreach my $type (@{$self->type_constraints}) {
68 if ($type->has_coercion) {
69 my $temp = $type->coerce($value);
70 return $temp if $self->check($temp);
76 sub _compiled_type_constraint {
80 foreach my $type (@{$self->type_constraints}) {
81 return 1 if $type->check($value);
90 $self->_compiled_type_constraint->($value);
97 foreach my $type (@{$self->type_constraints}) {
98 my $err = $type->validate($value);
99 return unless defined $err;
100 $message .= ($message ? ' and ' : '') . $err
103 return ($message . ' in (' . $self->name . ')') ;
107 my ($self, $type_name) = @_;
108 foreach my $type (@{$self->type_constraints}) {
109 return 1 if $type->is_a_type_of($type_name);
115 my ($self, $type_name) = @_;
116 foreach my $type (@{$self->type_constraints}) {
117 return 1 if $type->is_subtype_of($type_name);
122 ## hand optimized constraints
125 # it will just use all the hand optimized
126 # type constraints from it's list of type
127 # constraints automatically, but there is
128 # no simple way to optimize it even more
129 # (without B::Deparse or something). So
133 sub has_hand_optimized_type_constraint { 0 }
134 sub hand_optimized_type_constraint { undef }
144 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
148 This metaclass represents a union of Moose type constraints. More
149 details to be explained later (possibly in a Cookbook::Recipe).
151 This actually used to be part of Moose::Meta::TypeConstraint, but it
152 is now better off in it's own file.
156 This class is not a subclass of Moose::Meta::TypeConstraint,
157 but it does provide the same API
167 =item B<type_constraints>
173 =head2 Overriden methods
183 =item B<is_a_type_of>
185 =item B<is_subtype_of>
189 =head2 Empty or Stub methods
191 These methods tend to not be very relevant in
192 the context of a union. Either that or they are
193 just difficult to specify and not very useful
194 anyway. They are here for completeness.
202 =item B<has_coercion>
208 =item B<hand_optimized_type_constraint>
210 =item B<has_hand_optimized_type_constraint>
216 All complex software has bugs lurking in it, and this module is no
217 exception. If you find a bug please either email me, or add the bug
222 Stevan Little E<lt>stevan@iinteractive.comE<gt>
224 =head1 COPYRIGHT AND LICENSE
226 Copyright 2006, 2007 by Infinity Interactive, Inc.
228 L<http://www.iinteractive.com>
230 This library is free software; you can redistribute it and/or modify
231 it under the same terms as Perl itself.