bump version
[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
9409e92e 10our $VERSION = '0.72';
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',
18 writer => 'set_parent_registry',
19 predicate => 'has_parent_registry',
20));
21
22aed3c0 22__PACKAGE__->meta->add_attribute('type_constraints' => (
23 reader => 'type_constraints',
24 default => sub { {} }
25));
26
27sub new {
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) = @_;
e606ae5f 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
a0542df9 77This module is currently only use internally by L<Moose::Util::TypeConstraints>.
78It can be used to create your own private type constraint registry as well, but
79the details of that are currently left as an exercise to the reader. (One hint:
80You can use the 'parent_registry' feature to connect your private version with the
81base Moose registry and base Moose types will automagically be found too).
82
22aed3c0 83=head1 METHODS
84
85=over 4
86
87=item B<meta>
88
89=item B<new>
90
183ba44e 91=item B<get_parent_registry>
92
93=item B<set_parent_registry ($registry)>
94
95=item B<has_parent_registry>
96
22aed3c0 97=item B<type_constraints>
98
183ba44e 99=item B<has_type_constraint ($type_name)>
100
101=item B<get_type_constraint ($type_name)>
22aed3c0 102
e606ae5f 103Returns a type constraint object from the registry by name. Will return
104false if the supplied type name cannot be found.
105
183ba44e 106=item B<add_type_constraint ($type)>
22aed3c0 107
e606ae5f 108Adds a type constraint object to the registry. Will throw an exception if
109no type is supplied, or the supplied object does not inherit from
110L<Moose::Meta::TypeConstraint>
111
183ba44e 112=item B<find_type_constraint ($type_name)>
22aed3c0 113
114=back
115
116=head1 BUGS
117
118All complex software has bugs lurking in it, and this module is no
119exception. If you find a bug please either email me, or add the bug
120to cpan-RT.
121
122=head1 AUTHOR
123
124Stevan Little E<lt>stevan@iinteractive.comE<gt>
125
126=head1 COPYRIGHT AND LICENSE
127
2840a3b2 128Copyright 2006-2009 by Infinity Interactive, Inc.
22aed3c0 129
130L<http://www.iinteractive.com>
131
132This library is free software; you can redistribute it and/or modify
133it under the same terms as Perl itself.
134
135=cut