2 package Moose::Meta::TypeConstraint::Union;
8 use Moose::Meta::TypeCoercion::Union;
10 our $VERSION = '0.06';
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, $value) = @_;
46 foreach my $type (@{$self->type_constraints}) {
47 my $err = $type->validate($value);
48 return unless defined $err;
49 $message .= ($message ? ' and ' : '') . $err
52 return ($message . ' in (' . $self->name . ')') ;
56 my ($self, $type_name) = @_;
57 foreach my $type (@{$self->type_constraints}) {
58 return 1 if $type->is_a_type_of($type_name);
64 my ($self, $type_name) = @_;
65 foreach my $type (@{$self->type_constraints}) {
66 return 1 if $type->is_subtype_of($type_name);
79 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
83 This metaclass represents a union of Moose type constraints. More
84 details to be explained later (possibly in a Cookbook::Recipe).
86 This actually used to be part of Moose::Meta::TypeConstraint, but it
87 is now better off in it's own file.
91 This class is not a subclass of Moose::Meta::TypeConstraint,
92 but it does provide the same API
102 =item B<type_constraints>
108 =head2 Overriden methods
118 =item B<is_a_type_of>
120 =item B<is_subtype_of>
124 =head2 Empty or Stub methods
126 These methods tend to not be very relevant in
127 the context of a union. Either that or they are
128 just difficult to specify and not very useful
129 anyway. They are here for completeness.
137 =item B<has_coercion>
143 =item B<hand_optimized_type_constraint>
145 =item B<has_hand_optimized_type_constraint>
151 All complex software has bugs lurking in it, and this module is no
152 exception. If you find a bug please either email me, or add the bug
157 Stevan Little E<lt>stevan@iinteractive.comE<gt>
159 =head1 COPYRIGHT AND LICENSE
161 Copyright 2006-2008 by Infinity Interactive, Inc.
163 L<http://www.iinteractive.com>
165 This library is free software; you can redistribute it and/or modify
166 it under the same terms as Perl itself.