Beginning of dzilization
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Enum.pm
1 package Moose::Meta::TypeConstraint::Enum;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Moose::Util::TypeConstraints ();
8
9 our $AUTHORITY = 'cpan:STEVAN';
10
11 use base 'Moose::Meta::TypeConstraint';
12
13 __PACKAGE__->meta->add_attribute('values' => (
14     accessor => 'values',
15 ));
16
17 sub new {
18     my ( $class, %args ) = @_;
19
20     $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
21
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
38     my $self = $class->_new(\%args);
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
84 sub create_child_type {
85     my ($self, @args) = @_;
86     return Moose::Meta::TypeConstraint->new(@args, parent => $self);
87 }
88
89 1;
90
91 # ABSTRACT: Type constraint for enumerated values.
92
93 __END__
94
95 =pod
96
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
107 =head1 METHODS
108
109 =over 4
110
111 =item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >>
112
113 This creates a new enum type constraint based on the given
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
122 is generated automatically based on the provided C<values>.
123
124 =item B<< $constraint->values >>
125
126 Returns the array reference of acceptable values provided to the
127 constructor.
128
129 =item B<< $constraint->create_child_type >>
130
131 This returns a new L<Moose::Meta::TypeConstraint> object with the type
132 as its parent.
133
134 Note that it does I<not> return a C<Moose::Meta::TypeConstraint::Enum>
135 object!
136
137 =back
138
139 =head1 BUGS
140
141 See L<Moose/BUGS> for details on reporting bugs.
142
143 =cut
144
145