X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FTypeConstraint.pm;h=51b08670f70e0338af416f735163696202903528;hb=9c85e9dc1fea8af52dde98b81ad345eb44e79242;hp=538e3b288a6c45b1a82223783e36f8ca12653380;hpb=74be9f760d045a0174d76e54de1c66d6b66454c4;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/TypeConstraint.pm b/lib/Mouse/Meta/TypeConstraint.pm index 538e3b2..51b0867 100644 --- a/lib/Mouse/Meta/TypeConstraint.pm +++ b/lib/Mouse/Meta/TypeConstraint.pm @@ -1,6 +1,8 @@ package Mouse::Meta::TypeConstraint; use strict; use warnings; +use Carp (); + use overload '""' => sub { shift->{name} }, # stringify to tc name fallback => 1; @@ -28,6 +30,26 @@ sub check { $self->{_compiled_type_constraint}->(@_); } +sub validate { + my ($self, $value) = @_; + if ($self->{_compiled_type_constraint}->($value)) { + return undef; + } + else { + $self->get_message($value); + } +} + +sub assert_valid { + my ($self, $value) = @_; + + my $error = $self->validate($value); + return 1 if ! defined $error; + + Carp::confess($error); +} + + sub message { return $_[0]->{message}; }