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