Commit | Line | Data |
dabed765 |
1 | package Moose::Meta::TypeConstraint::Enum; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
964294c1 |
7 | use B; |
4078709c |
8 | use Moose::Util::TypeConstraints (); |
9 | |
dabed765 |
10 | use base 'Moose::Meta::TypeConstraint'; |
11 | |
12 | __PACKAGE__->meta->add_attribute('values' => ( |
4078709c |
13 | accessor => 'values', |
dabed765 |
14 | )); |
15 | |
964294c1 |
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 |
883e299e |
26 | "defined $val" |
27 | . "&& ! ref $val" . '&& $' |
964294c1 |
28 | . __PACKAGE__ |
29 | . '::ENUMS{' |
30 | . B::perlstring($name) |
31 | . "}{ $val }"; |
32 | }; |
33 | |
dabed765 |
34 | sub new { |
35 | my ( $class, %args ) = @_; |
36 | |
37 | $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str'); |
964294c1 |
38 | $args{inlined} = $inliner; |
dabed765 |
39 | |
f6af1028 |
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 | |
964294c1 |
56 | my %values = map { $_ => 1 } @{ $args{values} }; |
57 | $args{constraint} = sub { $values{ $_[0] } }; |
58 | |
1b8d1399 |
59 | my $self = $class->_new(\%args); |
dabed765 |
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 | |
2fb4885e |
105 | sub create_child_type { |
106 | my ($self, @args) = @_; |
39170e48 |
107 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); |
2fb4885e |
108 | } |
109 | |
4078709c |
110 | 1; |
dabed765 |
111 | |
ad46f524 |
112 | # ABSTRACT: Type constraint for enumerated values. |
113 | |
dabed765 |
114 | __END__ |
115 | |
116 | =pod |
117 | |
d7d8f2ee |
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 | |
dabed765 |
128 | =head1 METHODS |
129 | |
130 | =over 4 |
131 | |
d7d8f2ee |
132 | =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >> |
133 | |
d36ebb14 |
134 | This creates a new enum type constraint based on the given |
d7d8f2ee |
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 |
53de29e5 |
143 | is generated automatically based on the provided C<values>. |
dabed765 |
144 | |
d7d8f2ee |
145 | =item B<< $constraint->values >> |
dabed765 |
146 | |
d7d8f2ee |
147 | Returns the array reference of acceptable values provided to the |
148 | constructor. |
dabed765 |
149 | |
d7d8f2ee |
150 | =item B<< $constraint->create_child_type >> |
dabed765 |
151 | |
d7d8f2ee |
152 | This returns a new L<Moose::Meta::TypeConstraint> object with the type |
153 | as its parent. |
4078709c |
154 | |
d7d8f2ee |
155 | Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum> |
156 | object! |
2fb4885e |
157 | |
dabed765 |
158 | =back |
159 | |
4078709c |
160 | =head1 BUGS |
161 | |
d4048ef3 |
162 | See L<Moose/BUGS> for details on reporting bugs. |
4078709c |
163 | |
dabed765 |
164 | =cut |
165 | |
166 | |