Commit | Line | Data |
22aed3c0 |
1 | |
2 | package Moose::Meta::TypeConstraint::Registry; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | use metaclass; |
7 | |
8 | use Scalar::Util 'blessed'; |
22aed3c0 |
9 | |
22aed3c0 |
10 | use base 'Class::MOP::Object'; |
11 | |
183ba44e |
12 | __PACKAGE__->meta->add_attribute('parent_registry' => ( |
13 | reader => 'get_parent_registry', |
d03bd989 |
14 | writer => 'set_parent_registry', |
15 | predicate => 'has_parent_registry', |
183ba44e |
16 | )); |
17 | |
22aed3c0 |
18 | __PACKAGE__->meta->add_attribute('type_constraints' => ( |
19 | reader => 'type_constraints', |
20 | default => sub { {} } |
21 | )); |
22 | |
d03bd989 |
23 | sub new { |
22aed3c0 |
24 | my $class = shift; |
e606ae5f |
25 | my $self = $class->_new(@_); |
22aed3c0 |
26 | return $self; |
27 | } |
28 | |
29 | sub has_type_constraint { |
30 | my ($self, $type_name) = @_; |
4c015454 |
31 | ($type_name and exists $self->type_constraints->{$type_name}) ? 1 : 0 |
22aed3c0 |
32 | } |
33 | |
34 | sub get_type_constraint { |
35 | my ($self, $type_name) = @_; |
d03bd989 |
36 | return unless defined $type_name; |
22aed3c0 |
37 | $self->type_constraints->{$type_name} |
38 | } |
39 | |
40 | sub add_type_constraint { |
41 | my ($self, $type) = @_; |
70ea9161 |
42 | |
43 | unless ( $type && blessed $type && $type->isa('Moose::Meta::TypeConstraint') ) { |
44 | require Moose; |
45 | Moose->throw_error("No type supplied / type is not a valid type constraint"); |
46 | } |
47 | |
22aed3c0 |
48 | $self->type_constraints->{$type->name} = $type; |
49 | } |
50 | |
183ba44e |
51 | sub find_type_constraint { |
52 | my ($self, $type_name) = @_; |
53 | return $self->get_type_constraint($type_name) |
54 | if $self->has_type_constraint($type_name); |
55 | return $self->get_parent_registry->find_type_constraint($type_name) |
56 | if $self->has_parent_registry; |
57 | return; |
58 | } |
59 | |
22aed3c0 |
60 | 1; |
61 | |
ad46f524 |
62 | # ABSTRACT: registry for type constraints |
63 | |
22aed3c0 |
64 | __END__ |
65 | |
66 | |
67 | =pod |
68 | |
22aed3c0 |
69 | =head1 DESCRIPTION |
70 | |
c9861bfb |
71 | This class is a registry that maps type constraint names to |
72 | L<Moose::Meta::TypeConstraint> objects. |
73 | |
74 | Currently, it is only used internally by |
75 | L<Moose::Util::TypeConstraints>, which creates a single global |
76 | registry. |
77 | |
78 | =head1 INHERITANCE |
79 | |
80 | C<Moose::Meta::TypeConstraint::Registry> is a subclass of |
81 | L<Class::MOP::Object>. |
a0542df9 |
82 | |
22aed3c0 |
83 | =head1 METHODS |
84 | |
85 | =over 4 |
86 | |
c9861bfb |
87 | =item B<< Moose::Meta::TypeConstraint::Registry->new(%options) >> |
88 | |
89 | This creates a new registry object based on the provided C<%options>: |
90 | |
91 | =over 8 |
92 | |
93 | =item * parent_registry |
94 | |
95 | This is an optional L<Moose::Meta::TypeConstraint::Registry> |
96 | object. |
97 | |
98 | =item * type_constraints |
99 | |
100 | This is hash reference of type names to type objects. This is |
101 | optional. Constraints can be added to the registry after it is |
102 | created. |
103 | |
104 | =back |
105 | |
106 | =item B<< $registry->get_parent_registry >> |
107 | |
108 | Returns the registry's parent registry, if it has one. |
109 | |
110 | =item B<< $registry->has_parent_registry >> |
111 | |
112 | Returns true if the registry has a parent. |
22aed3c0 |
113 | |
c9861bfb |
114 | =item B<< $registry->set_parent_registry($registry) >> |
22aed3c0 |
115 | |
c9861bfb |
116 | Sets the parent registry. |
183ba44e |
117 | |
c9861bfb |
118 | =item B<< $registry->get_type_constraint($type_name) >> |
183ba44e |
119 | |
c9861bfb |
120 | This returns the L<Moose::Meta::TypeConstraint> object from the |
121 | registry for the given name, if one exists. |
22aed3c0 |
122 | |
c9861bfb |
123 | =item B<< $registry->has_type_constraint($type_name) >> |
183ba44e |
124 | |
c9861bfb |
125 | Returns true if the registry has a type of the given name. |
22aed3c0 |
126 | |
c9861bfb |
127 | =item B<< $registry->add_type_constraint($type) >> |
e606ae5f |
128 | |
c9861bfb |
129 | Adds a new L<Moose::Meta::TypeConstraint> object to the registry. |
22aed3c0 |
130 | |
c9861bfb |
131 | =item B<< $registry->find_type_constraint($type_name) >> |
e606ae5f |
132 | |
c9861bfb |
133 | This method looks in the current registry for the named type. If the |
134 | type is not found, then this method will look in the registry's |
135 | parent, if it has one. |
22aed3c0 |
136 | |
137 | =back |
138 | |
139 | =head1 BUGS |
140 | |
d4048ef3 |
141 | See L<Moose/BUGS> for details on reporting bugs. |
22aed3c0 |
142 | |
22aed3c0 |
143 | =cut |