1 package Moose::Meta::TypeConstraint::Enum;
7 use Moose::Util::TypeConstraints ();
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 if ( scalar @{ $args{values} } < 2 ) {
26 Moose->throw_error("You must have at least two values to enumerate through");
29 for (@{ $args{values} }) {
32 Moose->throw_error("Enum values must be strings, not undef");
36 Moose->throw_error("Enum values must be strings, not '$_'");
40 my $self = $class->_new(\%args);
42 $self->compile_type_constraint()
43 unless $self->_has_compiled_type_constraint;
49 my ( $self, $type_or_name ) = @_;
51 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
53 return unless $other->isa(__PACKAGE__);
55 my @self_values = sort @{ $self->values };
56 my @other_values = sort @{ $other->values };
58 return unless @self_values == @other_values;
60 while ( @self_values ) {
61 my $value = shift @self_values;
62 my $other_value = shift @other_values;
64 return unless $value eq $other_value;
73 my %values = map { $_ => undef } @{ $self->values };
75 return sub { exists $values{$_[0]} };
78 sub _compile_hand_optimized_type_constraint {
81 my %values = map { $_ => undef } @{ $self->values };
83 sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} };
86 sub create_child_type {
87 my ($self, @args) = @_;
88 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
99 Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values.
103 This class represents type constraints based on an enumerated list of
108 C<Moose::Meta::TypeConstraint::Enum> is a subclass of
109 L<Moose::Meta::TypeConstraint>.
115 =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
117 This creates a new enum type constraint based on the given
120 It takes the same options as its parent, with several
121 exceptions. First, it requires an additional option, C<values>. This
122 should be an array reference containing a list of valid string
123 values. Second, it automatically sets the parent to the C<Str> type.
125 Finally, it ignores any provided C<constraint> option. The constraint
126 is generated automatically based on the provided C<values>.
128 =item B<< $constraint->values >>
130 Returns the array reference of acceptable values provided to the
133 =item B<< $constraint->create_child_type >>
135 This returns a new L<Moose::Meta::TypeConstraint> object with the type
138 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
145 See L<Moose/BUGS> for details on reporting bugs.
149 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
151 =head1 COPYRIGHT AND LICENSE
153 Copyright 2006-2010 by Infinity Interactive, Inc.
155 L<http://www.iinteractive.com>
157 This library is free software; you can redistribute it and/or modify
158 it under the same terms as Perl itself.