Finish renaming Mouse::TypeRegistry to Mouse::Util::TypeConstraints
[gitmo/Mouse.git] / lib / Mouse / Util / TypeConstraints.pm
1 package Mouse::Util::TypeConstraints;
2 use strict;
3 use warnings;
4
5 use Carp ();
6 use Scalar::Util qw/blessed looks_like_number openhandle/;
7
8 my %SUBTYPE;
9 my %COERCE;
10 my %COERCE_KEYS;
11
12 #find_type_constraint register_type_constraint
13 sub import {
14     my $class  = shift;
15     my %args   = @_;
16     my $caller = $args{callee} || caller(0);
17
18     no strict 'refs';
19     *{"$caller\::as"}          = \&_as;
20     *{"$caller\::where"}       = \&_where;
21     *{"$caller\::message"}     = \&_message;
22     *{"$caller\::from"}        = \&_from;
23     *{"$caller\::via"}         = \&_via;
24     *{"$caller\::subtype"}     = \&_subtype;
25     *{"$caller\::coerce"}      = \&_coerce;
26     *{"$caller\::class_type"}  = \&_class_type;
27     *{"$caller\::role_type"}   = \&_role_type;
28 }
29
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
46 my $optimized_constraints;
47 my $optimized_constraints_base;
48 {
49     no warnings 'uninitialized';
50     %SUBTYPE = (
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
83     sub optimized_constraints { \%SUBTYPE }
84     my @SUBTYPE_KEYS = keys %SUBTYPE;
85     sub list_all_builtin_type_constraints { @SUBTYPE_KEYS }
86 }
87
88 sub _subtype {
89     my $pkg = caller(0);
90     my($name, %conf) = @_;
91     if (my $type = $SUBTYPE{$name}) {
92         Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
93     };
94     my $stuff = $conf{where} || do { $SUBTYPE{delete $conf{as} || 'Any' } };
95     my $as    = $conf{as} || '';
96     if ($as = $SUBTYPE{$as}) {
97         $SUBTYPE{$name} = sub { $as->($_) && $stuff->($_) };
98     } else {
99         $SUBTYPE{$name} = $stuff;
100     }
101 }
102
103 sub _coerce {
104     my($name, %conf) = @_;
105
106     Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
107         unless $SUBTYPE{$name};
108
109     unless ($COERCE{$name}) {
110         $COERCE{$name}      = {};
111         $COERCE_KEYS{$name} = [];
112     }
113     while (my($type, $code) = each %conf) {
114         Carp::croak "A coercion action already exists for '$type'"
115             if $COERCE{$name}->{$type};
116
117         Carp::croak "Could not find the type constraint ($type) to coerce from"
118             unless $SUBTYPE{$type};
119
120         push @{ $COERCE_KEYS{$name} }, $type;
121         $COERCE{$name}->{$type} = $code;
122     }
123 }
124
125 sub _class_type {
126     my $pkg = caller(0);
127     my($name, $conf) = @_;
128     my $class = $conf->{class};
129     Mouse::load_class($class);
130     _subtype(
131         $name => where => sub { $_->isa($class) }
132     );
133 }
134
135 sub _role_type {
136     my($name, $conf) = @_;
137     my $role = $conf->{role};
138     _subtype(
139         $name => where => sub {
140             return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
141             $_->meta->does_role($role);
142         }
143     );
144 }
145
146 sub typecast_constraints {
147     my($class, $pkg, $type_constraint, $types, $value) = @_;
148
149     local $_;
150     for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
151         next unless $COERCE{$type};
152         for my $coerce_type (@{ $COERCE_KEYS{$type}}) {
153             $_ = $value;
154             next unless $SUBTYPE{$coerce_type}->();
155             $_ = $value;
156             $_ = $COERCE{$type}->{$coerce_type}->();
157             return $_ if $type_constraint->();
158         }
159     }
160     return $value;
161 }
162
163 1;
164
165 __END__
166
167 =head1 NAME
168
169 Mouse::Util::TypeConstraints - simple type constraints
170
171 =head1 METHODS
172
173 =head2 optimized_constraints -> HashRef[CODE]
174
175 Returns the simple type constraints that Mouse understands.
176
177 =cut
178
179