Improve type constraint stuff
gfx [Tue, 15 Sep 2009 05:55:02 +0000 (14:55 +0900)]
Changes
lib/Mouse/Meta/Attribute.pm
lib/Mouse/Meta/Role.pm
lib/Mouse/Meta/TypeConstraint.pm
lib/Mouse/Util/TypeConstraints.pm
t/030_roles/002_role.t
t/402-attribute-application.t

diff --git a/Changes b/Changes
index 0ae2f73..7f5a7cb 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@ Revision history for Mouse
 
 0.29
     * Support is => 'bare', and you must pass the 'is' option (gfx)
+
     * Make generator methods private (gfx)
 
 0.28 Wed Sep  8 20:00:06 2009
index 65d6daf..4c220b1 100644 (file)
@@ -221,13 +221,6 @@ sub create {
         if exists $args{coerce};
 
     if (exists $args{isa}) {
-        confess "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'  &&
-               $1 ne 'Maybe'
-        ;
-
         my $type_constraint = delete $args{isa};
         $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint);
     }
index 2ea38e2..e132488 100644 (file)
@@ -3,7 +3,6 @@ use strict;
 use warnings;
 use Carp 'confess';
 
-use Mouse::Meta::Attribute;
 use Mouse::Util qw(version authority identifier);
 
 do {
@@ -51,7 +50,7 @@ sub add_attribute {
     my $self = shift;
     my $name = shift;
     my $spec = shift;
-    $self->{attributes}->{$name} = Mouse::Meta::Attribute->new($name, %$spec);
+    $self->{attributes}->{$name} = $spec;
 }
 
 sub has_attribute { exists $_[0]->{attributes}->{$_[1]}  }
index 538e3b2..51b0867 100644 (file)
@@ -1,6 +1,8 @@
 package Mouse::Meta::TypeConstraint;
 use strict;
 use warnings;
+use Carp ();
+
 use overload '""'     => sub { shift->{name} },   # stringify to tc name
              fallback => 1;
 
@@ -28,6 +30,26 @@ sub check {
     $self->{_compiled_type_constraint}->(@_);
 }
 
+sub validate {
+    my ($self, $value) = @_;\r
+    if ($self->{_compiled_type_constraint}->($value)) {\r
+        return undef;\r
+    }\r
+    else {\r
+        $self->get_message($value);\r
+    }\r
+}
+
+sub assert_valid {\r
+    my ($self, $value) = @_;\r
+\r
+    my $error = $self->validate($value);\r
+    return 1 if ! defined $error;\r
+
+    Carp::confess($error);\r
+}\r
+
+
 sub message {
     return $_[0]->{message};
 }
index 4f77130..993e8e0 100644 (file)
@@ -308,6 +308,13 @@ sub find_type_constraint {
 sub find_or_create_isa_type_constraint {
     my $type_constraint = shift;
 
+    Carp::confess("Got isa => type_constraints, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef and Maybe (rt.cpan.org #39795)")
+        if $type_constraint =~ /\A ( [^\[]+ ) \[\.+\] \z/xms &&
+           $1 ne 'ArrayRef' &&
+           $1 ne 'HashRef'  &&
+           $1 ne 'Maybe'
+    ;
+
     my $code;
 
     $type_constraint =~ s/\s+//g;
index 4deb2ec..96ead79 100755 (executable)
@@ -85,13 +85,13 @@ is_deeply(
 
 ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
 
-is $foo_role->get_attribute('bar')->name, 'bar', '... got the correct description of the bar attribute';
+is $foo_role->get_attribute('bar')->{is}, 'rw', '... got the correct description of the bar attribute';
 
 ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
 
 is(
-    $foo_role->get_attribute('baz')->name,
-    'baz',
+    $foo_role->get_attribute('baz')->{is},
+    'ro',
     '... got the correct description of the baz attribute');
 
 # method modifiers
index cfd0256..e4745c5 100644 (file)
@@ -16,7 +16,7 @@ do {
     no Mouse::Role;
 };
 
-is(Role->meta->get_attribute('attr')->default, 'Role');
+is(Role->meta->get_attribute('attr')->{default}, 'Role');
 
 do {
     package Class;