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 | |
92d82041 |
9 | our $VERSION = '0.87'; |
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 | |
1b8d1399 |
24 | my $self = $class->_new(\%args); |
dabed765 |
25 | |
26 | $self->compile_type_constraint() |
27 | unless $self->_has_compiled_type_constraint; |
28 | |
29 | return $self; |
30 | } |
31 | |
32 | sub equals { |
33 | my ( $self, $type_or_name ) = @_; |
34 | |
35 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); |
36 | |
37 | return unless $other->isa(__PACKAGE__); |
38 | |
39 | my @self_values = sort @{ $self->values }; |
40 | my @other_values = sort @{ $other->values }; |
41 | |
42 | return unless @self_values == @other_values; |
43 | |
44 | while ( @self_values ) { |
45 | my $value = shift @self_values; |
46 | my $other_value = shift @other_values; |
47 | |
48 | return unless $value eq $other_value; |
49 | } |
50 | |
51 | return 1; |
52 | } |
53 | |
54 | sub constraint { |
55 | my $self = shift; |
56 | |
57 | my %values = map { $_ => undef } @{ $self->values }; |
58 | |
59 | return sub { exists $values{$_[0]} }; |
60 | } |
61 | |
62 | sub _compile_hand_optimized_type_constraint { |
63 | my $self = shift; |
64 | |
65 | my %values = map { $_ => undef } @{ $self->values }; |
66 | |
67 | sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} }; |
68 | } |
69 | |
2fb4885e |
70 | sub create_child_type { |
71 | my ($self, @args) = @_; |
39170e48 |
72 | return Moose::Meta::TypeConstraint->new(@args, parent => $self); |
2fb4885e |
73 | } |
74 | |
4078709c |
75 | 1; |
dabed765 |
76 | |
77 | __END__ |
78 | |
79 | =pod |
80 | |
81 | =head1 NAME |
82 | |
83 | Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values. |
84 | |
d7d8f2ee |
85 | =head1 DESCRIPTION |
86 | |
87 | This class represents type constraints based on an enumerated list of |
88 | acceptable values. |
89 | |
90 | =head1 INHERITANCE |
91 | |
92 | C<Moose::Meta::TypeConstraint::Enum> is a subclass of |
93 | L<Moose::Meta::TypeConstraint>. |
94 | |
dabed765 |
95 | =head1 METHODS |
96 | |
97 | =over 4 |
98 | |
d7d8f2ee |
99 | =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >> |
100 | |
d36ebb14 |
101 | This creates a new enum type constraint based on the given |
d7d8f2ee |
102 | C<%options>. |
103 | |
104 | It takes the same options as its parent, with several |
105 | exceptions. First, it requires an additional option, C<values>. This |
106 | should be an array reference containing a list of valid string |
107 | values. Second, it automatically sets the parent to the C<Str> type. |
108 | |
109 | Finally, it ignores any provided C<constraint> option. The constraint |
53de29e5 |
110 | is generated automatically based on the provided C<values>. |
dabed765 |
111 | |
d7d8f2ee |
112 | =item B<< $constraint->values >> |
dabed765 |
113 | |
d7d8f2ee |
114 | Returns the array reference of acceptable values provided to the |
115 | constructor. |
dabed765 |
116 | |
d7d8f2ee |
117 | =item B<< $constraint->create_child_type >> |
dabed765 |
118 | |
d7d8f2ee |
119 | This returns a new L<Moose::Meta::TypeConstraint> object with the type |
120 | as its parent. |
4078709c |
121 | |
d7d8f2ee |
122 | Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum> |
123 | object! |
2fb4885e |
124 | |
dabed765 |
125 | =back |
126 | |
4078709c |
127 | =head1 BUGS |
128 | |
d03bd989 |
129 | All complex software has bugs lurking in it, and this module is no |
4078709c |
130 | exception. If you find a bug please either email me, or add the bug |
131 | to cpan-RT. |
132 | |
133 | =head1 AUTHOR |
134 | |
135 | Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt> |
136 | |
137 | =head1 COPYRIGHT AND LICENSE |
138 | |
2840a3b2 |
139 | Copyright 2006-2009 by Infinity Interactive, Inc. |
4078709c |
140 | |
141 | L<http://www.iinteractive.com> |
142 | |
143 | This library is free software; you can redistribute it and/or modify |
144 | it under the same terms as Perl itself. |
145 | |
dabed765 |
146 | =cut |
147 | |
148 | |