X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint.pm;h=f85cffb9bbe0f516b33696a5163b53d6aa8988f8;hb=5a18346bf3bb376bf1ee3fab06043bb9c8bc6081;hp=6ca3fe56f597a702393093141721d25620a44b83;hpb=e3d94ac0990080399851812a76928c893c3cd4fa;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index 6ca3fe5..f85cffb 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -5,18 +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 Try::Tiny; use base qw(Class::MOP::Object); -our $VERSION = '0.82'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - __PACKAGE__->meta->add_attribute('name' => (reader => 'name')); __PACKAGE__->meta->add_attribute('parent' => ( reader => 'parent', @@ -88,6 +87,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; @@ -104,6 +122,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,8 +139,14 @@ sub get_message { return $msg->($value); } else { - $value = (defined $value ? overload::StrVal($value) : 'undef'); - return "Validation failed for '" . $self->name . "' failed with value $value"; + # have to load it late like this, since it uses Moose itself + if (try { Class::MOP::load_class('Devel::PartialDump'); 1 }) { + $value = Devel::PartialDump->new->dump($value); + } + else { + $value = (defined $value ? overload::StrVal($value) : 'undef'); + } + return "Validation failed for '" . $self->name . "' with value $value"; } } @@ -123,7 +157,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; @@ -138,7 +172,7 @@ sub equals { return if $other->has_parent; } - return 1; + return; } sub is_a_type_of { @@ -290,18 +324,16 @@ sub create_child_type { 1; +# ABSTRACT: The Moose Type Constraint metaclass + __END__ =pod -=head1 NAME - -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. @@ -371,9 +403,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. @@ -385,6 +425,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. @@ -449,21 +496,6 @@ 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. - -=head1 AUTHOR - -Stevan Little Estevan@iinteractive.comE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006-2009 by Infinity Interactive, Inc. - -L - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. +See L for details on reporting bugs. =cut