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