Enum needs to check that the value is not a ref
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Enum.pm
CommitLineData
dabed765 1package Moose::Meta::TypeConstraint::Enum;
2
3use strict;
4use warnings;
5use metaclass;
6
964294c1 7use B;
4078709c 8use Moose::Util::TypeConstraints ();
9
dabed765 10use base 'Moose::Meta::TypeConstraint';
11
12__PACKAGE__->meta->add_attribute('values' => (
4078709c 13 accessor => 'values',
dabed765 14));
15
964294c1 16our %ENUMS;
17
18my $inliner = sub {
19 my $self = shift;
20 my $val = shift;
21
22 my $name = $self->name();
23 $ENUMS{$name} ||= { map { $_ => 1 } @{ $self->values() } };
24
25 return
883e299e 26 "defined $val"
27 . "&& ! ref $val" . '&& $'
964294c1 28 . __PACKAGE__
29 . '::ENUMS{'
30 . B::perlstring($name)
31 . "}{ $val }";
32};
33
dabed765 34sub new {
35 my ( $class, %args ) = @_;
36
37 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
964294c1 38 $args{inlined} = $inliner;
dabed765 39
f6af1028 40 if ( scalar @{ $args{values} } < 2 ) {
41 require Moose;
42 Moose->throw_error("You must have at least two values to enumerate through");
43 }
44
45 for (@{ $args{values} }) {
46 if (!defined($_)) {
47 require Moose;
48 Moose->throw_error("Enum values must be strings, not undef");
49 }
50 elsif (ref($_)) {
51 require Moose;
52 Moose->throw_error("Enum values must be strings, not '$_'");
53 }
54 }
55
964294c1 56 my %values = map { $_ => 1 } @{ $args{values} };
57 $args{constraint} = sub { $values{ $_[0] } };
58
1b8d1399 59 my $self = $class->_new(\%args);
dabed765 60
61 $self->compile_type_constraint()
62 unless $self->_has_compiled_type_constraint;
63
64 return $self;
65}
66
67sub equals {
68 my ( $self, $type_or_name ) = @_;
69
70 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
71
72 return unless $other->isa(__PACKAGE__);
73
74 my @self_values = sort @{ $self->values };
75 my @other_values = sort @{ $other->values };
76
77 return unless @self_values == @other_values;
78
79 while ( @self_values ) {
80 my $value = shift @self_values;
81 my $other_value = shift @other_values;
82
83 return unless $value eq $other_value;
84 }
85
86 return 1;
87}
88
89sub constraint {
90 my $self = shift;
91
92 my %values = map { $_ => undef } @{ $self->values };
93
94 return sub { exists $values{$_[0]} };
95}
96
97sub _compile_hand_optimized_type_constraint {
98 my $self = shift;
99
100 my %values = map { $_ => undef } @{ $self->values };
101
102 sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} };
103}
104
2fb4885e 105sub create_child_type {
106 my ($self, @args) = @_;
39170e48 107 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
2fb4885e 108}
109
4078709c 1101;
dabed765 111
ad46f524 112# ABSTRACT: Type constraint for enumerated values.
113
dabed765 114__END__
115
116=pod
117
d7d8f2ee 118=head1 DESCRIPTION
119
120This class represents type constraints based on an enumerated list of
121acceptable values.
122
123=head1 INHERITANCE
124
125C<Moose::Meta::TypeConstraint::Enum> is a subclass of
126L<Moose::Meta::TypeConstraint>.
127
dabed765 128=head1 METHODS
129
130=over 4
131
d7d8f2ee 132=item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
133
d36ebb14 134This creates a new enum type constraint based on the given
d7d8f2ee 135C<%options>.
136
137It takes the same options as its parent, with several
138exceptions. First, it requires an additional option, C<values>. This
139should be an array reference containing a list of valid string
140values. Second, it automatically sets the parent to the C<Str> type.
141
142Finally, it ignores any provided C<constraint> option. The constraint
53de29e5 143is generated automatically based on the provided C<values>.
dabed765 144
d7d8f2ee 145=item B<< $constraint->values >>
dabed765 146
d7d8f2ee 147Returns the array reference of acceptable values provided to the
148constructor.
dabed765 149
d7d8f2ee 150=item B<< $constraint->create_child_type >>
dabed765 151
d7d8f2ee 152This returns a new L<Moose::Meta::TypeConstraint> object with the type
153as its parent.
4078709c 154
d7d8f2ee 155Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
156object!
2fb4885e 157
dabed765 158=back
159
4078709c 160=head1 BUGS
161
d4048ef3 162See L<Moose/BUGS> for details on reporting bugs.
4078709c 163
dabed765 164=cut
165
166