1 package Moose::Meta::TypeConstraint::Enum;
7 use Moose::Util::TypeConstraints ();
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Moose::Meta::TypeConstraint';
14 __PACKAGE__->meta->add_attribute('values' => (
19 my ( $class, %args ) = @_;
21 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
23 my $self = $class->meta->new_object(%args);
25 $self->compile_type_constraint()
26 unless $self->_has_compiled_type_constraint;
32 my ( $self, $type_or_name ) = @_;
34 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
36 return unless $other->isa(__PACKAGE__);
38 my @self_values = sort @{ $self->values };
39 my @other_values = sort @{ $other->values };
41 return unless @self_values == @other_values;
43 while ( @self_values ) {
44 my $value = shift @self_values;
45 my $other_value = shift @other_values;
47 return unless $value eq $other_value;
56 my %values = map { $_ => undef } @{ $self->values };
58 return sub { exists $values{$_[0]} };
61 sub _compile_hand_optimized_type_constraint {
64 my %values = map { $_ => undef } @{ $self->values };
66 sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} };
77 Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values.
97 All complex software has bugs lurking in it, and this module is no
98 exception. If you find a bug please either email me, or add the bug
103 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
105 =head1 COPYRIGHT AND LICENSE
107 Copyright 2006-2008 by Infinity Interactive, Inc.
109 L<http://www.iinteractive.com>
111 This library is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself.