2 package Moose::Meta::TypeConstraint::Registry;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.87';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Object';
16 __PACKAGE__->meta->add_attribute('parent_registry' => (
17 reader => 'get_parent_registry',
18 writer => 'set_parent_registry',
19 predicate => 'has_parent_registry',
22 __PACKAGE__->meta->add_attribute('type_constraints' => (
23 reader => 'type_constraints',
29 my $self = $class->_new(@_);
33 sub has_type_constraint {
34 my ($self, $type_name) = @_;
35 ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0
38 sub get_type_constraint {
39 my ($self, $type_name) = @_;
40 return unless defined $type_name;
41 $self->type_constraints->{$type_name}
44 sub add_type_constraint {
45 my ($self, $type) = @_;
47 unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) {
49 Moose->throw_error("No type supplied / type is not a valid type constraint");
52 $self->type_constraints->{$type->name} = $type;
55 sub find_type_constraint {
56 my ($self, $type_name) = @_;
57 return $self->get_type_constraint($type_name)
58 if $self->has_type_constraint($type_name);
59 return $self->get_parent_registry->find_type_constraint($type_name)
60 if $self->has_parent_registry;
73 Moose::Meta::TypeConstraint::Registry - registry for type constraints
77 This class is a registry that maps type constraint names to
78 L<Moose::Meta::TypeConstraint> objects.
80 Currently, it is only used internally by
81 L<Moose::Util::TypeConstraints>, which creates a single global
86 C<Moose::Meta::TypeConstraint::Registry> is a subclass of
87 L<Class::MOP::Object>.
93 =item B<< Moose::Meta::TypeConstraint::Registry->new(%options) >>
95 This creates a new registry object based on the provided C<%options>:
99 =item * parent_registry
101 This is an optional L<Moose::Meta::TypeConstraint::Registry>
104 =item * type_constraints
106 This is hash reference of type names to type objects. This is
107 optional. Constraints can be added to the registry after it is
112 =item B<< $registry->get_parent_registry >>
114 Returns the registry's parent registry, if it has one.
116 =item B<< $registry->has_parent_registry >>
118 Returns true if the registry has a parent.
120 =item B<< $registry->set_parent_registry($registry) >>
122 Sets the parent registry.
124 =item B<< $registry->get_type_constraint($type_name) >>
126 This returns the L<Moose::Meta::TypeConstraint> object from the
127 registry for the given name, if one exists.
129 =item B<< $registry->has_type_constraint($type_name) >>
131 Returns true if the registry has a type of the given name.
133 =item B<< $registry->add_type_constraint($type) >>
135 Adds a new L<Moose::Meta::TypeConstraint> object to the registry.
137 =item B<< $registry->find_type_constraint($type_name) >>
139 This method looks in the current registry for the named type. If the
140 type is not found, then this method will look in the registry's
141 parent, if it has one.
147 All complex software has bugs lurking in it, and this module is no
148 exception. If you find a bug please either email me, or add the bug
153 Stevan Little E<lt>stevan@iinteractive.comE<gt>
155 =head1 COPYRIGHT AND LICENSE
157 Copyright 2006-2009 by Infinity Interactive, Inc.
159 L<http://www.iinteractive.com>
161 This library is free software; you can redistribute it and/or modify
162 it under the same terms as Perl itself.