many-many-docs
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints.pm
1
2 package Moose::Util::TypeConstraints;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed';
9
10 our $VERSION = '0.02';
11
12 use Moose::Meta::TypeConstraint;
13 use Moose::Meta::TypeCoercion;
14
15 sub import {
16         shift;
17         my $pkg = shift || caller();
18         return if $pkg eq '-no-export';
19         no strict 'refs';
20         foreach my $export (qw(type subtype as where coerce from via find_type_constraint)) {
21                 *{"${pkg}::${export}"} = \&{"${export}"};
22         }       
23 }
24
25 {
26     my %TYPES;
27     sub find_type_constraint { $TYPES{$_[0]} }
28
29     sub _create_type_constraint { 
30         my ($name, $parent, $check) = @_;
31         (!exists $TYPES{$name})
32             || confess "The type constraint '$name' has already been created"
33                 if defined $name;
34         $parent = $TYPES{$parent} if defined $parent;
35         my $constraint = Moose::Meta::TypeConstraint->new(
36             name       => $name || '__ANON__',
37             parent     => $parent,            
38             constraint => $check,           
39         );
40         $TYPES{$name} = $constraint if defined $name;
41         return $constraint;
42     }
43
44     sub _install_type_coercions { 
45         my ($type_name, $coercion_map) = @_;
46         my $type = $TYPES{$type_name};
47         (!$type->has_coercion)
48             || confess "The type coercion for '$type_name' has already been registered";        
49         my $type_coercion = Moose::Meta::TypeCoercion->new(
50             type_coercion_map => $coercion_map,
51             type_constraint   => $type
52         );            
53         $type->coercion($type_coercion);
54     }
55     
56     sub export_type_contstraints_as_functions {
57         my $pkg = caller();
58             no strict 'refs';
59         foreach my $constraint (keys %TYPES) {
60                 *{"${pkg}::${constraint}"} = $TYPES{$constraint}->_compiled_type_constraint;
61         }        
62     }    
63 }
64
65 # type constructors
66
67 sub type ($$) {
68         my ($name, $check) = @_;
69         _create_type_constraint($name, undef, $check);
70 }
71
72 sub subtype ($$;$) {
73         unshift @_ => undef if scalar @_ == 2;
74         _create_type_constraint(@_);
75 }
76
77 sub coerce ($@) {
78     my ($type_name, @coercion_map) = @_;   
79     _install_type_coercions($type_name, \@coercion_map);
80 }
81
82 sub as    ($) { $_[0] }
83 sub from  ($) { $_[0] }
84 sub where (&) { $_[0] }
85 sub via   (&) { $_[0] }
86
87 # define some basic types
88
89 type Any => where { 1 };
90
91 type Value => where { !ref($_) };
92 type Ref   => where {  ref($_) };
93
94 subtype Int => as Value => where {  Scalar::Util::looks_like_number($_) };
95 subtype Str => as Value => where { !Scalar::Util::looks_like_number($_) };
96
97 subtype ScalarRef => as Ref => where { ref($_) eq 'SCALAR' };   
98 subtype ArrayRef  => as Ref => where { ref($_) eq 'ARRAY'  };
99 subtype HashRef   => as Ref => where { ref($_) eq 'HASH'   };   
100 subtype CodeRef   => as Ref => where { ref($_) eq 'CODE'   };
101 subtype RegexpRef => as Ref => where { ref($_) eq 'Regexp' };   
102
103 # NOTE: 
104 # blessed(qr/.../) returns true,.. how odd
105 subtype Object => as Ref => where { blessed($_) && blessed($_) ne 'Regexp' };
106
107 1;
108
109 __END__
110
111 =pod
112
113 =head1 NAME
114
115 Moose::Util::TypeConstraints - Type constraint system for Moose
116
117 =head1 SYNOPSIS
118
119   use Moose::Util::TypeConstraints;
120
121   type Num => where { Scalar::Util::looks_like_number($_) };
122   
123   subtype Natural 
124       => as Num 
125       => where { $_ > 0 };
126   
127   subtype NaturalLessThanTen 
128       => as Natural
129       => where { $_ < 10 };
130       
131   coerce Num 
132       => from Str
133         => via { 0+$_ }; 
134
135 =head1 DESCRIPTION
136
137 This module provides Moose with the ability to create type contraints 
138 to be are used in both attribute definitions and for method argument 
139 validation. 
140
141 =head2 Important Caveat
142
143 This is B<NOT> a type system for Perl 5. These are type constraints, 
144 and they are not used by Moose unless you tell it to. No type 
145 inference is performed, expression are not typed, etc. etc. etc. 
146
147 This is simply a means of creating small constraint functions which 
148 can be used to simply your own type-checking code.
149
150 =head2 Default Type Constraints
151
152 This module also provides a simple hierarchy for Perl 5 types, this 
153 could probably use some work, but it works for me at the moment.
154
155   Any
156       Value
157           Int
158           Str
159       Ref
160           ScalarRef
161           ArrayRef
162           HashRef
163           CodeRef
164           RegexpRef
165           Object        
166
167 Suggestions for improvement are welcome.
168     
169 =head1 FUNCTIONS
170
171 =head2 Type Constraint Registry
172
173 =over 4
174
175 =item B<find_type_constraint ($type_name)>
176
177 This function can be used to locate a specific type constraint 
178 meta-object. What you do with it from there is up to you :)
179
180 =item B<export_type_contstraints_as_functions>
181
182 This will export all the current type constraints as functions 
183 into the caller's namespace. Right now, this is mostly used for 
184 testing, but it might prove useful to others.
185
186 =back
187
188 =head2 Type Constraint Constructors
189
190 The following functions are used to create type constraints. 
191 They will then register the type constraints in a global store 
192 where Moose can get to them if it needs to. 
193
194 See the L<SYNOPOSIS> for an example of how to use these.
195
196 =over 4
197
198 =item B<type ($name, $where_clause)>
199
200 This creates a base type, which has no parent. 
201
202 =item B<subtype ($name, $parent, $where_clause)>
203
204 This creates a named subtype. 
205
206 =item B<subtype ($parent, $where_clause)>
207
208 This creates an unnamed subtype and will return the type 
209 constraint meta-object, which will be an instance of 
210 L<Moose::Meta::TypeConstraint>. 
211
212 =item B<as>
213
214 This is just sugar for the type constraint construction syntax.
215
216 =item B<where>
217
218 This is just sugar for the type constraint construction syntax.
219
220 =back
221
222 =head2 Type Coercion Constructors
223
224 Type constraints can also contain type coercions as well. In most 
225 cases Moose will run the type-coercion code first, followed by the 
226 type constraint check. This feature should be used carefully as it 
227 is very powerful and could easily take off a limb if you are not 
228 careful.
229
230 See the L<SYNOPOSIS> for an example of how to use these.
231
232 =over 4
233
234 =item B<coerce>
235
236 =item B<from>
237
238 This is just sugar for the type coercion construction syntax.
239
240 =item B<via>
241
242 This is just sugar for the type coercion construction syntax.
243
244 =back
245
246 =head1 BUGS
247
248 All complex software has bugs lurking in it, and this module is no 
249 exception. If you find a bug please either email me, or add the bug
250 to cpan-RT.
251
252 =head1 AUTHOR
253
254 Stevan Little E<lt>stevan@iinteractive.comE<gt>
255
256 =head1 COPYRIGHT AND LICENSE
257
258 Copyright 2006 by Infinity Interactive, Inc.
259
260 L<http://www.iinteractive.com>
261
262 This library is free software; you can redistribute it and/or modify
263 it under the same terms as Perl itself. 
264
265 =cut