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