Version 0.96.
[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
2e7576bd 10our $VERSION = '0.96';
e606ae5f 11$VERSION = eval $VERSION;
22aed3c0 12our $AUTHORITY = 'cpan:STEVAN';
13
14use base 'Class::MOP::Object';
15
183ba44e 16__PACKAGE__->meta->add_attribute('parent_registry' => (
17 reader => 'get_parent_registry',
d03bd989 18 writer => 'set_parent_registry',
19 predicate => 'has_parent_registry',
183ba44e 20));
21
22aed3c0 22__PACKAGE__->meta->add_attribute('type_constraints' => (
23 reader => 'type_constraints',
24 default => sub { {} }
25));
26
d03bd989 27sub new {
22aed3c0 28 my $class = shift;
e606ae5f 29 my $self = $class->_new(@_);
22aed3c0 30 return $self;
31}
32
33sub has_type_constraint {
34 my ($self, $type_name) = @_;
4c015454 35 ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0
22aed3c0 36}
37
38sub get_type_constraint {
39 my ($self, $type_name) = @_;
d03bd989 40 return unless defined $type_name;
22aed3c0 41 $self->type_constraints->{$type_name}
42}
43
44sub add_type_constraint {
45 my ($self, $type) = @_;
70ea9161 46
47 unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) {
48 require Moose;
49 Moose->throw_error("No type supplied / type is not a valid type constraint");
50 }
51
22aed3c0 52 $self->type_constraints->{$type->name} = $type;
53}
54
183ba44e 55sub 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;
61 return;
62}
63
22aed3c0 641;
65
66__END__
67
68
69=pod
70
71=head1 NAME
72
a0542df9 73Moose::Meta::TypeConstraint::Registry - registry for type constraints
22aed3c0 74
75=head1 DESCRIPTION
76
c9861bfb 77This class is a registry that maps type constraint names to
78L<Moose::Meta::TypeConstraint> objects.
79
80Currently, it is only used internally by
81L<Moose::Util::TypeConstraints>, which creates a single global
82registry.
83
84=head1 INHERITANCE
85
86C<Moose::Meta::TypeConstraint::Registry> is a subclass of
87L<Class::MOP::Object>.
a0542df9 88
22aed3c0 89=head1 METHODS
90
91=over 4
92
c9861bfb 93=item B<< Moose::Meta::TypeConstraint::Registry->new(%options) >>
94
95This creates a new registry object based on the provided C<%options>:
96
97=over 8
98
99=item * parent_registry
100
101This is an optional L<Moose::Meta::TypeConstraint::Registry>
102object.
103
104=item * type_constraints
105
106This is hash reference of type names to type objects. This is
107optional. Constraints can be added to the registry after it is
108created.
109
110=back
111
112=item B<< $registry->get_parent_registry >>
113
114Returns the registry's parent registry, if it has one.
115
116=item B<< $registry->has_parent_registry >>
117
118Returns true if the registry has a parent.
22aed3c0 119
c9861bfb 120=item B<< $registry->set_parent_registry($registry) >>
22aed3c0 121
c9861bfb 122Sets the parent registry.
183ba44e 123
c9861bfb 124=item B<< $registry->get_type_constraint($type_name) >>
183ba44e 125
c9861bfb 126This returns the L<Moose::Meta::TypeConstraint> object from the
127registry for the given name, if one exists.
22aed3c0 128
c9861bfb 129=item B<< $registry->has_type_constraint($type_name) >>
183ba44e 130
c9861bfb 131Returns true if the registry has a type of the given name.
22aed3c0 132
c9861bfb 133=item B<< $registry->add_type_constraint($type) >>
e606ae5f 134
c9861bfb 135Adds a new L<Moose::Meta::TypeConstraint> object to the registry.
22aed3c0 136
c9861bfb 137=item B<< $registry->find_type_constraint($type_name) >>
e606ae5f 138
c9861bfb 139This method looks in the current registry for the named type. If the
140type is not found, then this method will look in the registry's
141parent, if it has one.
22aed3c0 142
143=back
144
145=head1 BUGS
146
d4048ef3 147See L<Moose/BUGS> for details on reporting bugs.
22aed3c0 148
149=head1 AUTHOR
150
151Stevan Little E<lt>stevan@iinteractive.comE<gt>
152
153=head1 COPYRIGHT AND LICENSE
154
7e0492d3 155Copyright 2006-2010 by Infinity Interactive, Inc.
22aed3c0 156
157L<http://www.iinteractive.com>
158
159This library is free software; you can redistribute it and/or modify
160it under the same terms as Perl itself.
161
162=cut