X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeCoercion%2FUnion.pm;h=6e6f7da4473e0d35ab99d53f24eb730b0f6da72b;hb=0031c50c1ba39e702417ee2bc1cb6a00b7af2cfa;hp=429186de0289511acb9da8c705b8e459129ab52d;hpb=a7be0f8593e4e7b7f570f49027ee4f8f25d4d8bc;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeCoercion/Union.pm b/lib/Moose/Meta/TypeCoercion/Union.pm index 429186d..6e6f7da 100644 --- a/lib/Moose/Meta/TypeCoercion/Union.pm +++ b/lib/Moose/Meta/TypeCoercion/Union.pm @@ -7,97 +7,72 @@ use metaclass; use Scalar::Util 'blessed'; -our $VERSION = '0.69'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - use base 'Moose::Meta::TypeCoercion'; sub compile_type_coercion { my $self = shift; my $type_constraint = $self->type_constraint; - + (blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Union')) - || Moose->throw_error("You can only a Moose::Meta::TypeCoercion::Union for a " . + || Moose->throw_error("You can only create a Moose::Meta::TypeCoercion::Union for a " . "Moose::Meta::TypeConstraint::Union, not a $type_constraint"); - - $self->_compiled_type_coercion(sub { - my $value = shift; - # go through all the type constraints - # in the union, and check em ... - foreach my $type (@{$type_constraint->type_constraints}) { - # if they have a coercion first - if ($type->has_coercion) { - # then try to coerce them ... + + $self->_compiled_type_coercion( + sub { + my $value = shift; + + foreach my $type ( grep { $_->has_coercion } + @{ $type_constraint->type_constraints } ) { my $temp = $type->coerce($value); - # and if they get something - # make sure it still fits within - # the union type ... return $temp if $type_constraint->check($temp); } + + return $value; } - return undef; - }); + ); } sub has_coercion_for_type { 0 } sub add_type_coercions { + require Moose; Moose->throw_error("Cannot add additional type coercions to Union types"); } 1; +# ABSTRACT: The Moose Type Coercion metaclass for Unions + __END__ =pod -=head1 NAME - -Moose::Meta::TypeCoercion::Union - The Moose Type Coercion metaclass for Unions - =head1 DESCRIPTION -For the most part, the only time you will ever encounter an -instance of this class is if you are doing some serious deep -introspection. This API should not be considered final, but -it is B that this will matter to a regular -Moose user. - -If you wish to use features at this depth, please come to the -#moose IRC channel on irc.perl.org and we can talk :) - +This is a subclass of L that is used for +L objects. =head1 METHODS =over 4 -=item B - -=item B +=item B<< $coercion->has_coercion_for_type >> -=item B +This method always returns false. -=item B +=item B<< $coercion->add_type_coercions >> -=back - -=head1 BUGS +This method always throws an error. You cannot add coercions to a +union type coercion. -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. +=item B<< $coercion->coerce($value) >> -=head1 AUTHOR +This method will coerce by trying the coercions for each type in the +union. -Stevan Little Estevan@iinteractive.comE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006-2009 by Infinity Interactive, Inc. +=back -L +=head1 BUGS -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