X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint%2FRegistry.pm;h=d95963250f095a91e5de2ae743ef7f1ba3014f4e;hb=46f7e6a545f17def944b291b05cd5c725bdacead;hp=c1414d03d914f90fcbada282c7826fd8f7db1279;hpb=22aed3c0fbe058c938ecb2834eb8a45c46d4e8ed;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint/Registry.pm b/lib/Moose/Meta/TypeConstraint/Registry.pm index c1414d0..d959632 100644 --- a/lib/Moose/Meta/TypeConstraint/Registry.pm +++ b/lib/Moose/Meta/TypeConstraint/Registry.pm @@ -6,13 +6,20 @@ use warnings; use metaclass; use Scalar::Util 'blessed'; -use Carp 'confess'; +use Carp 'confess'; # FIXME Moose->throw_error -our $VERSION = '0.01'; +our $VERSION = '0.57'; +$VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Object'; +__PACKAGE__->meta->add_attribute('parent_registry' => ( + reader => 'get_parent_registry', + writer => 'set_parent_registry', + predicate => 'has_parent_registry', +)); + __PACKAGE__->meta->add_attribute('type_constraints' => ( reader => 'type_constraints', default => sub { {} } @@ -20,7 +27,7 @@ __PACKAGE__->meta->add_attribute('type_constraints' => ( sub new { my $class = shift; - my $self = $class->meta->new_object(@_); + my $self = $class->_new(@_); return $self; } @@ -31,14 +38,26 @@ sub has_type_constraint { sub get_type_constraint { my ($self, $type_name) = @_; + return unless defined $type_name; $self->type_constraints->{$type_name} } sub add_type_constraint { my ($self, $type) = @_; + confess("No type supplied / type is not a valid type constraint") + unless ($type && blessed $type && $type->isa('Moose::Meta::TypeConstraint')); $self->type_constraints->{$type->name} = $type; } +sub find_type_constraint { + my ($self, $type_name) = @_; + return $self->get_type_constraint($type_name) + if $self->has_type_constraint($type_name); + return $self->get_parent_registry->find_type_constraint($type_name) + if $self->has_parent_registry; + return; +} + 1; __END__ @@ -48,10 +67,16 @@ __END__ =head1 NAME -Moose::Meta::TypeConstraint::Registry +Moose::Meta::TypeConstraint::Registry - registry for type constraints =head1 DESCRIPTION +This module is currently only use internally by L. +It can be used to create your own private type constraint registry as well, but +the details of that are currently left as an exercise to the reader. (One hint: +You can use the 'parent_registry' feature to connect your private version with the +base Moose registry and base Moose types will automagically be found too). + =head1 METHODS =over 4 @@ -60,13 +85,28 @@ Moose::Meta::TypeConstraint::Registry =item B +=item B + +=item B + +=item B + =item B -=item B +=item B + +=item B + +Returns a type constraint object from the registry by name. Will return +false if the supplied type name cannot be found. + +=item B -=item B +Adds a type constraint object to the registry. Will throw an exception if +no type is supplied, or the supplied object does not inherit from +L -=item B +=item B =back @@ -82,7 +122,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006, 2007 by Infinity Interactive, Inc. +Copyright 2006-2008 by Infinity Interactive, Inc. L