- 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.
use Scalar::Util 'blessed', 'reftype';
use Sub::Exporter;
-our $VERSION = '0.21';
+our $VERSION = '0.22';
our $AUTHORITY = 'cpan:STEVAN';
## --------------------------------------------------------
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;
B<NOTE:> This is not a true proper enum type, it is simple
a convient constraint builder.
+=item B<enum (\@values)>
+
+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<as>
This is just sugar for the type constraint construction syntax.
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();
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;
+
+
+