X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint.pm;h=0dd9126b772069289f7d4b9a00ce7b13bb2d46a5;hb=1dc5ef70c3d58834c76e3294791529bdc64e2b75;hp=76ea01d0b17a3449fa95972db3870e8f010e3190;hpb=6f9ff1af2a8de6a0c55f6cc7aa436bd9c3ad08a6;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index 76ea01d..0dd9126 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -12,7 +12,7 @@ use Sub::Name 'subname'; use Carp 'confess'; use Scalar::Util 'blessed'; -our $VERSION = '0.10'; +our $VERSION = '0.11'; our $AUTHORITY = 'cpan:STEVAN'; __PACKAGE__->meta->add_attribute('name' => (reader => 'name')); @@ -58,23 +58,29 @@ sub new { } sub coerce { ((shift)->coercion || confess "Cannot coerce without a type coercion")->coerce(@_) } -sub check { $_[0]->_compiled_type_constraint->($_[1]) } +sub check { $_[0]->_compiled_type_constraint->($_[1]) ? 1 : undef } sub validate { my ($self, $value) = @_; if ($self->_compiled_type_constraint->($value)) { return undef; } else { - if ($self->has_message) { - local $_ = $value; - return $self->message->($value); - } - else { - return "Validation failed for '" . $self->name . "' failed"; - } + $self->get_message($value); } } +sub get_message { + my ($self, $value) = @_; + $value = (defined $value ? overload::StrVal($value) : 'undef'); + if (my $msg = $self->message) { + local $_ = $value; + return $msg->($value); + } + else { + return "Validation failed for '" . $self->name . "' failed with value $value"; + } +} + ## type predicates ... sub is_a_type_of { @@ -124,11 +130,9 @@ sub _compile_hand_optimized_type_constraint { my $type_constraint = $self->hand_optimized_type_constraint; - return sub { - confess unless ref $type_constraint; - return undef unless $type_constraint->($_[0]); - return 1; - }; + confess unless ref $type_constraint; + + return $type_constraint; } sub _compile_subtype { @@ -251,6 +255,8 @@ the C will be used to construct a custom error message. =item B +=item B + =item B =item B @@ -284,7 +290,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006, 2007 by Infinity Interactive, Inc. +Copyright 2006-2008 by Infinity Interactive, Inc. L