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