X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint%2FParameterizable.pm;h=c85e6af4d76a6049bea2885b6331320ba21c37ed;hb=d344c3feeb1c99183a249e26ddfb0e790101f7b2;hp=19d7d65ff16be67c793ed6873ea723b7016dccaf;hpb=75b9541431131c67521bd1ec75df80c9707bd719;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm index 19d7d65..c85e6af 100644 --- a/lib/Moose/Meta/TypeConstraint/Parameterizable.pm +++ b/lib/Moose/Meta/TypeConstraint/Parameterizable.pm @@ -4,11 +4,13 @@ use strict; use warnings; use metaclass; -our $VERSION = '0.55_01'; +our $VERSION = '0.68'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; use base 'Moose::Meta::TypeConstraint'; +use Moose::Meta::TypeConstraint::Parameterized; +use Moose::Util::TypeConstraints (); __PACKAGE__->meta->add_attribute('constraint_generator' => ( accessor => 'constraint_generator', @@ -40,6 +42,39 @@ sub _can_coerce_constraint_from { }; } +sub _parse_type_parameter { + my ($self, $type_parameter) = @_; + return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter); +} + +sub parameterize { + my ($self, $type_parameter) = @_; + + my $contained_tc = $self->_parse_type_parameter($type_parameter); + + ## The type parameter should be a subtype of the parent's type parameter + ## if there is one. + + if(my $parent = $self->parent) { + if($parent->can('type_parameter')) { + $contained_tc->is_a_type_of($parent->type_parameter) + || Moose->throw_error("$type_parameter is not a subtype of ".$parent->type_parameter); + } + } + + if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) { + my $tc_name = $self->name . '[' . $contained_tc->name . ']'; + return Moose::Meta::TypeConstraint::Parameterized->new( + name => $tc_name, + parent => $self, + type_parameter => $contained_tc, + ); + } + else { + Moose->throw_error("The type parameter must be a Moose meta type"); + } +} + 1; @@ -62,6 +97,11 @@ Moose::Meta::TypeConstraint::Parameterizable - Higher Order type constraints for =item B +=item B + +Given a single type constraint or type constraint string, this method +parameterizes the type based on the given argument. + =item B =back @@ -78,7 +118,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006-2008 by Infinity Interactive, Inc. +Copyright 2006-2009 by Infinity Interactive, Inc. L