assert_valid to use constraints for assertion
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint.pm
index d5217ca..298656f 100644 (file)
@@ -9,10 +9,11 @@ use overload '""'     => sub { shift->name },   # stringify to tc name
              fallback => 1;
 
 use Scalar::Util qw(blessed refaddr);
+use Sub::Name qw(subname);
 
 use base qw(Class::MOP::Object);
 
-our $VERSION   = '0.76';
+our $VERSION   = '0.88';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
@@ -103,6 +104,16 @@ sub validate {
     }
 }
 
+sub assert_valid {
+    my ($self, $value) = @_;
+
+    my $error = $self->validate($value);
+    return 1 if ! defined $error;
+
+    require Moose;
+    Moose->throw_error($error);
+}
+
 sub get_message {
     my ($self, $value) = @_;
     if (my $msg = $self->message) {
@@ -234,7 +245,7 @@ sub _compile_subtype {
         if ( $check == $null_constraint ) {
             return $optimized_parent;
         } else {
-            return Class::MOP::subname($self->name, sub {
+            return subname($self->name, sub {
                 return undef unless $optimized_parent->($_[0]);
                 my (@args) = @_;
                 local $_ = $args[0];
@@ -245,7 +256,7 @@ sub _compile_subtype {
         # general case, check all the constraints, from the first parent to ourselves
         my @checks = @parents;
         push @checks, $check if $check != $null_constraint;
-        return Class::MOP::subname($self->name => sub {
+        return subname($self->name => sub {
             my (@args) = @_;
             local $_ = $args[0];
             foreach my $check (@checks) {
@@ -261,7 +272,7 @@ sub _compile_type {
 
     return $check if $check == $null_constraint; # Item, Any
 
-    return Class::MOP::subname($self->name => sub {
+    return subname($self->name => sub {
         my (@args) = @_;
         local $_ = $args[0];
         $check->(@args);