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