X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint.pm;h=1ec67a2cd86d224a766044751b80910b109d1ff5;hb=4a315f4b0c6b14bffc21f3337127c6335b7ec15f;hp=1b3196c9fe55e7935dd589f4416252f700d927da;hpb=7a10df4daf77f0efcf873693e0868b845387430d;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index 1b3196c..1ec67a2 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -5,7 +5,9 @@ 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); @@ -13,7 +15,7 @@ use Sub::Name qw(subname); use base qw(Class::MOP::Object); -our $VERSION = '0.93_03'; +our $VERSION = '1.19'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -88,6 +90,25 @@ sub coerce { return $coercion->coerce(@_); } +sub assert_coerce { + my $self = shift; + + my $coercion = $self->coercion; + + unless ($coercion) { + require Moose; + Moose->throw_error("Cannot coerce without a type coercion"); + } + + return $_[0] if $self->check($_[0]); + + my $result = $coercion->coerce(@_); + + $self->assert_valid($result); + + return $result; +} + sub check { my ($self, @args) = @_; my $constraint_subref = $self->_compiled_type_constraint; @@ -122,7 +143,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"; } } @@ -133,7 +154,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; @@ -148,7 +169,7 @@ sub equals { return if $other->has_parent; } - return 1; + return; } sub is_a_type_of { @@ -311,7 +332,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. @@ -381,9 +402,17 @@ C and C. =item B<< $constraint->coerce($value) >> -This will attempt to coerce the value to the type. If the type does +This will attempt to coerce the value to the type. If the type does not have any defined coercions this will throw an error. +If no coercion can produce a value matching C<$constraint>, the original +value is returned. + +=item B<< $constraint->assert_coerce($value) >> + +This method behaves just like C, but if the result is not valid +according to C<$constraint>, an error is thrown. + =item B<< $constraint->check($value) >> Returns true if the given value passes the constraint for the type.