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);
72 my ($self, $type) = @_;
77 for my $type (@{ $self->type_constraints }) {
78 return 1 if $subtype->is_a_type_of($type);
84 if ($type->isa('Moose::Meta::TypeConstraint::Union')) {
85 for my $t (@{ $type->type_constraints }) {
86 return 0 unless $has_type->($t);
90 return 0 unless $has_type->($type);
104 Moose::Meta::TypeConstraint::Union - A union of Moose type constraints
108 This metaclass represents a union of Moose type constraints. More
109 details to be explained later (possibly in a Cookbook::Recipe).
111 This actually used to be part of Moose::Meta::TypeConstraint, but it
112 is now better off in it's own file.
116 This class is not a subclass of Moose::Meta::TypeConstraint,
117 but it does provide the same API
127 =item B<type_constraints>
131 =item B<includes_type>
135 =head2 Overriden methods
145 =item B<is_a_type_of>
147 =item B<is_subtype_of>
151 =head2 Empty or Stub methods
153 These methods tend to not be very relevant in
154 the context of a union. Either that or they are
155 just difficult to specify and not very useful
156 anyway. They are here for completeness.
164 =item B<has_coercion>
170 =item B<hand_optimized_type_constraint>
172 =item B<has_hand_optimized_type_constraint>
178 All complex software has bugs lurking in it, and this module is no
179 exception. If you find a bug please either email me, or add the bug
184 Stevan Little E<lt>stevan@iinteractive.comE<gt>
186 =head1 COPYRIGHT AND LICENSE
188 Copyright 2006-2008 by Infinity Interactive, Inc.
190 L<http://www.iinteractive.com>
192 This library is free software; you can redistribute it and/or modify
193 it under the same terms as Perl itself.