Use dzil Authority plugin - remove $AUTHORITY from code
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Registry.pm
CommitLineData
22aed3c0 1
2package Moose::Meta::TypeConstraint::Registry;
3
4use strict;
5use warnings;
6use metaclass;
7
8use Scalar::Util 'blessed';
22aed3c0 9
22aed3c0 10use base 'Class::MOP::Object';
11
183ba44e 12__PACKAGE__->meta->add_attribute('parent_registry' => (
13 reader => 'get_parent_registry',
d03bd989 14 writer => 'set_parent_registry',
15 predicate => 'has_parent_registry',
183ba44e 16));
17
22aed3c0 18__PACKAGE__->meta->add_attribute('type_constraints' => (
19 reader => 'type_constraints',
20 default => sub { {} }
21));
22
d03bd989 23sub new {
22aed3c0 24 my $class = shift;
e606ae5f 25 my $self = $class->_new(@_);
22aed3c0 26 return $self;
27}
28
29sub has_type_constraint {
30 my ($self, $type_name) = @_;
4c015454 31 ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0
22aed3c0 32}
33
34sub get_type_constraint {
35 my ($self, $type_name) = @_;
d03bd989 36 return unless defined $type_name;
22aed3c0 37 $self->type_constraints->{$type_name}
38}
39
40sub add_type_constraint {
41 my ($self, $type) = @_;
70ea9161 42
43 unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) {
44 require Moose;
45 Moose->throw_error("No type supplied / type is not a valid type constraint");
46 }
47
22aed3c0 48 $self->type_constraints->{$type->name} = $type;
49}
50
183ba44e 51sub 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;
57 return;
58}
59
22aed3c0 601;
61
ad46f524 62# ABSTRACT: registry for type constraints
63
22aed3c0 64__END__
65
66
67=pod
68
22aed3c0 69=head1 DESCRIPTION
70
c9861bfb 71This class is a registry that maps type constraint names to
72L<Moose::Meta::TypeConstraint> objects.
73
74Currently, it is only used internally by
75L<Moose::Util::TypeConstraints>, which creates a single global
76registry.
77
78=head1 INHERITANCE
79
80C<Moose::Meta::TypeConstraint::Registry> is a subclass of
81L<Class::MOP::Object>.
a0542df9 82
22aed3c0 83=head1 METHODS
84
85=over 4
86
c9861bfb 87=item B<< Moose::Meta::TypeConstraint::Registry->new(%options) >>
88
89This creates a new registry object based on the provided C<%options>:
90
91=over 8
92
93=item * parent_registry
94
95This is an optional L<Moose::Meta::TypeConstraint::Registry>
96object.
97
98=item * type_constraints
99
100This is hash reference of type names to type objects. This is
101optional. Constraints can be added to the registry after it is
102created.
103
104=back
105
106=item B<< $registry->get_parent_registry >>
107
108Returns the registry's parent registry, if it has one.
109
110=item B<< $registry->has_parent_registry >>
111
112Returns true if the registry has a parent.
22aed3c0 113
c9861bfb 114=item B<< $registry->set_parent_registry($registry) >>
22aed3c0 115
c9861bfb 116Sets the parent registry.
183ba44e 117
c9861bfb 118=item B<< $registry->get_type_constraint($type_name) >>
183ba44e 119
c9861bfb 120This returns the L<Moose::Meta::TypeConstraint> object from the
121registry for the given name, if one exists.
22aed3c0 122
c9861bfb 123=item B<< $registry->has_type_constraint($type_name) >>
183ba44e 124
c9861bfb 125Returns true if the registry has a type of the given name.
22aed3c0 126
c9861bfb 127=item B<< $registry->add_type_constraint($type) >>
e606ae5f 128
c9861bfb 129Adds a new L<Moose::Meta::TypeConstraint> object to the registry.
22aed3c0 130
c9861bfb 131=item B<< $registry->find_type_constraint($type_name) >>
e606ae5f 132
c9861bfb 133This method looks in the current registry for the named type. If the
134type is not found, then this method will look in the registry's
135parent, if it has one.
22aed3c0 136
137=back
138
139=head1 BUGS
140
d4048ef3 141See L<Moose/BUGS> for details on reporting bugs.
22aed3c0 142
22aed3c0 143=cut