bump version to 1.14
[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
b6cca0d5 9our $VERSION = '1.14';
75b95414 10$VERSION = eval $VERSION;
dabed765 11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Moose::Meta::TypeConstraint';
14
15__PACKAGE__->meta->add_attribute('values' => (
4078709c 16 accessor => 'values',
dabed765 17));
18
19sub new {
20 my ( $class, %args ) = @_;
21
22 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
23
f6af1028 24 if ( scalar @{ $args{values} } < 2 ) {
25 require Moose;
26 Moose->throw_error("You must have at least two values to enumerate through");
27 }
28
29 for (@{ $args{values} }) {
30 if (!defined($_)) {
31 require Moose;
32 Moose->throw_error("Enum values must be strings, not undef");
33 }
34 elsif (ref($_)) {
35 require Moose;
36 Moose->throw_error("Enum values must be strings, not '$_'");
37 }
38 }
39
1b8d1399 40 my $self = $class->_new(\%args);
dabed765 41
42 $self->compile_type_constraint()
43 unless $self->_has_compiled_type_constraint;
44
45 return $self;
46}
47
48sub equals {
49 my ( $self, $type_or_name ) = @_;
50
51 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
52
53 return unless $other->isa(__PACKAGE__);
54
55 my @self_values = sort @{ $self->values };
56 my @other_values = sort @{ $other->values };
57
58 return unless @self_values == @other_values;
59
60 while ( @self_values ) {
61 my $value = shift @self_values;
62 my $other_value = shift @other_values;
63
64 return unless $value eq $other_value;
65 }
66
67 return 1;
68}
69
70sub constraint {
71 my $self = shift;
72
73 my %values = map { $_ => undef } @{ $self->values };
74
75 return sub { exists $values{$_[0]} };
76}
77
78sub _compile_hand_optimized_type_constraint {
79 my $self = shift;
80
81 my %values = map { $_ => undef } @{ $self->values };
82
83 sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} };
84}
85
2fb4885e 86sub create_child_type {
87 my ($self, @args) = @_;
39170e48 88 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
2fb4885e 89}
90
4078709c 911;
dabed765 92
93__END__
94
95=pod
96
97=head1 NAME
98
99Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values.
100
d7d8f2ee 101=head1 DESCRIPTION
102
103This class represents type constraints based on an enumerated list of
104acceptable values.
105
106=head1 INHERITANCE
107
108C<Moose::Meta::TypeConstraint::Enum> is a subclass of
109L<Moose::Meta::TypeConstraint>.
110
dabed765 111=head1 METHODS
112
113=over 4
114
d7d8f2ee 115=item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
116
d36ebb14 117This creates a new enum type constraint based on the given
d7d8f2ee 118C<%options>.
119
120It takes the same options as its parent, with several
121exceptions. First, it requires an additional option, C<values>. This
122should be an array reference containing a list of valid string
123values. Second, it automatically sets the parent to the C<Str> type.
124
125Finally, it ignores any provided C<constraint> option. The constraint
53de29e5 126is generated automatically based on the provided C<values>.
dabed765 127
d7d8f2ee 128=item B<< $constraint->values >>
dabed765 129
d7d8f2ee 130Returns the array reference of acceptable values provided to the
131constructor.
dabed765 132
d7d8f2ee 133=item B<< $constraint->create_child_type >>
dabed765 134
d7d8f2ee 135This returns a new L<Moose::Meta::TypeConstraint> object with the type
136as its parent.
4078709c 137
d7d8f2ee 138Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
139object!
2fb4885e 140
dabed765 141=back
142
4078709c 143=head1 BUGS
144
d4048ef3 145See L<Moose/BUGS> for details on reporting bugs.
4078709c 146
147=head1 AUTHOR
148
149Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
150
151=head1 COPYRIGHT AND LICENSE
152
7e0492d3 153Copyright 2006-2010 by Infinity Interactive, Inc.
4078709c 154
155L<http://www.iinteractive.com>
156
157This library is free software; you can redistribute it and/or modify
158it under the same terms as Perl itself.
159
dabed765 160=cut
161
162