X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FUtil%2FTypeConstraints.pm;h=38961822f2d169ba2443dfeb5c0ab850c72fbe8f;hb=939c3d875f956f33b2c351c951fdd1dc125f196c;hp=2b2d558c4f04efe7f795e1050a91661a6412a1cc;hpb=f152b0997e523b60b2dbcefff8d102fc1096bf49;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Util/TypeConstraints.pm b/lib/Mouse/Util/TypeConstraints.pm index 2b2d558..3896182 100644 --- a/lib/Mouse/Util/TypeConstraints.pm +++ b/lib/Mouse/Util/TypeConstraints.pm @@ -11,7 +11,11 @@ Mouse::Exporter->setup_import_methods( as_is => [qw( as where message optimize_as from via - type subtype coerce class_type role_type enum + + type subtype class_type role_type duck_type + enum + coerce + find_type_constraint )], ); @@ -95,9 +99,7 @@ sub _create_type{ } if(!defined $name){ - if(!defined($name = $args{name})){ - $name = '__ANON__'; - } + $name = $args{name}; } $args{name} = $name; @@ -106,16 +108,20 @@ sub _create_type{ $parent = delete $args{as}; if(!$parent){ $parent = delete $args{name}; - $name = '__ANON__'; + $name = undef; } } - my $package_defined_in = $args{package_defined_in} ||= caller(1); - - my $existing = $TYPE{$name}; - if($existing && $existing->{package_defined_in} ne $package_defined_in){ - confess("The type constraint '$name' has already been created in " - . "$existing->{package_defined_in} and cannot be created again in $package_defined_in"); + if(defined $name){ + my $package_defined_in = $args{package_defined_in} ||= caller(1); + my $existing = $TYPE{$name}; + if($existing && $existing->{package_defined_in} ne $package_defined_in){ + confess("The type constraint '$name' has already been created in " + . "$existing->{package_defined_in} and cannot be created again in $package_defined_in"); + } + } + else{ + $args{name} = '__ANON__'; } $args{constraint} = delete $args{where} if exists $args{where}; @@ -129,7 +135,12 @@ sub _create_type{ $constraint = Mouse::Meta::TypeConstraint->new(%args); } - return $TYPE{$name} = $constraint; + if(defined $name){ + return $TYPE{$name} = $constraint; + } + else{ + return $constraint; + } } sub type { @@ -172,13 +183,20 @@ sub role_type { ); } -sub typecast_constraints { # DEPRECATED - my($class, $pkg, $type, $value) = @_; - Carp::croak("wrong arguments count") unless @_ == 4; +sub duck_type { + my($name, @methods); + + if(!(@_ == 1 && ref($_[0]) eq 'ARRAY')){ + $name = shift; + } - Carp::cluck("typecast_constraints() has been deprecated, which was an internal utility anyway"); + @methods = (@_ == 1 && ref($_[0]) eq 'ARRAY') ? @{$_[0]} : @_; - return $type->coerce($value); + return _create_type 'type', $name => ( + optimized_as => Mouse::Util::generate_can_predicate_for(\@methods), + + type => 'DuckType', + ); } sub enum { @@ -346,7 +364,7 @@ Mouse::Util::TypeConstraints - Type constraint system for Mouse =head1 VERSION -This document describes Mouse version 0.43 +This document describes Mouse version 0.4501 =head2 SYNOPSIS @@ -519,16 +537,26 @@ Returns the names of all the type constraints. =over 4 -=item C<< subtype 'Name' => as 'Parent' => where { } ... -> Mouse::Meta::TypeConstraint >> +=item C<< type $name => where { } ... -> Mouse::Meta::TypeConstraint >> -=item C<< subtype as 'Parent' => where { } ... -> Mouse::Meta::TypeConstraint >> +=item C<< subtype $name => as $parent => where { } ... -> Mouse::Meta::TypeConstraint >> + +=item C<< subtype as $parent => where { } ... -> Mouse::Meta::TypeConstraint >> =item C<< class_type ($class, ?$options) -> Mouse::Meta::TypeConstraint >> =item C<< role_type ($role, ?$options) -> Mouse::Meta::TypeConstraint >> +=item C<< duck_type($name, @methods | \@methods) -> Mouse::Meta::TypeConstraint >> + +=item C<< duck_type(\@methods) -> Mouse::Meta::TypeConstraint >> + +=item C<< enum($name, @values | \@values) -> Mouse::Meta::TypeConstraint >> + =item C<< enum (\@values) -> Mouse::Meta::TypeConstraint >> +=item C<< coerce $type => from $another_type, via { }, ... >> + =back =over 4