X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FUtil%2FTypeConstraints.pm;h=732797fb87d2a2111e88b00310ee5fe0b9fce204;hb=7dbebb1bd235797a97c433d40c50a70628a07e3f;hp=a10e7a4c04b842bd6408bb801af1b287a15e351e;hpb=ee413bc51b36b2e61fdbcdc2244d40b64d2bb4d9;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Util/TypeConstraints.pm b/lib/Mouse/Util/TypeConstraints.pm index a10e7a4..732797f 100644 --- a/lib/Mouse/Util/TypeConstraints.pm +++ b/lib/Mouse/Util/TypeConstraints.pm @@ -1,11 +1,12 @@ -package Mouse::TypeRegistry; +package Mouse::Util::TypeConstraints; use strict; use warnings; use Carp (); use Scalar::Util qw/blessed looks_like_number openhandle/; -my %SUBTYPE; +my %TYPE; +my %TYPE_SOURCE; my %COERCE; my %COERCE_KEYS; @@ -21,6 +22,7 @@ sub import { *{"$caller\::message"} = \&_message; *{"$caller\::from"} = \&_from; *{"$caller\::via"} = \&_via; + *{"$caller\::type"} = \&_type; *{"$caller\::subtype"} = \&_subtype; *{"$caller\::coerce"} = \&_coerce; *{"$caller\::class_type"} = \&_class_type; @@ -47,7 +49,7 @@ my $optimized_constraints; my $optimized_constraints_base; { no warnings 'uninitialized'; - %SUBTYPE = ( + %TYPE = ( Any => sub { 1 }, Item => sub { 1 }, Bool => sub { @@ -70,33 +72,48 @@ my $optimized_constraints_base; GlobRef => sub { ref($_) eq 'GLOB' }, FileHandle => sub { - ref($_) eq 'GLOB' - && openhandle($_) + ref($_) eq 'GLOB' && openhandle($_) or - blessed($_) - && $_->isa("IO::Handle") - }, + blessed($_) && $_->isa("IO::Handle") + }, Object => sub { blessed($_) && blessed($_) ne 'Regexp' }, ); - sub optimized_constraints { \%SUBTYPE } - my @SUBTYPE_KEYS = keys %SUBTYPE; - sub list_all_builtin_type_constraints { @SUBTYPE_KEYS } + sub optimized_constraints { \%TYPE } + my @TYPE_KEYS = keys %TYPE; + sub list_all_builtin_type_constraints { @TYPE_KEYS } + + @TYPE_SOURCE{@TYPE_KEYS} = (__PACKAGE__) x @TYPE_KEYS; +} + +sub _type { + my $pkg = caller(0); + my($name, %conf) = @_; + if (my $type = $TYPE{$name}) { + Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg"; + }; + my $constraint = $conf{where} || do { $TYPE{delete $conf{as} || 'Any' } }; + + $TYPE_SOURCE{$name} = $pkg; + $TYPE{$name} = $constraint; } sub _subtype { my $pkg = caller(0); my($name, %conf) = @_; - if (my $type = $SUBTYPE{$name}) { - Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg"; + if (my $type = $TYPE{$name}) { + Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg"; }; - my $stuff = $conf{where} || do { $SUBTYPE{delete $conf{as} || 'Any' } }; - my $as = $conf{as} || ''; - if ($as = $SUBTYPE{$as}) { - $SUBTYPE{$name} = sub { $as->($_) && $stuff->($_) }; + my $constraint = $conf{where} || do { $TYPE{delete $conf{as} || 'Any' } }; + my $as = $conf{as} || ''; + + $TYPE_SOURCE{$name} = $pkg; + + if ($as = $TYPE{$as}) { + $TYPE{$name} = sub { $as->($_) && $constraint->($_) }; } else { - $SUBTYPE{$name} = $stuff; + $TYPE{$name} = $constraint; } } @@ -104,7 +121,7 @@ sub _coerce { my($name, %conf) = @_; Carp::croak "Cannot find type '$name', perhaps you forgot to load it." - unless $SUBTYPE{$name}; + unless $TYPE{$name}; unless ($COERCE{$name}) { $COERCE{$name} = {}; @@ -115,7 +132,7 @@ sub _coerce { if $COERCE{$name}->{$type}; Carp::croak "Could not find the type constraint ($type) to coerce from" - unless $SUBTYPE{$type}; + unless $TYPE{$type}; push @{ $COERCE_KEYS{$name} }, $type; $COERCE{$name}->{$type} = $code; @@ -151,7 +168,7 @@ sub typecast_constraints { next unless $COERCE{$type}; for my $coerce_type (@{ $COERCE_KEYS{$type}}) { $_ = $value; - next unless $SUBTYPE{$coerce_type}->(); + next unless $TYPE{$coerce_type}->(); $_ = $value; $_ = $COERCE{$type}->{$coerce_type}->(); return $_ if $type_constraint->(); @@ -166,7 +183,7 @@ __END__ =head1 NAME -Mouse::TypeRegistry - simple type constraints +Mouse::Util::TypeConstraints - simple type constraints =head1 METHODS