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