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',
16 Class::MOP::_definition_context(),
19 __PACKAGE__->meta->add_attribute('type_constraints' => (
20 reader => 'type_constraints',
21 default => sub { {} },
22 Class::MOP::_definition_context(),
27 my $self = $class->_new(@_);
31 sub has_type_constraint {
32 my ($self, $type_name) = @_;
33 ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0
36 sub get_type_constraint {
37 my ($self, $type_name) = @_;
38 return unless defined $type_name;
39 $self->type_constraints->{$type_name}
42 sub add_type_constraint {
43 my ($self, $type) = @_;
45 unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) {
47 Moose->throw_error("No type supplied / type is not a valid type constraint");
50 $self->type_constraints->{$type->name} = $type;
53 sub find_type_constraint {
54 my ($self, $type_name) = @_;
55 return $self->get_type_constraint($type_name)
56 if $self->has_type_constraint($type_name);
57 return $self->get_parent_registry->find_type_constraint($type_name)
58 if $self->has_parent_registry;
64 # ABSTRACT: registry for type constraints
73 This class is a registry that maps type constraint names to
74 L<Moose::Meta::TypeConstraint> objects.
76 Currently, it is only used internally by
77 L<Moose::Util::TypeConstraints>, which creates a single global
82 C<Moose::Meta::TypeConstraint::Registry> is a subclass of
83 L<Class::MOP::Object>.
89 =item B<< Moose::Meta::TypeConstraint::Registry->new(%options) >>
91 This creates a new registry object based on the provided C<%options>:
95 =item * parent_registry
97 This is an optional L<Moose::Meta::TypeConstraint::Registry>
100 =item * type_constraints
102 This is hash reference of type names to type objects. This is
103 optional. Constraints can be added to the registry after it is
108 =item B<< $registry->get_parent_registry >>
110 Returns the registry's parent registry, if it has one.
112 =item B<< $registry->has_parent_registry >>
114 Returns true if the registry has a parent.
116 =item B<< $registry->set_parent_registry($registry) >>
118 Sets the parent registry.
120 =item B<< $registry->get_type_constraint($type_name) >>
122 This returns the L<Moose::Meta::TypeConstraint> object from the
123 registry for the given name, if one exists.
125 =item B<< $registry->has_type_constraint($type_name) >>
127 Returns true if the registry has a type of the given name.
129 =item B<< $registry->add_type_constraint($type) >>
131 Adds a new L<Moose::Meta::TypeConstraint> object to the registry.
133 =item B<< $registry->find_type_constraint($type_name) >>
135 This method looks in the current registry for the named type. If the
136 type is not found, then this method will look in the registry's
137 parent, if it has one.
143 See L<Moose/BUGS> for details on reporting bugs.