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