support MouseX::Types's isa or ( isa => Str | Undef )
[gitmo/Mouse.git] / lib / Mouse / TypeRegistry.pm
CommitLineData
d60c78b9 1#!/usr/bin/env perl
2package Mouse::TypeRegistry;
3use strict;
4use warnings;
9baf5d6b 5
61a02a3a 6use Carp ();
29312fc3 7use Mouse::Util qw/blessed looks_like_number openhandle/;
d60c78b9 8
4188b837 9my $SUBTYPE = +{};
10my $COERCE = +{};
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;
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
31sub _as ($) {
32 as => $_[0]
33}
34sub _where (&) {
35 where => $_[0]
36}
37sub _message ($) {
38 message => $_[0]
39}
40
41sub _from { @_ }
42sub _via (&) {
43 $_[0]
44}
45
381f326a 46my $optimized_constraints;
47my $optimized_constraints_base;
48{
49 no warnings 'uninitialized';
d3982c7e 50 $SUBTYPE = {
381f326a 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 };
d3982c7e 82
83 sub optimized_constraints { $SUBTYPE }
84 my @SUBTYPE_KEYS = keys %{ $SUBTYPE };
85 sub list_all_builtin_type_constraints { @SUBTYPE_KEYS }
381f326a 86}
d3982c7e 87
4188b837 88sub _subtype {
89 my $pkg = caller(0);
61a02a3a 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 $as = $conf{as};
95 my $stuff = $conf{where} || optimized_constraints()->{$as};
96
97 $SUBTYPE->{$name} = $stuff;
4188b837 98}
99
100sub _coerce {
61a02a3a 101 my($name, %conf) = @_;
102
103 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
104 unless optimized_constraints()->{$name};
105
106 my $subtypes = optimized_constraints();
107 $COERCE->{$name} ||= {};
108 while (my($type, $code) = each %conf) {
109 Carp::croak "A coercion action already exists for '$type'"
110 if $COERCE->{$name}->{$type};
111
112 Carp::croak "Could not find the type constraint ($type) to coerce from"
113 unless $subtypes->{$type};
114
115 $COERCE->{$name}->{$type} = $code;
116 }
4188b837 117}
118
ecc6e3b1 119sub _class_type {
120 my $pkg = caller(0);
ecc6e3b1 121 my($name, $conf) = @_;
122 my $class = $conf->{class};
61a02a3a 123 _subtype(
124 $name => where => sub {
125 defined $_ && ref($_) eq $class;
126 }
127 );
ecc6e3b1 128}
129
47f36c05 130sub _role_type {
47f36c05 131 my($name, $conf) = @_;
132 my $role = $conf->{role};
61a02a3a 133 _subtype(
134 $name => where => sub {
135 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
136 $_->meta->does_role($role);
137 }
138 );
47f36c05 139}
140
4188b837 141sub typecast_constraints {
eec1bb49 142 my($class, $pkg, $type_constraint, $types, $value) = @_;
2b9094e8 143 my $optimized_constraints = optimized_constraints();
eec1bb49 144
145 for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
146 next unless $COERCE->{$type};
147
148 for my $coerce_type (keys %{ $COERCE->{$type} }) {
4188b837 149 local $_ = $value;
eec1bb49 150 if ($optimized_constraints->{$coerce_type}->()) {
151 local $_ = $value;
152 local $_ = $COERCE->{$type}->{$coerce_type}->();
153 return $_ if $type_constraint->();
154 }
4188b837 155 }
156 }
4188b837 157 return $value;
158}
159
d60c78b9 1601;
161
6feb83f1 162__END__
163
164=head1 NAME
165
166Mouse::TypeRegistry - simple type constraints
167
168=head1 METHODS
169
170=head2 optimized_constraints -> HashRef[CODE]
171
172Returns the simple type constraints that Mouse understands.
173
174=cut
175
176