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