Make sure that inlining union preserves the inline env
[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 __PACKAGE__->meta->add_attribute('_inline_var_name' => (
17     accessor => '_inline_var_name',
18 ));
19
20 my $inliner = sub {
21     my $self = shift;
22     my $val  = shift;
23
24     return 'defined(' . $val . ') '
25              . '&& !ref(' . $val . ') '
26              . '&& $' . $self->_inline_var_name . '{' . $val . '}';
27 };
28
29 # a quadrillion enums ought to be enough for any app
30 my $var_suffix = '000000000000000000';
31
32 sub new {
33     my ( $class, %args ) = @_;
34
35     $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
36     $args{inlined} = $inliner;
37
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
54     my %values = map { $_ => 1 } @{ $args{values} };
55     $args{constraint} = sub { $values{ $_[0] } };
56
57     my $var_name = 'enums' . $var_suffix++;;
58     $args{_inline_var_name} = $var_name;
59     $args{inline_environment} = { '%' . $var_name => \%values };
60
61     my $self = $class->_new(\%args);
62
63     $self->compile_type_constraint()
64         unless $self->_has_compiled_type_constraint;
65
66     return $self;
67 }
68
69 sub 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
91 sub constraint {
92     my $self = shift;
93
94     my %values = map { $_ => undef } @{ $self->values };
95
96     return sub { exists $values{$_[0]} };
97 }
98
99 sub create_child_type {
100     my ($self, @args) = @_;
101     return Moose::Meta::TypeConstraint->new(@args, parent => $self);
102 }
103
104 1;
105
106 # ABSTRACT: Type constraint for enumerated values.
107
108 __END__
109
110 =pod
111
112 =head1 DESCRIPTION
113
114 This class represents type constraints based on an enumerated list of
115 acceptable values.
116
117 =head1 INHERITANCE
118
119 C<Moose::Meta::TypeConstraint::Enum> is a subclass of
120 L<Moose::Meta::TypeConstraint>.
121
122 =head1 METHODS
123
124 =over 4
125
126 =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
127
128 This creates a new enum type constraint based on the given
129 C<%options>.
130
131 It takes the same options as its parent, with several
132 exceptions. First, it requires an additional option, C<values>. This
133 should be an array reference containing a list of valid string
134 values. Second, it automatically sets the parent to the C<Str> type.
135
136 Finally, it ignores any provided C<constraint> option. The constraint
137 is generated automatically based on the provided C<values>.
138
139 =item B<< $constraint->values >>
140
141 Returns the array reference of acceptable values provided to the
142 constructor.
143
144 =item B<< $constraint->create_child_type >>
145
146 This returns a new L<Moose::Meta::TypeConstraint> object with the type
147 as its parent.
148
149 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
150 object!
151
152 =back
153
154 =head1 BUGS
155
156 See L<Moose/BUGS> for details on reporting bugs.
157
158 =cut
159
160