Commit | Line | Data |
ca9d7442 |
1 | package MooseX::Types::CheckedUtilExports; |
2 | |
9fd37647 |
3 | #ABSTRACT: Wrap L<Moose::Util::TypeConstraints> to be safer for L<MooseX::Types> |
4 | |
ca9d7442 |
5 | use strict; |
6 | use warnings; |
7 | use Moose::Util::TypeConstraints (); |
8 | use Moose::Exporter; |
9 | use Sub::Name; |
10 | use Carp; |
11 | |
4eb26e95 |
12 | use namespace::autoclean; |
ca9d7442 |
13 | |
14 | my $StringFoundMsg = |
15 | q{WARNING: String found where Type expected (did you use a => instead of a , ?)}; |
16 | |
17 | my @exports = qw/type subtype maybe_type duck_type enum coerce from as/; |
18 | |
19 | =head1 DESCRIPTION |
20 | |
21 | Prevents errors like: |
22 | |
23 | subtype Foo => |
24 | ... |
25 | |
26 | Which should be written as: |
27 | |
28 | subtype Foo, |
29 | ... |
30 | |
31 | When using L<MooseX::Types>. Exported by that module. |
32 | |
33 | Exports checked versions of the following subs: |
34 | |
35 | C<type> C<subtype> C<maybe_type> C<duck_type> C<enum> C<coerce> C<from> C<as> |
36 | |
37 | While C<class_type> and C<role_type> will also register the type in the library. |
38 | |
39 | From L<Moose::Util::TypeConstraints>. See that module for syntax. |
40 | |
41 | =cut |
42 | |
ca9d7442 |
43 | for my $export (@exports) { |
44 | no strict 'refs'; |
45 | |
46 | *{$export} = sub { |
47 | my $caller = shift; |
48 | |
49 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
50 | |
51 | carp $StringFoundMsg |
52 | unless ref($_[0]) || |
53 | $_[0] =~ /\b::\b/ || # qualified type |
54 | $caller->get_registered_class_type($_[0]) || |
55 | $caller->get_registered_role_type($_[0]); |
56 | |
57 | goto &{"Moose::Util::TypeConstraints::$export"}; |
58 | } |
59 | } |
60 | |
f8779bc5 |
61 | Moose::Exporter->setup_import_methods( |
62 | with_caller => [ @exports, 'class_type', 'role_type' ] |
63 | ); |
64 | |
ca9d7442 |
65 | sub class_type { |
66 | my $caller = shift; |
67 | |
68 | $caller->register_class_type( |
69 | Moose::Util::TypeConstraints::class_type(@_) |
70 | ); |
71 | } |
72 | |
73 | sub role_type ($;$) { |
74 | my ($caller, $name, $opts) = @_; |
75 | |
76 | $caller->register_role_type( |
77 | Moose::Util::TypeConstraints::role_type($name, $opts) |
78 | ); |
79 | } |
80 | |
81 | =head1 SEE ALSO |
82 | |
83 | L<MooseX::Types> |
84 | |
85 | =head1 LICENSE |
86 | |
87 | This program is free software; you can redistribute it and/or modify |
88 | it under the same terms as perl itself. |
89 | |
90 | =cut |
91 | |
92 | 1; |