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