1 package Moose::Meta::TypeConstraint::Enum;
8 use Moose::Util::TypeConstraints ();
10 use base 'Moose::Meta::TypeConstraint';
12 __PACKAGE__->meta->add_attribute('values' => (
14 Class::MOP::_definition_context(),
17 __PACKAGE__->meta->add_attribute('_inline_var_name' => (
18 accessor => '_inline_var_name',
19 Class::MOP::_definition_context(),
26 return 'defined(' . $val . ') '
27 . '&& !ref(' . $val . ') '
28 . '&& $' . $self->_inline_var_name . '{' . $val . '}';
34 my ( $class, %args ) = @_;
36 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
37 $args{inlined} = $inliner;
39 if ( scalar @{ $args{values} } < 1 ) {
41 Moose->throw_error("You must have at least one value to enumerate through");
44 for (@{ $args{values} }) {
47 Moose->throw_error("Enum values must be strings, not undef");
51 Moose->throw_error("Enum values must be strings, not '$_'");
55 my %values = map { $_ => 1 } @{ $args{values} };
56 $args{constraint} = sub { $values{ $_[0] } };
58 my $var_name = 'enums' . $var_suffix++;;
59 $args{_inline_var_name} = $var_name;
60 $args{inline_environment} = { '%' . $var_name => \%values };
62 my $self = $class->SUPER::new(\%args);
64 $self->compile_type_constraint()
65 unless $self->_has_compiled_type_constraint;
71 my ( $self, $type_or_name ) = @_;
73 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
75 return unless $other->isa(__PACKAGE__);
77 my @self_values = sort @{ $self->values };
78 my @other_values = sort @{ $other->values };
80 return unless @self_values == @other_values;
82 while ( @self_values ) {
83 my $value = shift @self_values;
84 my $other_value = shift @other_values;
86 return unless $value eq $other_value;
95 my %values = map { $_ => undef } @{ $self->values };
97 return sub { exists $values{$_[0]} };
100 sub create_child_type {
101 my ($self, @args) = @_;
102 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
107 # ABSTRACT: Type constraint for enumerated values.
115 This class represents type constraints based on an enumerated list of
120 C<Moose::Meta::TypeConstraint::Enum> is a subclass of
121 L<Moose::Meta::TypeConstraint>.
127 =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
129 This creates a new enum type constraint based on the given
132 It takes the same options as its parent, with several
133 exceptions. First, it requires an additional option, C<values>. This
134 should be an array reference containing a list of valid string
135 values. Second, it automatically sets the parent to the C<Str> type.
137 Finally, it ignores any provided C<constraint> option. The constraint
138 is generated automatically based on the provided C<values>.
140 =item B<< $constraint->values >>
142 Returns the array reference of acceptable values provided to the
145 =item B<< $constraint->create_child_type >>
147 This returns a new L<Moose::Meta::TypeConstraint> object with the type
150 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
157 See L<Moose/BUGS> for details on reporting bugs.