[Moose-commits] r7845 - in Mouse/trunk: lib/Mouse/Meta t
lestrrat at code2.0beta.co.uk
lestrrat at code2.0beta.co.uk
Sat Mar 7 08:40:27 GMT 2009
Author: lestrrat
Date: 2009-03-07 00:40:26 -0800 (Sat, 07 Mar 2009)
New Revision: 7845
Modified:
Mouse/trunk/lib/Mouse/Meta/Attribute.pm
Mouse/trunk/t/043-parameterized-type.t
Log:
I want parameterized types... I want it!
Modified: Mouse/trunk/lib/Mouse/Meta/Attribute.pm
===================================================================
--- Mouse/trunk/lib/Mouse/Meta/Attribute.pm 2009-03-07 03:26:17 UTC (rev 7844)
+++ Mouse/trunk/lib/Mouse/Meta/Attribute.pm 2009-03-07 08:40:26 UTC (rev 7845)
@@ -191,6 +191,58 @@
return \%method_map;
}
+our $optimized_constraints;
+sub _build_type_constraint {
+ my $spec = shift;
+ local $optimized_constraints ||= Mouse::Util::TypeConstraints->optimized_constraints;
+ my $code;
+ if ($spec =~ /^([^\[]+)\[(.+)\]$/) {
+ # parameterized
+ my $constraint = $1;
+ my $param = $2;
+ my $parent = _build_type_constraint($constraint);
+ my $child = _build_type_constraint($param);
+ if ($constraint eq 'ArrayRef') {
+ my $code_str =
+ "sub {\n" .
+ " if (\$parent->(\$_)) {\n" .
+ " foreach my \$e (@\$_) {\n" .
+ " local \$_ = \$e;\n" .
+ " return () unless \$child->(\$_);\n" .
+ " }\n" .
+ " return 1;\n" .
+ " }\n" .
+ " return ();\n" .
+ "};\n"
+ ;
+ $code = eval $code_str or Carp::confess($@);
+ } elsif ($constraint eq 'HashRef') {
+ my $code_str =
+ "sub {\n" .
+ " if (\$parent->(\$_)) {\n" .
+ " foreach my \$e (values %\$_) {\n" .
+ " local \$_ = \$e;\n" .
+ " return () unless \$child->(\$_);\n" .
+ " }\n" .
+ " return 1;\n" .
+ " }\n" .
+ " return ();\n" .
+ "};\n"
+ ;
+ $code = eval $code_str or Carp::confess($@);
+ } else {
+ Carp::confess("Support for parameterized types other than ArrayRef or HashRef is not implemented yet");
+ }
+ } else {
+ $code = $optimized_constraints->{ $spec };
+ if (! $code) {
+ $code = sub { Scalar::Util::blessed($_) && $_->isa($spec) };
+ $optimized_constraints->{$spec} = $code;
+ }
+ }
+ return $code;
+}
+
sub create {
my ($self, $class, $name, %args) = @_;
@@ -204,24 +256,22 @@
if exists $args{coerce};
if (exists $args{isa}) {
- confess "Mouse does not yet support parameterized types (rt.cpan.org #39795)"
- if $args{isa} =~ /\[.*\]/;
+ warn "Got isa => $args{isa}, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef (rt.cpan.org #39795)"
+ if $args{isa} =~ /^([^\[]+)\[.+\]$/ &&
+ $1 ne 'ArrayRef' &&
+ $1 ne 'HashRef';
my $type_constraint = delete $args{isa};
$type_constraint =~ s/\s//g;
my @type_constraints = split /\|/, $type_constraint;
my $code;
- my $optimized_constraints = Mouse::Util::TypeConstraints->optimized_constraints;
if (@type_constraints == 1) {
- $code = $optimized_constraints->{$type_constraints[0]} ||
- sub { Scalar::Util::blessed($_) && $_->isa($type_constraints[0]) };
+ $code = _build_type_constraint($type_constraints[0]);
$args{type_constraint} = $type_constraints[0];
} else {
my @code_list = map {
- my $type = $_;
- $optimized_constraints->{$type} ||
- sub { Scalar::Util::blessed($_) && $_->isa($type) }
+ _build_type_constraint($_)
} @type_constraints;
$code = sub {
for my $code (@code_list) {
Modified: Mouse/trunk/t/043-parameterized-type.t
===================================================================
--- Mouse/trunk/t/043-parameterized-type.t 2009-03-07 03:26:17 UTC (rev 7844)
+++ Mouse/trunk/t/043-parameterized-type.t 2009-03-07 08:40:26 UTC (rev 7845)
@@ -1,13 +1,11 @@
#!/usr/bin/env perl
use strict;
use warnings;
-use Test::More tests => 1;
+use Test::More tests => 7;
use Test::Exception;
-TODO: {
- local $TODO = "Mouse does not support parameterized types yet";
-
- eval {
+{
+ {
package Foo;
use Mouse;
@@ -15,8 +13,44 @@
is => 'ro',
isa => 'HashRef[Int]',
);
+
+ has bar => (
+ is => 'ro',
+ isa => 'ArrayRef[Int]',
+ );
+
+ has 'complex' => (
+ is => 'rw',
+ isa => 'ArrayRef[HashRef[Int]]'
+ );
};
ok(Foo->meta->has_attribute('foo'));
-};
+ lives_and {
+ my $hash = { a => 1, b => 2, c => 3 };
+ my $array = [ 1, 2, 3 ];
+ my $complex = [ { a => 1, b => 1 }, { c => 2, d => 2} ];
+ my $foo = Foo->new(foo => $hash, bar => $array, complex => $complex);
+
+ is_deeply($foo->foo(), $hash, "foo is a proper hash");
+ is_deeply($foo->bar(), $array, "bar is a proper array");
+ is_deeply($foo->complex(), $complex, "complex is a proper ... structure");
+ } "Parameterized constraints work";
+
+ # check bad args
+ throws_ok {
+ Foo->new( foo => { a => 'b' });
+ } qr/Attribute \(foo\) does not pass the type constraint because: Validation failed for 'HashRef\[Int\]' failed with value/, "Bad args for hash throws an exception";
+
+ throws_ok {
+ Foo->new( bar => [ a => 'b' ]);
+ } qr/Attribute \(bar\) does not pass the type constraint because: Validation failed for 'ArrayRef\[Int\]' failed with value/, "Bad args for array throws an exception";
+
+ throws_ok {
+ Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] )
+ } qr/Attribute \(complex\) does not pass the type constraint because: Validation failed for 'ArrayRef\[HashRef\[Int\]\]' failed with value/, "Bad args for complex types throws an exception";
+}
+
+
+
More information about the Moose-commits
mailing list