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