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