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