X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint.pm;h=989ed2b82f509b4cd8fa55a36e213a7a8e33ff15;hb=56631d7a6a6b033c357239d5ac74b598b371cb41;hp=252179fac8682dbc51e0fe4e5d93588cd87aeafc;hpb=b6cca0d5690feec99436fe952315d5d4feeb9473;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index 252179f..989ed2b 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -15,10 +15,6 @@ use Sub::Name qw(subname); use base qw(Class::MOP::Object); -our $VERSION = '1.14'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - __PACKAGE__->meta->add_attribute('name' => (reader => 'name')); __PACKAGE__->meta->add_attribute('parent' => ( reader => 'parent', @@ -90,6 +86,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; @@ -302,14 +317,12 @@ 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 @@ -383,9 +396,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. @@ -470,17 +491,4 @@ behavior and change how child types are created. See L for details on reporting bugs. -=head1 AUTHOR - -Stevan Little Estevan@iinteractive.comE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006-2010 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. - =cut