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 | |
4188b837 |
46 | sub _subtype { |
47 | my $pkg = caller(0); |
61a02a3a |
48 | my($name, %conf) = @_; |
49 | if (my $type = $SUBTYPE->{$name}) { |
50 | Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg"; |
51 | }; |
52 | my $as = $conf{as}; |
53 | my $stuff = $conf{where} || optimized_constraints()->{$as}; |
54 | |
55 | $SUBTYPE->{$name} = $stuff; |
4188b837 |
56 | } |
57 | |
58 | sub _coerce { |
61a02a3a |
59 | my($name, %conf) = @_; |
60 | |
61 | Carp::croak "Cannot find type '$name', perhaps you forgot to load it." |
62 | unless optimized_constraints()->{$name}; |
63 | |
64 | my $subtypes = optimized_constraints(); |
65 | $COERCE->{$name} ||= {}; |
66 | while (my($type, $code) = each %conf) { |
67 | Carp::croak "A coercion action already exists for '$type'" |
68 | if $COERCE->{$name}->{$type}; |
69 | |
70 | Carp::croak "Could not find the type constraint ($type) to coerce from" |
71 | unless $subtypes->{$type}; |
72 | |
73 | $COERCE->{$name}->{$type} = $code; |
74 | } |
4188b837 |
75 | } |
76 | |
ecc6e3b1 |
77 | sub _class_type { |
78 | my $pkg = caller(0); |
ecc6e3b1 |
79 | my($name, $conf) = @_; |
80 | my $class = $conf->{class}; |
61a02a3a |
81 | _subtype( |
82 | $name => where => sub { |
83 | defined $_ && ref($_) eq $class; |
84 | } |
85 | ); |
ecc6e3b1 |
86 | } |
87 | |
47f36c05 |
88 | sub _role_type { |
47f36c05 |
89 | my($name, $conf) = @_; |
90 | my $role = $conf->{role}; |
61a02a3a |
91 | _subtype( |
92 | $name => where => sub { |
93 | return unless defined $_ && ref($_) && $_->isa('Mouse::Object'); |
94 | $_->meta->does_role($role); |
95 | } |
96 | ); |
47f36c05 |
97 | } |
98 | |
4188b837 |
99 | sub typecast_constraints { |
100 | my($class, $pkg, $type, $value) = @_; |
61a02a3a |
101 | return $value unless $COERCE->{$type}; |
4188b837 |
102 | |
103 | my $optimized_constraints = optimized_constraints(); |
61a02a3a |
104 | for my $coerce_type (keys %{ $COERCE->{$type} }) { |
4188b837 |
105 | local $_ = $value; |
106 | if ($optimized_constraints->{$coerce_type}->()) { |
107 | local $_ = $value; |
61a02a3a |
108 | return $COERCE->{$type}->{$coerce_type}->(); |
4188b837 |
109 | } |
110 | } |
111 | |
4188b837 |
112 | return $value; |
113 | } |
114 | |
115 | { |
116 | no warnings 'uninitialized'; |
117 | my $optimized_constraints = { |
d60c78b9 |
118 | Any => sub { 1 }, |
119 | Item => sub { 1 }, |
120 | Bool => sub { |
121 | !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' |
122 | }, |
f5fbe3cc |
123 | Undef => sub { !defined($_) }, |
124 | Defined => sub { defined($_) }, |
0f636a97 |
125 | Value => sub { defined($_) && !ref($_) }, |
126 | Num => sub { !ref($_) && looks_like_number($_) }, |
127 | Int => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ }, |
128 | Str => sub { defined($_) && !ref($_) }, |
79af4b55 |
129 | ClassName => sub { Mouse::is_class_loaded($_) }, |
0f636a97 |
130 | Ref => sub { ref($_) }, |
131 | |
132 | ScalarRef => sub { ref($_) eq 'SCALAR' }, |
133 | ArrayRef => sub { ref($_) eq 'ARRAY' }, |
134 | HashRef => sub { ref($_) eq 'HASH' }, |
135 | CodeRef => sub { ref($_) eq 'CODE' }, |
136 | RegexpRef => sub { ref($_) eq 'Regexp' }, |
137 | GlobRef => sub { ref($_) eq 'GLOB' }, |
138 | |
139 | FileHandle => sub { |
140 | ref($_) eq 'GLOB' |
141 | && openhandle($_) |
142 | or |
143 | blessed($_) |
144 | && $_->isa("IO::Handle") |
145 | }, |
146 | |
147 | Object => sub { blessed($_) && blessed($_) ne 'Regexp' }, |
d60c78b9 |
148 | }; |
5439cf97 |
149 | sub list_all_builtin_type_constraints { keys %{ $optimized_constraints } } |
4188b837 |
150 | sub optimized_constraints { |
61a02a3a |
151 | return { %{ $SUBTYPE }, %{ $optimized_constraints } }; |
4188b837 |
152 | } |
d60c78b9 |
153 | } |
154 | |
155 | 1; |
156 | |
6feb83f1 |
157 | __END__ |
158 | |
159 | =head1 NAME |
160 | |
161 | Mouse::TypeRegistry - simple type constraints |
162 | |
163 | =head1 METHODS |
164 | |
165 | =head2 optimized_constraints -> HashRef[CODE] |
166 | |
167 | Returns the simple type constraints that Mouse understands. |
168 | |
169 | =cut |
170 | |
171 | |