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