X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint%2FDuckType.pm;h=4b3d0c6dadb0c30718d6faae68f33253ddd4f399;hb=964294c1854e01a6dafe324c4f392acb528150a8;hp=2b9ed8381d95db00eecfef0919779f694bfda37d;hpb=113d3174264db82cb788bc846f617584072cba39;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint/DuckType.pm b/lib/Moose/Meta/TypeConstraint/DuckType.pm index 2b9ed83..4b3d0c6 100644 --- a/lib/Moose/Meta/TypeConstraint/DuckType.pm +++ b/lib/Moose/Meta/TypeConstraint/DuckType.pm @@ -4,26 +4,43 @@ use strict; use warnings; use metaclass; +use B; use Scalar::Util 'blessed'; use List::MoreUtils qw(all); use Moose::Util 'english_list'; use Moose::Util::TypeConstraints (); -our $VERSION = '0.91'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - use base 'Moose::Meta::TypeConstraint'; __PACKAGE__->meta->add_attribute('methods' => ( accessor => 'methods', )); +my $inliner = sub { + my $self = shift; + my $val = shift; + + return + "Scalar::Util::blessed($val)" + . qq{&& Scalar::Util::blessed($val) ne 'Regexp'} + . "&& &List::MoreUtils::all( sub { $val->can(\$_) }, " + . ( join ', ', map { B::perlstring($_) } @{ $self->methods } ) . ' )'; +}; + sub new { my ( $class, %args ) = @_; - $args{parent} = Moose::Util::TypeConstraints::find_type_constraint('Object'); + $args{parent} + = Moose::Util::TypeConstraints::find_type_constraint('Object'); + + my @methods = @{ $args{methods} }; + $args{constraint} = sub { + blessed( $_[0] ) ne 'Regexp' + && all { $_[0]->can($_) } @methods; + }; + + $args{inlined} = $inliner; my $self = $class->_new(\%args); @@ -102,14 +119,12 @@ sub get_message { 1; +# ABSTRACT: Type constraint for duck typing + __END__ =pod -=head1 NAME - -Moose::Meta::TypeConstraint::DuckType - Type constraint for duck typing - =head1 DESCRIPTION This class represents type constraints based on an enumerated list of @@ -154,24 +169,7 @@ object! =head1 BUGS -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. - -=head1 AUTHOR - -Chris Prather Echris@prather.orgE - -Shawn M Moore Esartak@gmail.comE - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006-2009 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. +See L for details on reporting bugs. =cut