Checking in changes prior to tagging of version 0.93. Changelog diff is:
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint.pm
index 559ef20..1e3736d 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.75_01';
+our $VERSION   = '0.93';
 $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) {
@@ -110,9 +121,9 @@ sub get_message {
         return $msg->($value);
     }
     else {
-        $value = (defined $value ? overload::StrVal($value) : 'undef');        
+        $value = (defined $value ? overload::StrVal($value) : 'undef');
         return "Validation failed for '" . $self->name . "' failed with value $value";
-    }    
+    }
 }
 
 ## type predicates ...
@@ -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);
@@ -384,6 +395,13 @@ method returns an explicit C<undef>. If the type is not valid, we call
 C<< $self->get_message($value) >> internally to generate an error
 message.
 
+=item B<< $constraint->assert_valid($value) >>
+
+Like C<check> and C<validate>, this method checks whether C<$value> is
+valid under the constraint.  If it is, it will return true.  If it is not,
+an exception will be thrown with the results of
+C<< $self->get_message($value) >>.
+
 =item B<< $constraint->name >>
 
 Returns the type's name, as provided to the constructor.