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 | |
3975b592 |
25 | return 'defined(' . $val . ') ' |
26 | . '&& !ref(' . $val . ') ' |
27 | . '&& $' . __PACKAGE__ . '::ENUMS{' . B::perlstring($name) . '}' |
28 | . '{' . $val . '}'; |
964294c1 |
29 | }; |
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] } }; |
55 | |
1b8d1399 |
56 | my $self = $class->_new(\%args); |
dabed765 |
57 | |
58 | $self->compile_type_constraint() |
59 | unless $self->_has_compiled_type_constraint; |
60 | |
61 | return $self; |
62 | } |
63 | |
64 | sub equals { |
65 | my ( $self, $type_or_name ) = @_; |
66 | |
67 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); |
68 | |
69 | return unless $other->isa(__PACKAGE__); |
70 | |
71 | my @self_values = sort @{ $self->values }; |
72 | my @other_values = sort @{ $other->values }; |
73 | |
74 | return unless @self_values == @other_values; |
75 | |
76 | while ( @self_values ) { |
77 | my $value = shift @self_values; |
78 | my $other_value = shift @other_values; |
79 | |
80 | return unless $value eq $other_value; |
81 | } |
82 | |
83 | return 1; |
84 | } |
85 | |
86 | sub constraint { |
87 | my $self = shift; |
88 | |
89 | my %values = map { $_ => undef } @{ $self->values }; |
90 | |
91 | return sub { exists $values{$_[0]} }; |
92 | } |
93 | |
2fb4885e |
94 | sub create_child_type { |
95 | my ($self, @args) = @_; |
39170e48 |
96 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); |
2fb4885e |
97 | } |
98 | |
4078709c |
99 | 1; |
dabed765 |
100 | |
ad46f524 |
101 | # ABSTRACT: Type constraint for enumerated values. |
102 | |
dabed765 |
103 | __END__ |
104 | |
105 | =pod |
106 | |
d7d8f2ee |
107 | =head1 DESCRIPTION |
108 | |
109 | This class represents type constraints based on an enumerated list of |
110 | acceptable values. |
111 | |
112 | =head1 INHERITANCE |
113 | |
114 | C<Moose::Meta::TypeConstraint::Enum> is a subclass of |
115 | L<Moose::Meta::TypeConstraint>. |
116 | |
dabed765 |
117 | =head1 METHODS |
118 | |
119 | =over 4 |
120 | |
d7d8f2ee |
121 | =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >> |
122 | |
d36ebb14 |
123 | This creates a new enum type constraint based on the given |
d7d8f2ee |
124 | C<%options>. |
125 | |
126 | It takes the same options as its parent, with several |
127 | exceptions. First, it requires an additional option, C<values>. This |
128 | should be an array reference containing a list of valid string |
129 | values. Second, it automatically sets the parent to the C<Str> type. |
130 | |
131 | Finally, it ignores any provided C<constraint> option. The constraint |
53de29e5 |
132 | is generated automatically based on the provided C<values>. |
dabed765 |
133 | |
d7d8f2ee |
134 | =item B<< $constraint->values >> |
dabed765 |
135 | |
d7d8f2ee |
136 | Returns the array reference of acceptable values provided to the |
137 | constructor. |
dabed765 |
138 | |
d7d8f2ee |
139 | =item B<< $constraint->create_child_type >> |
dabed765 |
140 | |
d7d8f2ee |
141 | This returns a new L<Moose::Meta::TypeConstraint> object with the type |
142 | as its parent. |
4078709c |
143 | |
d7d8f2ee |
144 | Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum> |
145 | object! |
2fb4885e |
146 | |
dabed765 |
147 | =back |
148 | |
4078709c |
149 | =head1 BUGS |
150 | |
d4048ef3 |
151 | See L<Moose/BUGS> for details on reporting bugs. |
4078709c |
152 | |
dabed765 |
153 | =cut |
154 | |
155 | |