Commit | Line | Data |
d60c78b9 |
1 | package Mouse::TypeRegistry; |
2 | use strict; |
3 | use warnings; |
9baf5d6b |
4 | |
61a02a3a |
5 | use Carp (); |
6c169c50 |
6 | use Scalar::Util qw/blessed looks_like_number openhandle/; |
d60c78b9 |
7 | |
8a7f2a8a |
8 | my %SUBTYPE; |
9 | my %COERCE; |
10 | my %COERCE_KEYS; |
4188b837 |
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'; |
8a7f2a8a |
50 | %SUBTYPE = ( |
381f326a |
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' }, |
8a7f2a8a |
81 | ); |
d3982c7e |
82 | |
8a7f2a8a |
83 | sub optimized_constraints { \%SUBTYPE } |
84 | my @SUBTYPE_KEYS = keys %SUBTYPE; |
d3982c7e |
85 | sub list_all_builtin_type_constraints { @SUBTYPE_KEYS } |
381f326a |
86 | } |
d3982c7e |
87 | |
4188b837 |
88 | sub _subtype { |
89 | my $pkg = caller(0); |
61a02a3a |
90 | my($name, %conf) = @_; |
8a7f2a8a |
91 | if (my $type = $SUBTYPE{$name}) { |
61a02a3a |
92 | Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg"; |
93 | }; |
2f665925 |
94 | my $stuff = $conf{where} || do { $SUBTYPE{delete $conf{as} || 'Any' } }; |
95 | my $as = $conf{as} || ''; |
1fbefea5 |
96 | if ($as = $SUBTYPE{$as}) { |
97 | $SUBTYPE{$name} = sub { $as->($_) && $stuff->($_) }; |
98 | } else { |
99 | $SUBTYPE{$name} = $stuff; |
100 | } |
4188b837 |
101 | } |
102 | |
103 | sub _coerce { |
61a02a3a |
104 | my($name, %conf) = @_; |
105 | |
106 | Carp::croak "Cannot find type '$name', perhaps you forgot to load it." |
b3b74cc6 |
107 | unless $SUBTYPE{$name}; |
61a02a3a |
108 | |
8a7f2a8a |
109 | unless ($COERCE{$name}) { |
110 | $COERCE{$name} = {}; |
111 | $COERCE_KEYS{$name} = []; |
112 | } |
61a02a3a |
113 | while (my($type, $code) = each %conf) { |
114 | Carp::croak "A coercion action already exists for '$type'" |
8a7f2a8a |
115 | if $COERCE{$name}->{$type}; |
61a02a3a |
116 | |
117 | Carp::croak "Could not find the type constraint ($type) to coerce from" |
b3b74cc6 |
118 | unless $SUBTYPE{$type}; |
61a02a3a |
119 | |
8a7f2a8a |
120 | push @{ $COERCE_KEYS{$name} }, $type; |
121 | $COERCE{$name}->{$type} = $code; |
61a02a3a |
122 | } |
4188b837 |
123 | } |
124 | |
ecc6e3b1 |
125 | sub _class_type { |
126 | my $pkg = caller(0); |
ecc6e3b1 |
127 | my($name, $conf) = @_; |
128 | my $class = $conf->{class}; |
2f6fade8 |
129 | Mouse::load_class($class); |
61a02a3a |
130 | _subtype( |
131 | $name => where => sub { |
132 | defined $_ && ref($_) eq $class; |
133 | } |
134 | ); |
ecc6e3b1 |
135 | } |
136 | |
47f36c05 |
137 | sub _role_type { |
47f36c05 |
138 | my($name, $conf) = @_; |
139 | my $role = $conf->{role}; |
61a02a3a |
140 | _subtype( |
141 | $name => where => sub { |
142 | return unless defined $_ && ref($_) && $_->isa('Mouse::Object'); |
143 | $_->meta->does_role($role); |
144 | } |
145 | ); |
47f36c05 |
146 | } |
147 | |
4188b837 |
148 | sub typecast_constraints { |
eec1bb49 |
149 | my($class, $pkg, $type_constraint, $types, $value) = @_; |
eec1bb49 |
150 | |
b3b74cc6 |
151 | local $_; |
eec1bb49 |
152 | for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) { |
8a7f2a8a |
153 | next unless $COERCE{$type}; |
8a7f2a8a |
154 | for my $coerce_type (@{ $COERCE_KEYS{$type}}) { |
b3b74cc6 |
155 | $_ = $value; |
156 | next unless $SUBTYPE{$coerce_type}->(); |
157 | $_ = $value; |
158 | $_ = $COERCE{$type}->{$coerce_type}->(); |
159 | return $_ if $type_constraint->(); |
4188b837 |
160 | } |
161 | } |
4188b837 |
162 | return $value; |
163 | } |
164 | |
d60c78b9 |
165 | 1; |
166 | |
6feb83f1 |
167 | __END__ |
168 | |
169 | =head1 NAME |
170 | |
171 | Mouse::TypeRegistry - simple type constraints |
172 | |
173 | =head1 METHODS |
174 | |
175 | =head2 optimized_constraints -> HashRef[CODE] |
176 | |
177 | Returns the simple type constraints that Mouse understands. |
178 | |
179 | =cut |
180 | |
181 | |