use base qw(Class::MOP::Object);
-our $VERSION = '1.14';
+our $VERSION = '1.19';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
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;
=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<coerce>, 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.