2 package Moose::Meta::TypeConstraint::Registry;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.71_01';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Object';
16 __PACKAGE__->meta->add_attribute('parent_registry' => (
17 reader => 'get_parent_registry',
18 writer => 'set_parent_registry',
19 predicate => 'has_parent_registry',
22 __PACKAGE__->meta->add_attribute('type_constraints' => (
23 reader => 'type_constraints',
29 my $self = $class->_new(@_);
33 sub has_type_constraint {
34 my ($self, $type_name) = @_;
35 ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0
38 sub get_type_constraint {
39 my ($self, $type_name) = @_;
40 return unless defined $type_name;
41 $self->type_constraints->{$type_name}
44 sub add_type_constraint {
45 my ($self, $type) = @_;
47 unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) {
49 Moose->throw_error("No type supplied / type is not a valid type constraint");
52 $self->type_constraints->{$type->name} = $type;
55 sub 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;
73 Moose::Meta::TypeConstraint::Registry - registry for type constraints
77 This module is currently only use internally by L<Moose::Util::TypeConstraints>.
78 It can be used to create your own private type constraint registry as well, but
79 the details of that are currently left as an exercise to the reader. (One hint:
80 You can use the 'parent_registry' feature to connect your private version with the
81 base Moose registry and base Moose types will automagically be found too).
91 =item B<get_parent_registry>
93 =item B<set_parent_registry ($registry)>
95 =item B<has_parent_registry>
97 =item B<type_constraints>
99 =item B<has_type_constraint ($type_name)>
101 =item B<get_type_constraint ($type_name)>
103 Returns a type constraint object from the registry by name. Will return
104 false if the supplied type name cannot be found.
106 =item B<add_type_constraint ($type)>
108 Adds a type constraint object to the registry. Will throw an exception if
109 no type is supplied, or the supplied object does not inherit from
110 L<Moose::Meta::TypeConstraint>
112 =item B<find_type_constraint ($type_name)>
118 All complex software has bugs lurking in it, and this module is no
119 exception. If you find a bug please either email me, or add the bug
124 Stevan Little E<lt>stevan@iinteractive.comE<gt>
126 =head1 COPYRIGHT AND LICENSE
128 Copyright 2006-2009 by Infinity Interactive, Inc.
130 L<http://www.iinteractive.com>
132 This library is free software; you can redistribute it and/or modify
133 it under the same terms as Perl itself.