Deprecate the Role type
[gitmo/Moose.git] / lib / Moose / Util / TypeConstraints / OptimizedConstraints.pm
1 package Moose::Util::TypeConstraints::OptimizedConstraints;
2
3 use strict;
4 use warnings;
5
6 use Class::MOP;
7 use Scalar::Util 'blessed', 'looks_like_number';
8
9 our $VERSION   = '1.08';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 sub Value { defined($_[0]) && !ref($_[0]) }
14
15 sub Ref { ref($_[0]) }
16
17 # We need to use a temporary here to flatten LVALUEs, for instance as in
18 # Str(substr($_,0,255)).
19 sub Str {
20     my $value = $_[0];
21     defined($value) && ref(\$value) eq 'SCALAR'
22 }
23
24 sub Num { !ref($_[0]) && looks_like_number($_[0]) }
25
26 sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
27
28 sub ScalarRef { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' }
29 sub ArrayRef  { ref($_[0]) eq 'ARRAY'  }
30 sub HashRef   { ref($_[0]) eq 'HASH'   }
31 sub CodeRef   { ref($_[0]) eq 'CODE'   }
32 sub RegexpRef { ref($_[0]) eq 'Regexp' }
33 sub GlobRef   { ref($_[0]) eq 'GLOB'   }
34
35 sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") }
36
37 sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
38
39 sub Role {
40     Moose::Deprecated::deprecated(
41         feature => 'Role type',
42         message =>
43             'The Role type has been deprecated. Maybe you meant to create a RoleName type?'
44     );
45     blessed( $_[0] ) && $_[0]->can('does');
46 }
47
48 sub ClassName {
49     return Class::MOP::is_class_loaded( $_[0] );
50 }
51
52 sub RoleName {
53     ClassName($_[0])
54     && (Class::MOP::class_of($_[0]) || return)->isa('Moose::Meta::Role')
55 }
56
57 # NOTE:
58 # we have XS versions too, ...
59 # 04:09 <@konobi> nothingmuch: konobi.co.uk/code/utilsxs.tar.gz
60 # 04:09 <@konobi> or utilxs.tar.gz iirc
61
62 1;
63
64 __END__
65
66 =pod
67
68 =head1 NAME
69
70 Moose::Util::TypeConstraints::OptimizedConstraints - Optimized constraint
71 bodies for various moose types
72
73 =head1 DESCRIPTION
74
75 This file contains the hand optimized versions of Moose type constraints,
76 no user serviceable parts inside.
77
78 =head1 FUNCTIONS
79
80 =over 4
81
82 =item C<Value>
83
84 =item C<Ref>
85
86 =item C<Str>
87
88 =item C<Num>
89
90 =item C<Int>
91
92 =item C<ScalarRef>
93
94 =item C<ArrayRef>
95
96 =item C<HashRef>
97
98 =item C<CodeRef>
99
100 =item C<RegexpRef>
101
102 =item C<GlobRef>
103
104 =item C<FileHandle>
105
106 =item C<Object>
107
108 =item C<Role>
109
110 =item C<ClassName>
111
112 =item C<RoleName>
113
114 =back
115
116 =head1 BUGS
117
118 See L<Moose/BUGS> for details on reporting bugs.
119
120 =head1 AUTHOR
121
122 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
123
124 =head1 COPYRIGHT AND LICENSE
125
126 Copyright 2007-2009 by Infinity Interactive, Inc.
127
128 L<http://www.iinteractive.com>
129
130 This library is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.
132
133 =cut