1 package Moose::Meta::TypeConstraint::Enum;
7 use Moose::Util::TypeConstraints ();
9 our $VERSION = '0.62_01';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::TypeConstraint';
15 __PACKAGE__->meta->add_attribute('values' => (
20 my ( $class, %args ) = @_;
22 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
24 my $self = $class->_new(\%args);
26 $self->compile_type_constraint()
27 unless $self->_has_compiled_type_constraint;
33 my ( $self, $type_or_name ) = @_;
35 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
37 return unless $other->isa(__PACKAGE__);
39 my @self_values = sort @{ $self->values };
40 my @other_values = sort @{ $other->values };
42 return unless @self_values == @other_values;
44 while ( @self_values ) {
45 my $value = shift @self_values;
46 my $other_value = shift @other_values;
48 return unless $value eq $other_value;
57 my %values = map { $_ => undef } @{ $self->values };
59 return sub { exists $values{$_[0]} };
62 sub _compile_hand_optimized_type_constraint {
65 my %values = map { $_ => undef } @{ $self->values };
67 sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} };
70 sub create_child_type {
71 my ($self, @args) = @_;
72 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
83 Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values.
99 =item B<create_child_type>
105 All complex software has bugs lurking in it, and this module is no
106 exception. If you find a bug please either email me, or add the bug
111 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
113 =head1 COPYRIGHT AND LICENSE
115 Copyright 2006-2008 by Infinity Interactive, Inc.
117 L<http://www.iinteractive.com>
119 This library is free software; you can redistribute it and/or modify
120 it under the same terms as Perl itself.