Commit | Line | Data |
d60c78b9 |
1 | #!/usr/bin/env perl |
2 | package Mouse::TypeRegistry; |
3 | use strict; |
4 | use warnings; |
9baf5d6b |
5 | |
61a02a3a |
6 | use Carp (); |
29312fc3 |
7 | use Mouse::Util qw/blessed looks_like_number openhandle/; |
d60c78b9 |
8 | |
4188b837 |
9 | my $SUBTYPE = +{}; |
10 | my $COERCE = +{}; |
11 | |
bc37d507 |
12 | #find_type_constraint register_type_constraint |
4188b837 |
13 | sub import { |
14 | my $class = shift; |
15 | my %args = @_; |
bc37d507 |
16 | my $caller = $args{callee} || caller(0); |
4188b837 |
17 | |
18 | no strict 'refs'; |
61a02a3a |
19 | *{"$caller\::as"} = \&_as; |
20 | *{"$caller\::where"} = \&_where; |
21 | *{"$caller\::message"} = \&_message; |
22 | *{"$caller\::from"} = \&_from; |
23 | *{"$caller\::via"} = \&_via; |
4188b837 |
24 | *{"$caller\::subtype"} = \&_subtype; |
25 | *{"$caller\::coerce"} = \&_coerce; |
ecc6e3b1 |
26 | *{"$caller\::class_type"} = \&_class_type; |
47f36c05 |
27 | *{"$caller\::role_type"} = \&_role_type; |
4188b837 |
28 | } |
29 | |
61a02a3a |
30 | |
31 | sub _as ($) { |
32 | as => $_[0] |
33 | } |
34 | sub _where (&) { |
35 | where => $_[0] |
36 | } |
37 | sub _message ($) { |
38 | message => $_[0] |
39 | } |
40 | |
41 | sub _from { @_ } |
42 | sub _via (&) { |
43 | $_[0] |
44 | } |
45 | |
381f326a |
46 | my $optimized_constraints; |
47 | my $optimized_constraints_base; |
48 | { |
49 | no warnings 'uninitialized'; |
50 | $optimized_constraints = $optimized_constraints_base = { |
51 | Any => sub { 1 }, |
52 | Item => sub { 1 }, |
53 | Bool => sub { |
54 | !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' |
55 | }, |
56 | Undef => sub { !defined($_) }, |
57 | Defined => sub { defined($_) }, |
58 | Value => sub { defined($_) && !ref($_) }, |
59 | Num => sub { !ref($_) && looks_like_number($_) }, |
60 | Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ }, |
61 | Str => sub { defined($_) && !ref($_) }, |
62 | ClassName => sub { Mouse::is_class_loaded($_) }, |
63 | Ref => sub { ref($_) }, |
64 | |
65 | ScalarRef => sub { ref($_) eq 'SCALAR' }, |
66 | ArrayRef => sub { ref($_) eq 'ARRAY' }, |
67 | HashRef => sub { ref($_) eq 'HASH' }, |
68 | CodeRef => sub { ref($_) eq 'CODE' }, |
69 | RegexpRef => sub { ref($_) eq 'Regexp' }, |
70 | GlobRef => sub { ref($_) eq 'GLOB' }, |
71 | |
72 | FileHandle => sub { |
73 | ref($_) eq 'GLOB' |
74 | && openhandle($_) |
75 | or |
76 | blessed($_) |
77 | && $_->isa("IO::Handle") |
78 | }, |
79 | |
80 | Object => sub { blessed($_) && blessed($_) ne 'Regexp' }, |
81 | }; |
82 | } |
4188b837 |
83 | sub _subtype { |
84 | my $pkg = caller(0); |
61a02a3a |
85 | my($name, %conf) = @_; |
86 | if (my $type = $SUBTYPE->{$name}) { |
87 | Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg"; |
88 | }; |
89 | my $as = $conf{as}; |
90 | my $stuff = $conf{where} || optimized_constraints()->{$as}; |
91 | |
92 | $SUBTYPE->{$name} = $stuff; |
381f326a |
93 | $optimized_constraints = +{ %{ $SUBTYPE }, %{ $optimized_constraints_base } }; |
4188b837 |
94 | } |
95 | |
96 | sub _coerce { |
61a02a3a |
97 | my($name, %conf) = @_; |
98 | |
99 | Carp::croak "Cannot find type '$name', perhaps you forgot to load it." |
100 | unless optimized_constraints()->{$name}; |
101 | |
102 | my $subtypes = optimized_constraints(); |
103 | $COERCE->{$name} ||= {}; |
104 | while (my($type, $code) = each %conf) { |
105 | Carp::croak "A coercion action already exists for '$type'" |
106 | if $COERCE->{$name}->{$type}; |
107 | |
108 | Carp::croak "Could not find the type constraint ($type) to coerce from" |
109 | unless $subtypes->{$type}; |
110 | |
111 | $COERCE->{$name}->{$type} = $code; |
112 | } |
4188b837 |
113 | } |
114 | |
ecc6e3b1 |
115 | sub _class_type { |
116 | my $pkg = caller(0); |
ecc6e3b1 |
117 | my($name, $conf) = @_; |
118 | my $class = $conf->{class}; |
61a02a3a |
119 | _subtype( |
120 | $name => where => sub { |
121 | defined $_ && ref($_) eq $class; |
122 | } |
123 | ); |
ecc6e3b1 |
124 | } |
125 | |
47f36c05 |
126 | sub _role_type { |
47f36c05 |
127 | my($name, $conf) = @_; |
128 | my $role = $conf->{role}; |
61a02a3a |
129 | _subtype( |
130 | $name => where => sub { |
131 | return unless defined $_ && ref($_) && $_->isa('Mouse::Object'); |
132 | $_->meta->does_role($role); |
133 | } |
134 | ); |
47f36c05 |
135 | } |
136 | |
4188b837 |
137 | sub typecast_constraints { |
138 | my($class, $pkg, $type, $value) = @_; |
61a02a3a |
139 | return $value unless $COERCE->{$type}; |
4188b837 |
140 | |
141 | my $optimized_constraints = optimized_constraints(); |
61a02a3a |
142 | for my $coerce_type (keys %{ $COERCE->{$type} }) { |
4188b837 |
143 | local $_ = $value; |
144 | if ($optimized_constraints->{$coerce_type}->()) { |
145 | local $_ = $value; |
61a02a3a |
146 | return $COERCE->{$type}->{$coerce_type}->(); |
4188b837 |
147 | } |
148 | } |
149 | |
4188b837 |
150 | return $value; |
151 | } |
152 | |
381f326a |
153 | sub optimized_constraints { $optimized_constraints } |
4188b837 |
154 | { |
381f326a |
155 | my @optimized_constraints_keys = keys %{ $optimized_constraints }; |
156 | sub list_all_builtin_type_constraints { @optimized_constraints_keys } |
d60c78b9 |
157 | } |
158 | |
159 | 1; |
160 | |
6feb83f1 |
161 | __END__ |
162 | |
163 | =head1 NAME |
164 | |
165 | Mouse::TypeRegistry - simple type constraints |
166 | |
167 | =head1 METHODS |
168 | |
169 | =head2 optimized_constraints -> HashRef[CODE] |
170 | |
171 | Returns the simple type constraints that Mouse understands. |
172 | |
173 | =cut |
174 | |
175 | |