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