1 package Moose::Meta::TypeConstraint::Enum;
8 use Moose::Util::TypeConstraints ();
10 use base 'Moose::Meta::TypeConstraint';
12 __PACKAGE__->meta->add_attribute('values' => (
16 __PACKAGE__->meta->add_attribute('_inline_var_name' => (
17 accessor => '_inline_var_name',
24 return 'defined(' . $val . ') '
25 . '&& !ref(' . $val . ') '
26 . '&& $' . $self->_inline_var_name . '{' . $val . '}';
29 # a quadrillion enums ought to be enough for any app
30 my $var_suffix = '000000000000000000';
33 my ( $class, %args ) = @_;
35 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
36 $args{inlined} = $inliner;
38 if ( scalar @{ $args{values} } < 2 ) {
40 Moose->throw_error("You must have at least two values to enumerate through");
43 for (@{ $args{values} }) {
46 Moose->throw_error("Enum values must be strings, not undef");
50 Moose->throw_error("Enum values must be strings, not '$_'");
54 my %values = map { $_ => 1 } @{ $args{values} };
55 $args{constraint} = sub { $values{ $_[0] } };
57 my $var_name = 'enums' . $var_suffix++;;
58 $args{_inline_var_name} = $var_name;
59 $args{inline_environment} = { '%' . $var_name => \%values };
61 my $self = $class->_new(\%args);
63 $self->compile_type_constraint()
64 unless $self->_has_compiled_type_constraint;
70 my ( $self, $type_or_name ) = @_;
72 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
74 return unless $other->isa(__PACKAGE__);
76 my @self_values = sort @{ $self->values };
77 my @other_values = sort @{ $other->values };
79 return unless @self_values == @other_values;
81 while ( @self_values ) {
82 my $value = shift @self_values;
83 my $other_value = shift @other_values;
85 return unless $value eq $other_value;
94 my %values = map { $_ => undef } @{ $self->values };
96 return sub { exists $values{$_[0]} };
99 sub create_child_type {
100 my ($self, @args) = @_;
101 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
106 # ABSTRACT: Type constraint for enumerated values.
114 This class represents type constraints based on an enumerated list of
119 C<Moose::Meta::TypeConstraint::Enum> is a subclass of
120 L<Moose::Meta::TypeConstraint>.
126 =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
128 This creates a new enum type constraint based on the given
131 It takes the same options as its parent, with several
132 exceptions. First, it requires an additional option, C<values>. This
133 should be an array reference containing a list of valid string
134 values. Second, it automatically sets the parent to the C<Str> type.
136 Finally, it ignores any provided C<constraint> option. The constraint
137 is generated automatically based on the provided C<values>.
139 =item B<< $constraint->values >>
141 Returns the array reference of acceptable values provided to the
144 =item B<< $constraint->create_child_type >>
146 This returns a new L<Moose::Meta::TypeConstraint> object with the type
149 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
156 See L<Moose/BUGS> for details on reporting bugs.