bump version to 0.66
[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';
c245d69b 9use Carp 'confess'; # FIXME Moose->throw_error
22aed3c0 10
aa7bbf26 11our $VERSION = '0.66';
e606ae5f 12$VERSION = eval $VERSION;
22aed3c0 13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Class::MOP::Object';
16
183ba44e 17__PACKAGE__->meta->add_attribute('parent_registry' => (
18 reader => 'get_parent_registry',
19 writer => 'set_parent_registry',
20 predicate => 'has_parent_registry',
21));
22
22aed3c0 23__PACKAGE__->meta->add_attribute('type_constraints' => (
24 reader => 'type_constraints',
25 default => sub { {} }
26));
27
28sub new {
29 my $class = shift;
e606ae5f 30 my $self = $class->_new(@_);
22aed3c0 31 return $self;
32}
33
34sub has_type_constraint {
35 my ($self, $type_name) = @_;
4c015454 36 ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0
22aed3c0 37}
38
39sub get_type_constraint {
40 my ($self, $type_name) = @_;
e606ae5f 41 return unless defined $type_name;
22aed3c0 42 $self->type_constraints->{$type_name}
43}
44
45sub add_type_constraint {
46 my ($self, $type) = @_;
e606ae5f 47 confess("No type supplied / type is not a valid type constraint")
48 unless ($type && blessed $type && $type->isa('Moose::Meta::TypeConstraint'));
22aed3c0 49 $self->type_constraints->{$type->name} = $type;
50}
51
183ba44e 52sub find_type_constraint {
53 my ($self, $type_name) = @_;
54 return $self->get_type_constraint($type_name)
55 if $self->has_type_constraint($type_name);
56 return $self->get_parent_registry->find_type_constraint($type_name)
57 if $self->has_parent_registry;
58 return;
59}
60
22aed3c0 611;
62
63__END__
64
65
66=pod
67
68=head1 NAME
69
a0542df9 70Moose::Meta::TypeConstraint::Registry - registry for type constraints
22aed3c0 71
72=head1 DESCRIPTION
73
a0542df9 74This module is currently only use internally by L<Moose::Util::TypeConstraints>.
75It can be used to create your own private type constraint registry as well, but
76the details of that are currently left as an exercise to the reader. (One hint:
77You can use the 'parent_registry' feature to connect your private version with the
78base Moose registry and base Moose types will automagically be found too).
79
22aed3c0 80=head1 METHODS
81
82=over 4
83
84=item B<meta>
85
86=item B<new>
87
183ba44e 88=item B<get_parent_registry>
89
90=item B<set_parent_registry ($registry)>
91
92=item B<has_parent_registry>
93
22aed3c0 94=item B<type_constraints>
95
183ba44e 96=item B<has_type_constraint ($type_name)>
97
98=item B<get_type_constraint ($type_name)>
22aed3c0 99
e606ae5f 100Returns a type constraint object from the registry by name. Will return
101false if the supplied type name cannot be found.
102
183ba44e 103=item B<add_type_constraint ($type)>
22aed3c0 104
e606ae5f 105Adds a type constraint object to the registry. Will throw an exception if
106no type is supplied, or the supplied object does not inherit from
107L<Moose::Meta::TypeConstraint>
108
183ba44e 109=item B<find_type_constraint ($type_name)>
22aed3c0 110
111=back
112
113=head1 BUGS
114
115All complex software has bugs lurking in it, and this module is no
116exception. If you find a bug please either email me, or add the bug
117to cpan-RT.
118
119=head1 AUTHOR
120
121Stevan Little E<lt>stevan@iinteractive.comE<gt>
122
123=head1 COPYRIGHT AND LICENSE
124
2840a3b2 125Copyright 2006-2009 by Infinity Interactive, Inc.
22aed3c0 126
127L<http://www.iinteractive.com>
128
129This library is free software; you can redistribute it and/or modify
130it under the same terms as Perl itself.
131
132=cut