bump version to 0.65
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Enum.pm
CommitLineData
dabed765 1package Moose::Meta::TypeConstraint::Enum;
2
3use strict;
4use warnings;
5use metaclass;
6
4078709c 7use Moose::Util::TypeConstraints ();
8
30350cb4 9our $VERSION = '0.65';
75b95414 10$VERSION = eval $VERSION;
dabed765 11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Moose::Meta::TypeConstraint';
14
15__PACKAGE__->meta->add_attribute('values' => (
4078709c 16 accessor => 'values',
dabed765 17));
18
19sub new {
20 my ( $class, %args ) = @_;
21
22 $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str');
23
1b8d1399 24 my $self = $class->_new(\%args);
dabed765 25
26 $self->compile_type_constraint()
27 unless $self->_has_compiled_type_constraint;
28
29 return $self;
30}
31
32sub equals {
33 my ( $self, $type_or_name ) = @_;
34
35 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
36
37 return unless $other->isa(__PACKAGE__);
38
39 my @self_values = sort @{ $self->values };
40 my @other_values = sort @{ $other->values };
41
42 return unless @self_values == @other_values;
43
44 while ( @self_values ) {
45 my $value = shift @self_values;
46 my $other_value = shift @other_values;
47
48 return unless $value eq $other_value;
49 }
50
51 return 1;
52}
53
54sub constraint {
55 my $self = shift;
56
57 my %values = map { $_ => undef } @{ $self->values };
58
59 return sub { exists $values{$_[0]} };
60}
61
62sub _compile_hand_optimized_type_constraint {
63 my $self = shift;
64
65 my %values = map { $_ => undef } @{ $self->values };
66
67 sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} };
68}
69
2fb4885e 70sub create_child_type {
71 my ($self, @args) = @_;
39170e48 72 return Moose::Meta::TypeConstraint->new(@args, parent => $self);
2fb4885e 73}
74
4078709c 751;
dabed765 76
77__END__
78
79=pod
80
81=head1 NAME
82
83Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values.
84
85=head1 METHODS
86
87=over 4
88
89=item B<new>
90
91=item B<equals>
92
93=item B<constraint>
94
95=item B<values>
96
4078709c 97=item B<meta>
98
2fb4885e 99=item B<create_child_type>
100
dabed765 101=back
102
4078709c 103=head1 BUGS
104
105All complex software has bugs lurking in it, and this module is no
106exception. If you find a bug please either email me, or add the bug
107to cpan-RT.
108
109=head1 AUTHOR
110
111Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
112
113=head1 COPYRIGHT AND LICENSE
114
115Copyright 2006-2008 by Infinity Interactive, Inc.
116
117L<http://www.iinteractive.com>
118
119This library is free software; you can redistribute it and/or modify
120it under the same terms as Perl itself.
121
dabed765 122=cut
123
124