use Eval::Closure rather than doing string eval directly
[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 create_child_type {
98     my ($self, @args) = @_;
99     return Moose::Meta::TypeConstraint->new(@args, parent => $self);
100 }
101
102 1;
103
104 # ABSTRACT: Type constraint for enumerated values.
105
106 __END__
107
108 =pod
109
110 =head1 DESCRIPTION
111
112 This class represents type constraints based on an enumerated list of
113 acceptable values.
114
115 =head1 INHERITANCE
116
117 C<Moose::Meta::TypeConstraint::Enum> is a subclass of
118 L<Moose::Meta::TypeConstraint>.
119
120 =head1 METHODS
121
122 =over 4
123
124 =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
125
126 This creates a new enum type constraint based on the given
127 C<%options>.
128
129 It takes the same options as its parent, with several
130 exceptions. First, it requires an additional option, C<values>. This
131 should be an array reference containing a list of valid string
132 values. Second, it automatically sets the parent to the C<Str> type.
133
134 Finally, it ignores any provided C<constraint> option. The constraint
135 is generated automatically based on the provided C<values>.
136
137 =item B<< $constraint->values >>
138
139 Returns the array reference of acceptable values provided to the
140 constructor.
141
142 =item B<< $constraint->create_child_type >>
143
144 This returns a new L<Moose::Meta::TypeConstraint> object with the type
145 as its parent.
146
147 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
148 object!
149
150 =back
151
152 =head1 BUGS
153
154 See L<Moose/BUGS> for details on reporting bugs.
155
156 =cut
157
158