TypeLibrary now uses list_all_builtin_type_constraints (phaylon)
[gitmo/MooseX-Types.git] / lib / MooseX / TypeLibrary.pm
CommitLineData
8af0a70d 1package MooseX::TypeLibrary;
2
3=head1 NAME
4
5MooseX::TypeLibrary - Organise your Moose types in libraries
6
7=cut
8
9use warnings;
10use strict;
11
12use Sub::Uplevel;
13use Moose::Util::TypeConstraints;
14use MooseX::TypeLibrary::Base;
15use Sub::Install qw( install_sub );
16use namespace::clean;
17
18our $VERSION = 0.01;
19
20my $UndefMsg = q{Action for type '%s' not yet defined in library '%s'};
21
22=head1 SYNOPSIS
23
24 #
25 # Library Definition
26 #
27 package MyLibrary;
28 use strict;
29
30 # predeclare our own types
31 use MooseX::TypeLibrary
32 -declare => [qw( PositiveInt NegativeInt )];
33
34 # import builtin types
35 use MooseX::TypeLibrary::Moose 'Int';
36
37 # type definition
38 subtype PositiveInt,
39 as Int,
40 where { $_ > 0 },
41 message { "Int is not larger than 0" };
42
43 subtype NegativeInt,
44 as Int,
45 where { $_ < 0 },
46 message { "Int is not smaller than 0" };
47
48 # type coercion
49 coerce PositiveInt,
50 from Int,
51 via { 1 };
52
53 1;
54
55 #
56 # Usage
57 #
58 package Foo;
59 use Moose;
60 use MyLibrary qw( PositiveInt NegativeInt );
61
62 # use the exported constants as type names
63 has 'bar',
64 isa => PositiveInt,
65 is => 'rw';
66 has 'baz',
67 isa => NegativeInt,
68 is => 'rw';
69
70 sub quux {
71 my ($self, $value);
72
73 # test the value
74 print "positive\n" if is_PositiveInt($value);
75 print "negative\n" if is_NegativeInt($value);
76
77 # coerce the value, NegativeInt doesn't have a coercion
78 # helper, since it didn't define any coercions.
79 $value = to_PositiveInt($value) or die "Cannot coerce";
80 }
81
82 1;
83
84=head1 DESCRIPTION
85
86The types provided with L<Moose> are by design global. This package helps
87you to organise and selectively import your own and the built-in types in
88libraries. As a nice side effect, it catches typos at compile-time too.
89
90However, the main reason for this module is to provide an easy way to not
91have conflicts with your type names, since the internal fully qualified
92names of the types will be prefixed with the library's name.
93
94This module will also provide you with some helper functions to make it
95easier to use Moose types in your code.
96
97=head1 TYPE HANDLER FUNCTIONS
98
99=head2 $type
100
101A constant with the name of your type. It contains the type's fully
102qualified name. Takes no value, as all constants.
103
104=head2 is_$type
105
106This handler takes a value and tests if it is a valid value for this
107C<$type>. It will return true or false.
108
109=head2 to_$type
110
111A handler that will take a value and coerce it into the C<$type>. It will
112return a false value if the type could not be coerced.
113
114B<Important Note>: This handler will only be exported for types that can
115do type coercion. This has the advantage that a coercion to a type that
116cannot hasn't defined any coercions will lead to a compile-time error.
117
118=head1 LIBRARY DEFINITION
119
120A MooseX::TypeLibrary is just a normal Perl module. Unlike Moose
121itself, it does not install C<use strict> and C<use warnings> in your
122class by default, so this is up to you.
123
124The only thing a library is required to do is
125
126 use MooseX::TypeLibrary -declare => \@types;
127
128with C<@types> being a list of types you wish to define in this library.
129This line will install a proper base class in your package as well as the
130full set of L<handlers|/"TYPE HANDLER FUNCTIONS"> for your declared
131types. It will then hand control over to L<Moose::Util::TypeConstraints>'
132C<import> method to export the functions you will need to declare your
133types.
134
135If you want to use Moose' built-in types (e.g. for subtyping) you will
136want to
137
138 use MooseX::TypeLibrary::Moose @types;
139
140to import the helpers from the shipped L<MooseX::TypeLibrary::Moose>
141library which can export all types that come with Moose.
142
143You will have to define coercions for your types or your library won't
144export a L</to_$type> coercion helper for it.
145
146=head1 LIBRARY USAGE
147
148You can import the L<"type helpers"|/"TYPE HANDLER FUNCTIONS"> of a
149library by C<use>ing it with a list of types to import as arguments. If
150you want all of them, use the C<:all> tag. For example:
151
152 use MyLibrary ':all';
153 use MyOtherLibrary qw( TypeA TypeB );
154
155MooseX::TypeLibrary comes with a library of Moose' built-in types called
156L<MooseX::TypeLibrary::Moose>.
157
158=head1 METHODS
159
160=head2 import
161
162=cut
163
164sub import {
165 my ($class, %args) = @_;
166 my $callee = caller;
167
168 # inject base class into new library
169 { no strict 'refs';
170 unshift @{ $callee . '::ISA' }, 'MooseX::TypeLibrary::Base';
171 }
172
173 # generate predeclared type helpers
174 if (my @declare = @{ $args{ -declare } || [] }) {
175 for my $type (@declare) {
176 $callee->add_type($type);
177 $callee->export_type_into(
178 $callee, $type,
179 sprintf($UndefMsg, $type, $callee),
180 -full => 1,
181 );
182 }
183 }
184
185 # run type constraints import
186 return uplevel 1,
187 Moose::Util::TypeConstraints->can('import'),
188 'Moose::Util::TypeConstraints';
189}
190
191=head2 type_export_generator
192
193=cut
194
195sub type_export_generator {
196 my ($class, $type, $full) = @_;
197 return sub { $full };
198}
199
200=head2 coercion_export_generator
201
202=cut
203
204sub coercion_export_generator {
205 my ($class, $type, $full, $undef_msg) = @_;
206 return sub {
207 my ($value) = @_;
208
209 # we need a type object
210 my $tobj = find_type_constraint($full) or croak $undef_msg;
211 my $return = $tobj->coerce($value);
212
213 # non-successful coercion returns false
214 return unless $tobj->check($return);
215
216 return $return;
217 }
218}
219
220=head2 check_export_generator
221
222=cut
223
224sub check_export_generator {
225 my ($class, $type, $full, $undef_msg) = @_;
226 return sub {
227 my ($value) = @_;
228
229 # we need a type object
230 my $tobj = find_type_constraint($full) or croak $undef_msg;
231
232 return $tobj->check($value);
233 }
234}
235
236=head1 SEE ALSO
237
238L<Moose>, L<Moose::Util::TypeConstraints>, L<MooseX::TypeLibrary::Moose>
239
240=head1 AUTHOR AND COPYRIGHT
241
242Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
243the C<#moose> cabal on C<irc.perl.org>.
244
245=head1 LICENSE
246
247This program is free software; you can redistribute it and/or modify
248it under the same terms as perl itself.
249
250=cut
251
2521;