Verify type constraints in the constructor
Shawn M Moore [Wed, 4 Jun 2008 04:04:58 +0000 (04:04 +0000)]
lib/Mouse/Attribute.pm
lib/Mouse/Object.pm

index da448b5..36c907a 100644 (file)
@@ -44,15 +44,7 @@ sub generate_accessor {
     my $default    = $attribute->{default};
     my $trigger    = $attribute->{trigger};
     my $type       = $attribute->{type_constraint};
-
-    my $constraint = sub {
-        return unless $type;
-
-        my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
-        return $checker if $checker;
-
-        confess "Unable to parse type constraint '$type'";
-    }->();
+    my $constraint = $attribute->find_type_constraint;
 
     my $accessor = 'sub {
         my $self = shift;';
@@ -187,6 +179,33 @@ sub create {
     return $attribute;
 }
 
+sub find_type_constraint {
+    my $self = shift;
+    my $type = $self->type_constraint;
+
+    return unless $type;
+
+    my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
+    return $checker if $checker;
+
+    confess "Unable to parse type constraint '$type'";
+}
+
+sub verify_type_constraint {
+    my $self = shift;
+    local $_ = shift;
+
+    my $type = $self->type_constraint
+        or return 1;
+    my $constraint = $self->find_type_constraint
+        or return 1;
+
+    return 1 if $constraint->($_);
+
+    my $name = $self->name;
+    Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
+}
+
 1;
 
 __END__
index 82cbec0..2a5a639 100644 (file)
@@ -19,16 +19,17 @@ sub new {
         if (!exists($args{$key})) {
             if (exists($attribute->{default})) {
                 unless ($attribute->{lazy}) {
-                    if (ref($attribute->{default}) eq 'CODE') {
-                        $instance->{$key} = $attribute->{default}->();
-                        Scalar::Util::weaken($instance->{$key})
-                            if $attribute->{weak_ref};
-                    }
-                    else {
-                        $instance->{$key} = $attribute->{default};
-                        Scalar::Util::weaken($instance->{$key})
-                            if $attribute->{weak_ref};
-                    }
+                    my $default = ref($attribute->{default}) eq 'CODE'
+                                ? $attribute->{default}->()
+                                : $attribute->{default};
+
+                    $attribute->verify_type_constraint($default)
+                        if $attribute->has_type_constraint;
+
+                    $instance->{$key} = $default;
+
+                    Scalar::Util::weaken($instance->{$key})
+                        if $attribute->{weak_ref};
                 }
             }
             else {
@@ -39,7 +40,11 @@ sub new {
         }
 
         if (exists($args{$key})) {
+            $attribute->verify_type_constraint($args{$key})
+                if $attribute->has_type_constraint;
+
             $instance->{$key} = $args{$key};
+
             Scalar::Util::weaken($instance->{$key})
                 if $attribute->{weak_ref};