1 package MooseX::Meta::TypeConstraint::ForceCoercion;
4 # ABSTRACT: Force coercion when validating type constraints
7 use namespace::autoclean;
11 has _type_constraint => (
13 isa => 'Moose::Meta::TypeConstraint',
14 init_arg => 'type_constraint',
20 my ($self, $value) = @_;
21 my $coerced = $self->_type_constraint->coerce($value);
22 return undef if $coerced == $value;
23 return $self->_type_constraint->check($coerced);
28 my ($self, $value, $coerced_ref) = @_;
29 my $coerced = $self->_type_constraint->coerce($value);
30 return 'Coercion failed' if $coerced == $value;
31 ${ $coerced_ref } = $coerced if $coerced_ref;
32 return $self->_type_constraint->validate($coerced);
35 my $meta = __PACKAGE__->meta;
37 for my $meth (qw/isa can meta/) {
38 my $orig = __PACKAGE__->can($meth);
39 $meta->add_method($meth => sub {
41 return $self->$orig(@_) unless blessed $self;
43 my $tc = $self->_type_constraint;
44 # this might happen during global destruction
45 return $self->$orig(@_) unless $tc;
47 return $tc->$meth(@_);
53 my ($meth) = (our $AUTOLOAD =~ /([^:]+)$/);
54 return unless blessed $self;
56 my $tc = $self->_type_constraint;
59 return $tc->$meth(@_);
62 $meta->make_immutable;
69 MooseX::Meta::TypeConstraint::ForceCoercion - Force coercion when validating type constraints
77 use MooseX::Types:::Moose qw/Str Any/;
78 use Moose::Util::TypeConstraints;
79 use MooseX::Meta::TypeConstraint::ForceCoercion;
81 # get any type constraint
84 # declare one or more coercions for it
89 # wrap the $tc to force coercion
90 my $coercing_tc = MooseX::Meta::TypeConstraint::ForceCoercion->new(
91 type_constraint => $tc,
94 # check a value against new type constraint. this will run the type
95 # coercions for the wrapped type, even if the value already passes
96 # validation before coercion. it will fail if the value couldn't be
98 $coercing_tc->check('Affe');
102 This class allows to wrap any C<Moose::Meta::TypeConstraint> in a way that will
103 force coercion of the value when checking or validating a value against it.
109 =head2 type_constraint
111 The type constraint to wrap. All methods except for C<validate> and C<check>
112 are delegated to the value of this attribute.
118 =head2 check ($value)
120 Same as C<Moose::Meta::TypeConstraint::check>, except it will always try to
121 coerce C<$value> before checking it against the actual type constraint. If
122 coercing fails the check will fail, too.
126 =head2 validate ($value, $coerced_ref?)
128 Same as C<Moose::Meta::TypeConstraint::validate>, except it will always try to
129 coerce C<$value> before validating it against the actual type constraint. If
130 coercing fails the validation will fail, too.
132 If coercion was successful and a C<$coerced_ref> references was passed, the
133 coerced value will be stored in that.
137 Florian Ragwitz <rafl@debian.org>
139 =head1 COPYRIGHT AND LICENSE
141 This software is copyright (c) 2009 by Florian Ragwitz.
143 This is free software; you can redistribute it and/or modify it under
144 the same terms as perl itself.