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.96';
-$VERSION = eval $VERSION;
-our $AUTHORITY = 'cpan:STEVAN';
-
__PACKAGE__->meta->add_attribute('name' => (reader => 'name'));
__PACKAGE__->meta->add_attribute('parent' => (
reader => 'parent',
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;
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";
}
}
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;
return if $other->has_parent;
}
- return 1;
+ return;
}
sub is_a_type_of {
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
=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.
See L<Moose/BUGS> for details on reporting bugs.
-=head1 AUTHOR
-
-Stevan Little E<lt>stevan@iinteractive.comE<gt>
-
-=head1 COPYRIGHT AND LICENSE
-
-Copyright 2006-2010 by Infinity Interactive, Inc.
-
-L<http://www.iinteractive.com>
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
-
=cut