set instance to attributes default code
[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
8a7f2a8a 9my %SUBTYPE;
10my %COERCE;
11my %COERCE_KEYS;
4188b837 12
bc37d507 13#find_type_constraint register_type_constraint
4188b837 14sub import {
15 my $class = shift;
16 my %args = @_;
bc37d507 17 my $caller = $args{callee} || caller(0);
4188b837 18
19 no strict 'refs';
61a02a3a 20 *{"$caller\::as"} = \&_as;
21 *{"$caller\::where"} = \&_where;
22 *{"$caller\::message"} = \&_message;
23 *{"$caller\::from"} = \&_from;
24 *{"$caller\::via"} = \&_via;
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';
8a7f2a8a 51 %SUBTYPE = (
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 {
74 ref($_) eq 'GLOB'
75 && openhandle($_)
76 or
77 blessed($_)
78 && $_->isa("IO::Handle")
79 },
80
81 Object => sub { blessed($_) && blessed($_) ne 'Regexp' },
8a7f2a8a 82 );
d3982c7e 83
8a7f2a8a 84 sub optimized_constraints { \%SUBTYPE }
85 my @SUBTYPE_KEYS = keys %SUBTYPE;
d3982c7e 86 sub list_all_builtin_type_constraints { @SUBTYPE_KEYS }
381f326a 87}
d3982c7e 88
4188b837 89sub _subtype {
90 my $pkg = caller(0);
61a02a3a 91 my($name, %conf) = @_;
8a7f2a8a 92 if (my $type = $SUBTYPE{$name}) {
61a02a3a 93 Carp::croak "The type constraint '$name' has already been created, cannot be created again in $pkg";
94 };
95 my $as = $conf{as};
96 my $stuff = $conf{where} || optimized_constraints()->{$as};
97
8a7f2a8a 98 $SUBTYPE{$name} = $stuff;
4188b837 99}
100
101sub _coerce {
61a02a3a 102 my($name, %conf) = @_;
103
104 Carp::croak "Cannot find type '$name', perhaps you forgot to load it."
105 unless optimized_constraints()->{$name};
106
107 my $subtypes = optimized_constraints();
8a7f2a8a 108 unless ($COERCE{$name}) {
109 $COERCE{$name} = {};
110 $COERCE_KEYS{$name} = [];
111 }
61a02a3a 112 while (my($type, $code) = each %conf) {
113 Carp::croak "A coercion action already exists for '$type'"
8a7f2a8a 114 if $COERCE{$name}->{$type};
61a02a3a 115
116 Carp::croak "Could not find the type constraint ($type) to coerce from"
117 unless $subtypes->{$type};
118
8a7f2a8a 119 push @{ $COERCE_KEYS{$name} }, $type;
120 $COERCE{$name}->{$type} = $code;
61a02a3a 121 }
4188b837 122}
123
ecc6e3b1 124sub _class_type {
125 my $pkg = caller(0);
ecc6e3b1 126 my($name, $conf) = @_;
127 my $class = $conf->{class};
61a02a3a 128 _subtype(
129 $name => where => sub {
130 defined $_ && ref($_) eq $class;
131 }
132 );
ecc6e3b1 133}
134
47f36c05 135sub _role_type {
47f36c05 136 my($name, $conf) = @_;
137 my $role = $conf->{role};
61a02a3a 138 _subtype(
139 $name => where => sub {
140 return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
141 $_->meta->does_role($role);
142 }
143 );
47f36c05 144}
145
4188b837 146sub typecast_constraints {
eec1bb49 147 my($class, $pkg, $type_constraint, $types, $value) = @_;
eec1bb49 148
149 for my $type (ref($types) eq 'ARRAY' ? @{ $types } : ( $types )) {
8a7f2a8a 150 next unless $COERCE{$type};
eec1bb49 151
8a7f2a8a 152 for my $coerce_type (@{ $COERCE_KEYS{$type}}) {
4188b837 153 local $_ = $value;
8a7f2a8a 154 if ($SUBTYPE{$coerce_type}->()) {
eec1bb49 155 local $_ = $value;
8a7f2a8a 156 local $_ = $COERCE{$type}->{$coerce_type}->();
eec1bb49 157 return $_ if $type_constraint->();
158 }
4188b837 159 }
160 }
4188b837 161 return $value;
162}
163
d60c78b9 1641;
165
6feb83f1 166__END__
167
168=head1 NAME
169
170Mouse::TypeRegistry - simple type constraints
171
172=head1 METHODS
173
174=head2 optimized_constraints -> HashRef[CODE]
175
176Returns the simple type constraints that Mouse understands.
177
178=cut
179
180