foo
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints.pm
CommitLineData
a15dff8d 1
2package Moose::Util::TypeConstraints;
3
4use strict;
5use warnings;
6
e90c03d0 7use Carp 'confess';
a15dff8d 8use Scalar::Util 'blessed';
9
2c0cbef7 10our $VERSION = '0.07';
a15dff8d 11
4e036ee4 12use Moose::Meta::TypeConstraint;
2ca63f5d 13use Moose::Meta::TypeCoercion;
4e036ee4 14
9e93dd19 15use Sub::Exporter -setup => {
16 exports => [qw/
17 type subtype as where message
18 coerce from via
19 enum
20 find_type_constraint
21 /],
22 groups => {
23 default => [':all']
2c0cbef7 24 }
9e93dd19 25};
a15dff8d 26
182134e8 27{
28 my %TYPES;
2c0cbef7 29 sub find_type_constraint ($) {
446e850f 30 return $TYPES{$_[0]}->[1]
31 if exists $TYPES{$_[0]};
32 return;
33 }
34
35 sub _dump_type_constraints {
36 require Data::Dumper;
256903b6 37 Data::Dumper::Dumper(\%TYPES);
446e850f 38 }
39
2c0cbef7 40 sub _create_type_constraint ($$$;$) {
76d37e5a 41 my ($name, $parent, $check, $message) = @_;
0e6614c3 42 my $pkg_defined_in = scalar(caller(1));
43 ($TYPES{$name}->[0] eq $pkg_defined_in)
446e850f 44 || confess "The type constraint '$name' has already been created "
0e6614c3 45 if defined $name && exists $TYPES{$name};
46 $parent = find_type_constraint($parent) if defined $parent;
a27aa600 47 my $constraint = Moose::Meta::TypeConstraint->new(
48 name => $name || '__ANON__',
66811d63 49 parent => $parent,
76d37e5a 50 constraint => $check,
51 message => $message,
4e036ee4 52 );
0e6614c3 53 $TYPES{$name} = [ $pkg_defined_in, $constraint ] if defined $name;
a27aa600 54 return $constraint;
182134e8 55 }
182134e8 56
2c0cbef7 57 sub _install_type_coercions ($$) {
a27aa600 58 my ($type_name, $coercion_map) = @_;
0e6614c3 59 my $type = find_type_constraint($type_name);
4e036ee4 60 (!$type->has_coercion)
d46a48f3 61 || confess "The type coercion for '$type_name' has already been registered";
a27aa600 62 my $type_coercion = Moose::Meta::TypeCoercion->new(
63 type_coercion_map => $coercion_map,
64 type_constraint => $type
65 );
66 $type->coercion($type_coercion);
182134e8 67 }
66811d63 68
2c0cbef7 69 sub create_type_constraint_union (@) {
c07af9d2 70 my (@type_constraint_names) = @_;
71 return Moose::Meta::TypeConstraint->union(
72 map {
73 find_type_constraint($_)
74 } @type_constraint_names
75 );
76 }
77
66811d63 78 sub export_type_contstraints_as_functions {
79 my $pkg = caller();
80 no strict 'refs';
81 foreach my $constraint (keys %TYPES) {
0e6614c3 82 *{"${pkg}::${constraint}"} = find_type_constraint($constraint)->_compiled_type_constraint;
66811d63 83 }
84 }
182134e8 85}
a15dff8d 86
7c13858b 87# type constructors
a15dff8d 88
89sub type ($$) {
90 my ($name, $check) = @_;
7c13858b 91 _create_type_constraint($name, undef, $check);
a15dff8d 92}
93
76d37e5a 94sub subtype ($$;$$) {
95 unshift @_ => undef if scalar @_ <= 2;
2c0cbef7 96 goto &_create_type_constraint;
a15dff8d 97}
98
4b598ea3 99sub coerce ($@) {
66811d63 100 my ($type_name, @coercion_map) = @_;
7c13858b 101 _install_type_coercions($type_name, \@coercion_map);
182134e8 102}
103
76d37e5a 104sub as ($) { $_[0] }
105sub from ($) { $_[0] }
106sub where (&) { $_[0] }
107sub via (&) { $_[0] }
108sub message (&) { $_[0] }
a15dff8d 109
2c0cbef7 110sub enum ($;@) {
fcec2383 111 my ($type_name, @values) = @_;
2c0cbef7 112 (scalar @values >= 2)
113 || confess "You must have at least two values to enumerate through";
fcec2383 114 my $regexp = join '|' => @values;
115 _create_type_constraint(
116 $type_name,
117 'Str',
118 sub { qr/^$regexp$/i }
119 );
120}
121
a15dff8d 122# define some basic types
123
f65cb534 124type 'Any' => where { 1 }; # meta-type including all
125type 'Item' => where { 1 }; # base-type
a15dff8d 126
f65cb534 127subtype 'Undef' => as 'Item' => where { !defined($_) };
128subtype 'Defined' => as 'Item' => where { defined($_) };
a15dff8d 129
81dc201f 130subtype 'Bool' => as 'Item' => where { !defined($_) || $_ eq "" || "$_" eq '1' || "$_" eq '0' };
5204cd52 131
5a4c5493 132subtype 'Value' => as 'Defined' => where { !ref($_) };
133subtype 'Ref' => as 'Defined' => where { ref($_) };
134
135subtype 'Str' => as 'Value' => where { 1 };
a15dff8d 136
81dc201f 137subtype 'Num' => as 'Value' => where { Scalar::Util::looks_like_number($_) };
d4634ca2 138subtype 'Int' => as 'Num' => where { "$_" =~ /^-?[0-9]+$/ };
81dc201f 139
140subtype 'ScalarRef' => as 'Ref' => where { ref($_) eq 'SCALAR' };
451c8248 141subtype 'ArrayRef' => as 'Ref' => where { ref($_) eq 'ARRAY' };
142subtype 'HashRef' => as 'Ref' => where { ref($_) eq 'HASH' };
e9ec68d6 143subtype 'CodeRef' => as 'Ref' => where { ref($_) eq 'CODE' };
144subtype 'RegexpRef' => as 'Ref' => where { ref($_) eq 'Regexp' };
a15dff8d 145
146# NOTE:
147# blessed(qr/.../) returns true,.. how odd
e9ec68d6 148subtype 'Object' => as 'Ref' => where { blessed($_) && blessed($_) ne 'Regexp' };
a15dff8d 149
02a0fb52 150subtype 'Role' => as 'Object' => where { $_->can('does') };
151
a15dff8d 1521;
153
154__END__
155
156=pod
157
158=head1 NAME
159
e522431d 160Moose::Util::TypeConstraints - Type constraint system for Moose
a15dff8d 161
162=head1 SYNOPSIS
163
164 use Moose::Util::TypeConstraints;
165
2c0cbef7 166 type 'Num' => where { Scalar::Util::looks_like_number($_) };
a15dff8d 167
2c0cbef7 168 subtype 'Natural'
169 => as 'Num'
a15dff8d 170 => where { $_ > 0 };
171
2c0cbef7 172 subtype 'NaturalLessThanTen'
173 => as 'Natural'
79592a54 174 => where { $_ < 10 }
175 => message { "This number ($_) is not less than ten!" };
6b8bd8d3 176
2c0cbef7 177 coerce 'Num'
178 => from 'Str'
d6e2d9a1 179 => via { 0+$_ };
98aae381 180
2c0cbef7 181 enum 'RGBColors' => qw(red green blue);
a15dff8d 182
183=head1 DESCRIPTION
184
e522431d 185This module provides Moose with the ability to create type contraints
186to be are used in both attribute definitions and for method argument
187validation.
188
6ba6d68c 189=head2 Important Caveat
190
191This is B<NOT> a type system for Perl 5. These are type constraints,
192and they are not used by Moose unless you tell it to. No type
193inference is performed, expression are not typed, etc. etc. etc.
194
195This is simply a means of creating small constraint functions which
a7d0cd00 196can be used to simplify your own type-checking code.
6ba6d68c 197
2c0cbef7 198=head2 Slightly Less Important Caveat
199
200It is almost always a good idea to quote your type and subtype names.
43d599e5 201This is to prevent perl from trying to execute the call as an indirect
2c0cbef7 202object call. This issue only seems to come up when you have a subtype
203the same name as a valid class, but when the issue does arise it tends
204to be quite annoying to debug.
205
206So for instance, this:
207
208 subtype DateTime => as Object => where { $_->isa('DateTime') };
209
210will I<Just Work>, while this:
211
212 use DateTime;
213 subtype DateTime => as Object => where { $_->isa('DateTime') };
214
215will fail silently and cause many headaches. The simple way to solve
216this, as well as future proof your subtypes from classes which have
217yet to have been created yet, is to simply do this:
218
219 use DateTime;
220 subtype 'DateTime' => as Object => where { $_->isa('DateTime') };
221
6ba6d68c 222=head2 Default Type Constraints
e522431d 223
e522431d 224This module also provides a simple hierarchy for Perl 5 types, this
225could probably use some work, but it works for me at the moment.
226
227 Any
f65cb534 228 Item
5a4c5493 229 Bool
f65cb534 230 Undef
231 Defined
5a4c5493 232 Value
233 Num
234 Int
235 Str
236 Ref
237 ScalarRef
451c8248 238 ArrayRef
239 HashRef
5a4c5493 240 CodeRef
241 RegexpRef
242 Object
243 Role
e522431d 244
6ba6d68c 245Suggestions for improvement are welcome.
2c0cbef7 246
247B<NOTE:> The C<Undef> type constraint does not work correctly
248in every occasion, please use it sparringly.
e522431d 249
a15dff8d 250=head1 FUNCTIONS
251
182134e8 252=head2 Type Constraint Registry
253
254=over 4
255
256=item B<find_type_constraint ($type_name)>
257
6ba6d68c 258This function can be used to locate a specific type constraint
259meta-object. What you do with it from there is up to you :)
182134e8 260
c07af9d2 261=item B<create_type_constraint_union (@type_constraint_names)>
262
263Given a list of C<@type_constraint_names>, this will return a
264B<Moose::Meta::TypeConstraint::Union> instance.
265
182134e8 266=item B<export_type_contstraints_as_functions>
267
6ba6d68c 268This will export all the current type constraints as functions
269into the caller's namespace. Right now, this is mostly used for
270testing, but it might prove useful to others.
271
182134e8 272=back
273
a15dff8d 274=head2 Type Constraint Constructors
275
6ba6d68c 276The following functions are used to create type constraints.
277They will then register the type constraints in a global store
278where Moose can get to them if it needs to.
a15dff8d 279
6ba6d68c 280See the L<SYNOPOSIS> for an example of how to use these.
a15dff8d 281
6ba6d68c 282=over 4
a15dff8d 283
6ba6d68c 284=item B<type ($name, $where_clause)>
a15dff8d 285
6ba6d68c 286This creates a base type, which has no parent.
a15dff8d 287
79592a54 288=item B<subtype ($name, $parent, $where_clause, ?$message)>
182134e8 289
6ba6d68c 290This creates a named subtype.
d6e2d9a1 291
79592a54 292=item B<subtype ($parent, $where_clause, ?$message)>
182134e8 293
6ba6d68c 294This creates an unnamed subtype and will return the type
295constraint meta-object, which will be an instance of
296L<Moose::Meta::TypeConstraint>.
a15dff8d 297
fcec2383 298=item B<enum ($name, @values)>
299
2c0cbef7 300This will create a basic subtype for a given set of strings.
301The resulting constraint will be a subtype of C<Str> and
302will match any of the items in C<@values>. See the L<SYNOPSIS>
303for a simple example.
304
305B<NOTE:> This is not a true proper enum type, it is simple
306a convient constraint builder.
307
6ba6d68c 308=item B<as>
a15dff8d 309
6ba6d68c 310This is just sugar for the type constraint construction syntax.
a15dff8d 311
6ba6d68c 312=item B<where>
a15dff8d 313
6ba6d68c 314This is just sugar for the type constraint construction syntax.
76d37e5a 315
316=item B<message>
317
318This is just sugar for the type constraint construction syntax.
a15dff8d 319
6ba6d68c 320=back
a15dff8d 321
6ba6d68c 322=head2 Type Coercion Constructors
a15dff8d 323
6ba6d68c 324Type constraints can also contain type coercions as well. In most
325cases Moose will run the type-coercion code first, followed by the
326type constraint check. This feature should be used carefully as it
327is very powerful and could easily take off a limb if you are not
328careful.
a15dff8d 329
6ba6d68c 330See the L<SYNOPOSIS> for an example of how to use these.
a15dff8d 331
6ba6d68c 332=over 4
a15dff8d 333
6ba6d68c 334=item B<coerce>
a15dff8d 335
6ba6d68c 336=item B<from>
a15dff8d 337
6ba6d68c 338This is just sugar for the type coercion construction syntax.
339
340=item B<via>
a15dff8d 341
6ba6d68c 342This is just sugar for the type coercion construction syntax.
a15dff8d 343
344=back
345
346=head1 BUGS
347
348All complex software has bugs lurking in it, and this module is no
349exception. If you find a bug please either email me, or add the bug
350to cpan-RT.
351
a15dff8d 352=head1 AUTHOR
353
354Stevan Little E<lt>stevan@iinteractive.comE<gt>
355
356=head1 COPYRIGHT AND LICENSE
357
358Copyright 2006 by Infinity Interactive, Inc.
359
360L<http://www.iinteractive.com>
361
362This library is free software; you can redistribute it and/or modify
363it under the same terms as Perl itself.
364
81dc201f 365=cut