2 package Moose::Meta::TypeConstraint::Registry;
8 use Scalar::Util 'blessed';
9 use Carp 'confess'; # FIXME Moose->throw_error
11 our $VERSION = '0.60';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
15 use base 'Class::MOP::Object';
17 __PACKAGE__->meta->add_attribute('parent_registry' => (
18 reader => 'get_parent_registry',
19 writer => 'set_parent_registry',
20 predicate => 'has_parent_registry',
23 __PACKAGE__->meta->add_attribute('type_constraints' => (
24 reader => 'type_constraints',
30 my $self = $class->_new(@_);
34 sub has_type_constraint {
35 my ($self, $type_name) = @_;
36 ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0
39 sub get_type_constraint {
40 my ($self, $type_name) = @_;
41 return unless defined $type_name;
42 $self->type_constraints->{$type_name}
45 sub add_type_constraint {
46 my ($self, $type) = @_;
47 confess("No type supplied / type is not a valid type constraint")
48 unless ($type && blessed $type && $type->isa('Moose::Meta::TypeConstraint'));
49 $self->type_constraints->{$type->name} = $type;
52 sub 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;
70 Moose::Meta::TypeConstraint::Registry - registry for type constraints
74 This module is currently only use internally by L<Moose::Util::TypeConstraints>.
75 It can be used to create your own private type constraint registry as well, but
76 the details of that are currently left as an exercise to the reader. (One hint:
77 You can use the 'parent_registry' feature to connect your private version with the
78 base Moose registry and base Moose types will automagically be found too).
88 =item B<get_parent_registry>
90 =item B<set_parent_registry ($registry)>
92 =item B<has_parent_registry>
94 =item B<type_constraints>
96 =item B<has_type_constraint ($type_name)>
98 =item B<get_type_constraint ($type_name)>
100 Returns a type constraint object from the registry by name. Will return
101 false if the supplied type name cannot be found.
103 =item B<add_type_constraint ($type)>
105 Adds a type constraint object to the registry. Will throw an exception if
106 no type is supplied, or the supplied object does not inherit from
107 L<Moose::Meta::TypeConstraint>
109 =item B<find_type_constraint ($type_name)>
115 All complex software has bugs lurking in it, and this module is no
116 exception. If you find a bug please either email me, or add the bug
121 Stevan Little E<lt>stevan@iinteractive.comE<gt>
123 =head1 COPYRIGHT AND LICENSE
125 Copyright 2006-2008 by Infinity Interactive, Inc.
127 L<http://www.iinteractive.com>
129 This library is free software; you can redistribute it and/or modify
130 it under the same terms as Perl itself.