X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint.pm;h=bac571a94e45b304d434c4be8c1a9cb1ed7f2296;hb=e462f6f3d260687b8f7372b112a50c5c2a2c431c;hp=d5217ca3912293cc6b82f66ce8a911c4069b0593;hpb=be21cc5c8077f4bb88f1682793ab70f35e627e86;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index d5217ca..bac571a 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -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 = '1.05'; $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) { @@ -111,7 +122,7 @@ sub get_message { } else { $value = (defined $value ? overload::StrVal($value) : 'undef'); - return "Validation failed for '" . $self->name . "' failed with value $value"; + return "Validation failed for '" . $self->name . "' with value $value"; } } @@ -137,7 +148,7 @@ sub equals { return if $other->has_parent; } - return 1; + return; } sub is_a_type_of { @@ -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); @@ -300,7 +311,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 object as objects of this class. @@ -384,6 +395,13 @@ method returns an explicit C. 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 and C, 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. @@ -448,9 +466,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 for details on reporting bugs. =head1 AUTHOR @@ -458,7 +474,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006-2009 by Infinity Interactive, Inc. +Copyright 2006-2010 by Infinity Interactive, Inc. L