Commit | Line | Data |
dabed765 |
1 | package Moose::Meta::TypeConstraint::Enum; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
4078709c |
7 | use Moose::Util::TypeConstraints (); |
8 | |
dabed765 |
9 | our $AUTHORITY = 'cpan:STEVAN'; |
10 | |
11 | use base 'Moose::Meta::TypeConstraint'; |
12 | |
13 | __PACKAGE__->meta->add_attribute('values' => ( |
4078709c |
14 | accessor => 'values', |
dabed765 |
15 | )); |
16 | |
17 | sub new { |
18 | my ( $class, %args ) = @_; |
19 | |
20 | $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str'); |
21 | |
f6af1028 |
22 | if ( scalar @{ $args{values} } < 2 ) { |
23 | require Moose; |
24 | Moose->throw_error("You must have at least two values to enumerate through"); |
25 | } |
26 | |
27 | for (@{ $args{values} }) { |
28 | if (!defined($_)) { |
29 | require Moose; |
30 | Moose->throw_error("Enum values must be strings, not undef"); |
31 | } |
32 | elsif (ref($_)) { |
33 | require Moose; |
34 | Moose->throw_error("Enum values must be strings, not '$_'"); |
35 | } |
36 | } |
37 | |
1b8d1399 |
38 | my $self = $class->_new(\%args); |
dabed765 |
39 | |
40 | $self->compile_type_constraint() |
41 | unless $self->_has_compiled_type_constraint; |
42 | |
43 | return $self; |
44 | } |
45 | |
46 | sub equals { |
47 | my ( $self, $type_or_name ) = @_; |
48 | |
49 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); |
50 | |
51 | return unless $other->isa(__PACKAGE__); |
52 | |
53 | my @self_values = sort @{ $self->values }; |
54 | my @other_values = sort @{ $other->values }; |
55 | |
56 | return unless @self_values == @other_values; |
57 | |
58 | while ( @self_values ) { |
59 | my $value = shift @self_values; |
60 | my $other_value = shift @other_values; |
61 | |
62 | return unless $value eq $other_value; |
63 | } |
64 | |
65 | return 1; |
66 | } |
67 | |
68 | sub constraint { |
69 | my $self = shift; |
70 | |
71 | my %values = map { $_ => undef } @{ $self->values }; |
72 | |
73 | return sub { exists $values{$_[0]} }; |
74 | } |
75 | |
76 | sub _compile_hand_optimized_type_constraint { |
77 | my $self = shift; |
78 | |
79 | my %values = map { $_ => undef } @{ $self->values }; |
80 | |
81 | sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} }; |
82 | } |
83 | |
2fb4885e |
84 | sub create_child_type { |
85 | my ($self, @args) = @_; |
39170e48 |
86 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); |
2fb4885e |
87 | } |
88 | |
4078709c |
89 | 1; |
dabed765 |
90 | |
ad46f524 |
91 | # ABSTRACT: Type constraint for enumerated values. |
92 | |
dabed765 |
93 | __END__ |
94 | |
95 | =pod |
96 | |
d7d8f2ee |
97 | =head1 DESCRIPTION |
98 | |
99 | This class represents type constraints based on an enumerated list of |
100 | acceptable values. |
101 | |
102 | =head1 INHERITANCE |
103 | |
104 | C<Moose::Meta::TypeConstraint::Enum> is a subclass of |
105 | L<Moose::Meta::TypeConstraint>. |
106 | |
dabed765 |
107 | =head1 METHODS |
108 | |
109 | =over 4 |
110 | |
d7d8f2ee |
111 | =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >> |
112 | |
d36ebb14 |
113 | This creates a new enum type constraint based on the given |
d7d8f2ee |
114 | C<%options>. |
115 | |
116 | It takes the same options as its parent, with several |
117 | exceptions. First, it requires an additional option, C<values>. This |
118 | should be an array reference containing a list of valid string |
119 | values. Second, it automatically sets the parent to the C<Str> type. |
120 | |
121 | Finally, it ignores any provided C<constraint> option. The constraint |
53de29e5 |
122 | is generated automatically based on the provided C<values>. |
dabed765 |
123 | |
d7d8f2ee |
124 | =item B<< $constraint->values >> |
dabed765 |
125 | |
d7d8f2ee |
126 | Returns the array reference of acceptable values provided to the |
127 | constructor. |
dabed765 |
128 | |
d7d8f2ee |
129 | =item B<< $constraint->create_child_type >> |
dabed765 |
130 | |
d7d8f2ee |
131 | This returns a new L<Moose::Meta::TypeConstraint> object with the type |
132 | as its parent. |
4078709c |
133 | |
d7d8f2ee |
134 | Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum> |
135 | object! |
2fb4885e |
136 | |
dabed765 |
137 | =back |
138 | |
4078709c |
139 | =head1 BUGS |
140 | |
d4048ef3 |
141 | See L<Moose/BUGS> for details on reporting bugs. |
4078709c |
142 | |
dabed765 |
143 | =cut |
144 | |
145 | |