2 package Moose::Meta::TypeConstraint::Registry;
8 use Scalar::Util 'blessed';
10 use base 'Class::MOP::Object';
12 __PACKAGE__->meta->add_attribute('parent_registry' => (
13 reader => 'get_parent_registry',
14 writer => 'set_parent_registry',
15 predicate => 'has_parent_registry',
18 __PACKAGE__->meta->add_attribute('type_constraints' => (
19 reader => 'type_constraints',
25 my $self = $class->_new(@_);
29 sub has_type_constraint {
30 my ($self, $type_name) = @_;
31 ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0
34 sub get_type_constraint {
35 my ($self, $type_name) = @_;
36 return unless defined $type_name;
37 $self->type_constraints->{$type_name}
40 sub add_type_constraint {
41 my ($self, $type) = @_;
43 unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) {
45 Moose->throw_error("No type supplied / type is not a valid type constraint");
48 $self->type_constraints->{$type->name} = $type;
51 sub find_type_constraint {
52 my ($self, $type_name) = @_;
53 return $self->get_type_constraint($type_name)
54 if $self->has_type_constraint($type_name);
55 return $self->get_parent_registry->find_type_constraint($type_name)
56 if $self->has_parent_registry;
62 # ABSTRACT: registry for type constraints
71 This class is a registry that maps type constraint names to
72 L<Moose::Meta::TypeConstraint> objects.
74 Currently, it is only used internally by
75 L<Moose::Util::TypeConstraints>, which creates a single global
80 C<Moose::Meta::TypeConstraint::Registry> is a subclass of
81 L<Class::MOP::Object>.
87 =item B<< Moose::Meta::TypeConstraint::Registry->new(%options) >>
89 This creates a new registry object based on the provided C<%options>:
93 =item * parent_registry
95 This is an optional L<Moose::Meta::TypeConstraint::Registry>
98 =item * type_constraints
100 This is hash reference of type names to type objects. This is
101 optional. Constraints can be added to the registry after it is
106 =item B<< $registry->get_parent_registry >>
108 Returns the registry's parent registry, if it has one.
110 =item B<< $registry->has_parent_registry >>
112 Returns true if the registry has a parent.
114 =item B<< $registry->set_parent_registry($registry) >>
116 Sets the parent registry.
118 =item B<< $registry->get_type_constraint($type_name) >>
120 This returns the L<Moose::Meta::TypeConstraint> object from the
121 registry for the given name, if one exists.
123 =item B<< $registry->has_type_constraint($type_name) >>
125 Returns true if the registry has a type of the given name.
127 =item B<< $registry->add_type_constraint($type) >>
129 Adds a new L<Moose::Meta::TypeConstraint> object to the registry.
131 =item B<< $registry->find_type_constraint($type_name) >>
133 This method looks in the current registry for the named type. If the
134 type is not found, then this method will look in the registry's
135 parent, if it has one.
141 See L<Moose/BUGS> for details on reporting bugs.