added MouseX::Types, MouseX::Types::Mouse
[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 Mouse::Util qw/blessed looks_like_number openhandle/;
8
9 my $SUBTYPE = +{};
10 my $COERCE = +{};
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 sub _subtype {
47     my $pkg = caller(0);
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;
56 }
57
58 sub _coerce {
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     }
75 }
76
77 sub _class_type {
78     my $pkg = caller(0);
79     my($name, $conf) = @_;
80     my $class = $conf->{class};
81     _subtype(
82         $name => where => sub {
83             defined $_ && ref($_) eq $class;
84         }
85     );
86 }
87
88 sub _role_type {
89     my($name, $conf) = @_;
90     my $role = $conf->{role};
91     _subtype(
92         $name => where => sub {
93             return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
94             $_->meta->does_role($role);
95         }
96     );
97 }
98
99 sub typecast_constraints {
100     my($class, $pkg, $type, $value) = @_;
101     return $value unless $COERCE->{$type};
102
103     my $optimized_constraints = optimized_constraints();
104     for my $coerce_type (keys %{ $COERCE->{$type} }) {
105         local $_ = $value;
106         if ($optimized_constraints->{$coerce_type}->()) {
107             local $_ = $value;
108             return $COERCE->{$type}->{$coerce_type}->();
109         }
110     }
111
112     return $value;
113 }
114
115 {
116     no warnings 'uninitialized';
117     my $optimized_constraints = {
118         Any        => sub { 1 },
119         Item       => sub { 1 },
120         Bool       => sub {
121             !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0'
122         },
123         Undef      => sub { !defined($_) },
124         Defined    => sub { defined($_) },
125         Value      => sub { defined($_) && !ref($_) },
126         Num        => sub { !ref($_) && looks_like_number($_) },
127         Int        => sub { defined($_) && !ref($_) && /^-?[0-9]+$/ },
128         Str        => sub { defined($_) && !ref($_) },
129         ClassName  => sub { Mouse::is_class_loaded($_) },
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' },
148     };
149     sub list_all_builtin_type_constraints { keys %{ $optimized_constraints } }
150     sub optimized_constraints {
151         return { %{ $SUBTYPE }, %{ $optimized_constraints } };
152     }
153 }
154
155 1;
156
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