From: Stevan Little Date: Tue, 26 Feb 2008 18:51:11 +0000 (+0000) Subject: adding in anon-enums X-Git-Tag: 0_55~291 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9f4334a133bce21fd69e7172f486ba32286694f6;p=gitmo%2FMoose.git adding in anon-enums --- diff --git a/Changes b/Changes index 29d8b9b..fabb0ff 100644 --- a/Changes +++ b/Changes @@ -13,6 +13,12 @@ Revision history for Perl extension Moose - fixing the 'apply_all_roles' keyword so that it will not trigger Ovid's bug (http://use.perl.org/~Ovid/journal/35763) + * Moose::Util::TypeConstraints + - it is now possible to make anon-enums by passing 'enum' an + ARRAY ref instead of the $name => @values. Everything else + works as before. + - added tests for this + * t/ - making test for using '+name' on attributes consumed from a role, it works and makes sense too. diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 171a28a..903ec91 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -8,7 +8,7 @@ use Carp 'confess'; use Scalar::Util 'blessed', 'reftype'; use Sub::Exporter; -our $VERSION = '0.21'; +our $VERSION = '0.22'; our $AUTHORITY = 'cpan:STEVAN'; ## -------------------------------------------------------- @@ -253,6 +253,14 @@ sub optimize_as (&) { +{ optimized => $_[0] } } sub enum ($;@) { my ($type_name, @values) = @_; + # NOTE: + # if only an array-ref is passed then + # you get an anon-enum + # - SL + if (ref $type_name eq 'ARRAY' && !@values) { + @values = @$type_name; + $type_name = undef; + } (scalar @values >= 2) || confess "You must have at least two values to enumerate through"; my %valid = map { $_ => 1 } @values; @@ -833,6 +841,17 @@ See the L for a simple example. B This is not a true proper enum type, it is simple a convient constraint builder. +=item B + +If passed an ARRAY reference instead of the C<$name>, C<@values> pair, +this will create an unnamed enum. This can then be used in an attribute +definition like so: + + has 'sort_order' => ( + is => 'ro', + isa => enum([qw[ ascending descending ]]), + ); + =item B This is just sugar for the type constraint construction syntax. diff --git a/t/040_type_constraints/015_enum.t b/t/040_type_constraints/015_enum.t index d2cb922..a0b7851 100644 --- a/t/040_type_constraints/015_enum.t +++ b/t/040_type_constraints/015_enum.t @@ -30,7 +30,8 @@ push @invalid_metacharacters, '^1?$|^(11+?)\1+$'; plan tests => @valid_letters + @invalid_letters + @valid_languages + @invalid_languages - + @valid_metacharacters + @invalid_metacharacters; + + @valid_metacharacters + @invalid_metacharacters + + @valid_languages + 3; Moose::Util::TypeConstraints->export_type_constraints_as_functions(); @@ -44,3 +45,15 @@ ok(Metacharacter($_), "'$_' is a metacharacter") for @valid_metacharacters; ok(!Metacharacter($_), "'$_' is not a metacharacter") for @invalid_metacharacters; +# check anon enums + +my $anon_enum = enum \@valid_languages; +isa_ok($anon_enum, 'Moose::Meta::TypeConstraint'); + +is($anon_enum->name, '__ANON__', '... got the right name'); +is($anon_enum->parent->name, 'Str', '... got the right parent name'); + +ok($anon_enum->check($_), "'$_' is a language") for @valid_languages; + + +