X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint%2FEnum.pm;h=54b558120be59a92754b7664d30932d6ec3d4c40;hb=462bdc732ebe32abf5b9f3cd40033540a5f1388f;hp=588c7276831039392783e9651291d46528ce5310;hpb=c7d8fe950098e91780355ed7a91f066e682eb4a3;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint/Enum.pm b/lib/Moose/Meta/TypeConstraint/Enum.pm index 588c727..54b5581 100644 --- a/lib/Moose/Meta/TypeConstraint/Enum.pm +++ b/lib/Moose/Meta/TypeConstraint/Enum.pm @@ -6,10 +6,6 @@ use metaclass; use Moose::Util::TypeConstraints (); -our $VERSION = '0.55_02'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - use base 'Moose::Meta::TypeConstraint'; __PACKAGE__->meta->add_attribute('values' => ( @@ -21,6 +17,22 @@ sub new { $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str'); + if ( scalar @{ $args{values} } < 2 ) { + require Moose; + Moose->throw_error("You must have at least two values to enumerate through"); + } + + for (@{ $args{values} }) { + if (!defined($_)) { + require Moose; + Moose->throw_error("Enum values must be strings, not undef"); + } + elsif (ref($_)) { + require Moose; + Moose->throw_error("Enum values must be strings, not '$_'"); + } + } + my $self = $class->_new(\%args); $self->compile_type_constraint() @@ -67,50 +79,64 @@ sub _compile_hand_optimized_type_constraint { sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} }; } +sub create_child_type { + my ($self, @args) = @_; + return Moose::Meta::TypeConstraint->new(@args, parent => $self); +} + 1; +# ABSTRACT: Type constraint for enumerated values. + __END__ =pod -=head1 NAME +=head1 DESCRIPTION -Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values. +This class represents type constraints based on an enumerated list of +acceptable values. -=head1 METHODS +=head1 INHERITANCE -=over 4 +C is a subclass of +L. -=item B +=head1 METHODS -=item B +=over 4 -=item B +=item B<< Moose::Meta::TypeConstraint::Enum->new(%options) >> -=item B +This creates a new enum type constraint based on the given +C<%options>. -=item B +It takes the same options as its parent, with several +exceptions. First, it requires an additional option, C. This +should be an array reference containing a list of valid string +values. Second, it automatically sets the parent to the C type. -=back +Finally, it ignores any provided C option. The constraint +is generated automatically based on the provided C. -=head1 BUGS +=item B<< $constraint->values >> -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug -to cpan-RT. +Returns the array reference of acceptable values provided to the +constructor. -=head1 AUTHOR +=item B<< $constraint->create_child_type >> -Yuval Kogman Enothingmuch@cpan.orgE +This returns a new L object with the type +as its parent. -=head1 COPYRIGHT AND LICENSE +Note that it does I return a C +object! -Copyright 2006-2008 by Infinity Interactive, Inc. +=back -L +=head1 BUGS -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. +See L for details on reporting bugs. =cut