X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint%2FEnum.pm;h=3ef723416af6c78ed68f446f06ade9319f893a69;hb=topic%2Fdocument-instance-role-application;hp=64ceb1f376d94e9f4b8ba6538c797e57efcd11a3;hpb=60f0816092ffe11986388dd2bba56a356b697843;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint/Enum.pm b/lib/Moose/Meta/TypeConstraint/Enum.pm index 64ceb1f..3ef7234 100644 --- a/lib/Moose/Meta/TypeConstraint/Enum.pm +++ b/lib/Moose/Meta/TypeConstraint/Enum.pm @@ -4,24 +4,62 @@ use strict; use warnings; use metaclass; +use B; use Moose::Util::TypeConstraints (); -our $VERSION = '1.09'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - use base 'Moose::Meta::TypeConstraint'; __PACKAGE__->meta->add_attribute('values' => ( accessor => 'values', + Class::MOP::_definition_context(), +)); + +__PACKAGE__->meta->add_attribute('_inline_var_name' => ( + accessor => '_inline_var_name', + Class::MOP::_definition_context(), )); +my $inliner = sub { + my $self = shift; + my $val = shift; + + return 'defined(' . $val . ') ' + . '&& !ref(' . $val . ') ' + . '&& $' . $self->_inline_var_name . '{' . $val . '}'; +}; + +my $var_suffix = 0; + sub new { my ( $class, %args ) = @_; $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Str'); + $args{inlined} = $inliner; - my $self = $class->_new(\%args); + if ( scalar @{ $args{values} } < 1 ) { + require Moose; + Moose->throw_error("You must have at least one value 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 %values = map { $_ => 1 } @{ $args{values} }; + $args{constraint} = sub { $values{ $_[0] } }; + + my $var_name = 'enums' . $var_suffix++;; + $args{_inline_var_name} = $var_name; + $args{inline_environment} = { '%' . $var_name => \%values }; + + my $self = $class->SUPER::new(\%args); $self->compile_type_constraint() unless $self->_has_compiled_type_constraint; @@ -59,14 +97,6 @@ sub constraint { return sub { exists $values{$_[0]} }; } -sub _compile_hand_optimized_type_constraint { - my $self = shift; - - my %values = map { $_ => undef } @{ $self->values }; - - sub { defined($_[0]) && !ref($_[0]) && exists $values{$_[0]} }; -} - sub create_child_type { my ($self, @args) = @_; return Moose::Meta::TypeConstraint->new(@args, parent => $self); @@ -74,14 +104,12 @@ sub create_child_type { 1; +# ABSTRACT: Type constraint for enumerated values. + __END__ =pod -=head1 NAME - -Moose::Meta::TypeConstraint::Enum - Type constraint for enumerated values. - =head1 DESCRIPTION This class represents type constraints based on an enumerated list of @@ -128,19 +156,6 @@ object! See L for details on reporting bugs. -=head1 AUTHOR - -Yuval Kogman Enothingmuch@cpan.orgE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006-2010 by Infinity Interactive, Inc. - -L - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - =cut