Use dzil Authority plugin - remove $AUTHORITY from code
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Enum.pm
1 package Moose::Meta::TypeConstraint::Enum;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Moose::Util::TypeConstraints ();
8
9 use base 'Moose::Meta::TypeConstraint';
10
11 __PACKAGE__->meta->add_attribute('values' => (
12     accessor => 'values',
13 ));
14
15 sub new {
16     my ( $class, %args ) = @_;
17
18     $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
19
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
36     my $self = $class->_new(\%args);
37
38     $self->compile_type_constraint()
39         unless $self->_has_compiled_type_constraint;
40
41     return $self;
42 }
43
44 sub 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
66 sub constraint {
67     my $self = shift;
68
69     my %values = map { $_ => undef } @{ $self->values };
70
71     return sub { exists $values{$_[0]} };
72 }
73
74 sub _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
82 sub create_child_type {
83     my ($self, @args) = @_;
84     return Moose::Meta::TypeConstraint->new(@args, parent => $self);
85 }
86
87 1;
88
89 # ABSTRACT: Type constraint for enumerated values.
90
91 __END__
92
93 =pod
94
95 =head1 DESCRIPTION
96
97 This class represents type constraints based on an enumerated list of
98 acceptable values.
99
100 =head1 INHERITANCE
101
102 C<Moose::Meta::TypeConstraint::Enum> is a subclass of
103 L<Moose::Meta::TypeConstraint>.
104
105 =head1 METHODS
106
107 =over 4
108
109 =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
110
111 This creates a new enum type constraint based on the given
112 C<%options>.
113
114 It takes the same options as its parent, with several
115 exceptions. First, it requires an additional option, C<values>. This
116 should be an array reference containing a list of valid string
117 values. Second, it automatically sets the parent to the C<Str> type.
118
119 Finally, it ignores any provided C<constraint> option. The constraint
120 is generated automatically based on the provided C<values>.
121
122 =item B<< $constraint->values >>
123
124 Returns the array reference of acceptable values provided to the
125 constructor.
126
127 =item B<< $constraint->create_child_type >>
128
129 This returns a new L<Moose::Meta::TypeConstraint> object with the type
130 as its parent.
131
132 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
133 object!
134
135 =back
136
137 =head1 BUGS
138
139 See L<Moose/BUGS> for details on reporting bugs.
140
141 =cut
142
143