version bump
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint.pm
index c259370..30f2506 100644 (file)
@@ -5,14 +5,17 @@ use strict;
 use warnings;
 use metaclass;
 
-use overload '""'     => sub { shift->name },   # stringify to tc name
+use overload '0+'     => sub { refaddr(shift) }, # id an object
+             '""'     => sub { shift->name },   # stringify to tc name
+             bool     => sub { 1 },
              fallback => 1;
 
 use Scalar::Util qw(blessed refaddr);
+use Sub::Name qw(subname);
 
 use base qw(Class::MOP::Object);
 
-our $VERSION   = '0.73_01';
+our $VERSION   = '1.08';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
@@ -82,6 +85,8 @@ sub coerce {
         Moose->throw_error("Cannot coerce without a type coercion");
     }
 
+    return $_[0] if $self->check($_[0]);
+
     return $coercion->coerce(@_);
 }
 
@@ -101,6 +106,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) {
@@ -108,9 +123,9 @@ sub get_message {
         return $msg->($value);
     }
     else {
-        $value = (defined $value ? overload::StrVal($value) : 'undef');        
-        return "Validation failed for '" . $self->name . "' failed with value $value";
-    }    
+        $value = (defined $value ? overload::StrVal($value) : 'undef');
+        return "Validation failed for '" . $self->name . "' with value $value";
+    }
 }
 
 ## type predicates ...
@@ -120,7 +135,7 @@ sub equals {
 
     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) or return;
 
-    return 1 if refaddr($self) == refaddr($other);
+    return 1 if $self == $other;
 
     if ( $self->has_hand_optimized_type_constraint and $other->has_hand_optimized_type_constraint ) {
         return 1 if $self->hand_optimized_type_constraint == $other->hand_optimized_type_constraint;
@@ -135,7 +150,7 @@ sub equals {
         return if $other->has_parent;
     }
 
-    return 1;
+    return;
 }
 
 sub is_a_type_of {
@@ -232,7 +247,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];
@@ -243,7 +258,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) {
@@ -259,7 +274,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);
@@ -285,10 +300,6 @@ sub create_child_type {
     return $class->new(%opts, parent => $self);
 }
 
-## this should get deprecated actually ...
-
-sub union { Carp::croak "DEPRECATED" }
-
 1;
 
 __END__
@@ -302,7 +313,7 @@ Moose::Meta::TypeConstraint - The Moose Type Constraint metaclass
 =head1 DESCRIPTION
 
 This class represents a single type constraint. Moose's built-in type
-constraints, as well as constraints you define, are all store in a
+constraints, as well as constraints you define, are all stored in a
 L<Moose::Meta::TypeConstraint::Registry> object as objects of this
 class.
 
@@ -386,6 +397,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.
@@ -450,9 +468,7 @@ behavior and change how child types are created.
 
 =head1 BUGS
 
-All complex software has bugs lurking in it, and this module is no
-exception. If you find a bug please either email me, or add the bug
-to cpan-RT.
+See L<Moose/BUGS> for details on reporting bugs.
 
 =head1 AUTHOR
 
@@ -460,7 +476,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2009 by Infinity Interactive, Inc.
+Copyright 2006-2010 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>