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