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 | |
ca789903 |
16 | __PACKAGE__->meta->add_attribute('_inline_var_name' => ( |
17 | accessor => '_inline_var_name', |
18 | )); |
19 | |
964294c1 |
20 | my $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 |
30 | my $var_suffix = '000000000000000000'; |
31 | |
dabed765 |
32 | sub 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 | |
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 | |
2fb4885e |
99 | sub create_child_type { |
100 | my ($self, @args) = @_; |
39170e48 |
101 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); |
2fb4885e |
102 | } |
103 | |
4078709c |
104 | 1; |
dabed765 |
105 | |
ad46f524 |
106 | # ABSTRACT: Type constraint for enumerated values. |
107 | |
dabed765 |
108 | __END__ |
109 | |
110 | =pod |
111 | |
d7d8f2ee |
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 | |
dabed765 |
122 | =head1 METHODS |
123 | |
124 | =over 4 |
125 | |
d7d8f2ee |
126 | =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >> |
127 | |
d36ebb14 |
128 | This creates a new enum type constraint based on the given |
d7d8f2ee |
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 |
53de29e5 |
137 | is generated automatically based on the provided C<values>. |
dabed765 |
138 | |
d7d8f2ee |
139 | =item B<< $constraint->values >> |
dabed765 |
140 | |
d7d8f2ee |
141 | Returns the array reference of acceptable values provided to the |
142 | constructor. |
dabed765 |
143 | |
d7d8f2ee |
144 | =item B<< $constraint->create_child_type >> |
dabed765 |
145 | |
d7d8f2ee |
146 | This returns a new L<Moose::Meta::TypeConstraint> object with the type |
147 | as its parent. |
4078709c |
148 | |
d7d8f2ee |
149 | Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum> |
150 | object! |
2fb4885e |
151 | |
dabed765 |
152 | =back |
153 | |
4078709c |
154 | =head1 BUGS |
155 | |
d4048ef3 |
156 | See L<Moose/BUGS> for details on reporting bugs. |
4078709c |
157 | |
dabed765 |
158 | =cut |
159 | |
160 | |