changes from jrockway with some additional docs
[gitmo/MooseX-Types-Path-Class.git] / lib / MooseX / Types / Path / Class.pm
CommitLineData
fd56ddb6 1package MooseX::Types::Path::Class;
2
3use warnings FATAL => 'all';
4use strict;
5
2474bec1 6our $VERSION = '0.06';
7$VERSION = eval $VERSION;
52e426e5 8our $AUTHORITY = 'cpan:THEPLER';
fd56ddb6 9
fd56ddb6 10use Path::Class ();
d752957e 11# TODO: export dir() and file() from Path::Class? (maybe)
fd56ddb6 12
13use MooseX::Types
2474bec1 14 -declare => [qw( Dir File ExistingDir ExistingFile NonExistingDir NonExistingFile )];
fd56ddb6 15
dae5e463 16use MooseX::Types::Moose qw(Str ArrayRef);
fd56ddb6 17
dae5e463 18class_type('Path::Class::Dir');
19class_type('Path::Class::File');
fd56ddb6 20
dae5e463 21subtype Dir, as 'Path::Class::Dir';
22subtype File, as 'Path::Class::File';
fd56ddb6 23
4b3d0bfc 24subtype ExistingFile, as File, where { -e $_->stringify },
2474bec1 25 message { "File '$_' must exist." };
4b3d0bfc 26
2474bec1 27subtype NonExistingFile, as File, where { !-e $_->stringify },
28 message { "File '$_' must not exist." };
4b3d0bfc 29
2474bec1 30subtype ExistingDir, as Dir,
31 where { -e $_->stringify && -d $_->stringify },
32 message { "Directory '$_' must exist" };
33
34subtype NonExistingDir, as Dir,
35 where { !-e $_->stringify },
36 message { "Directory '$_' not must exist" };
37
38for my $type ( 'Path::Class::Dir', Dir, ExistingDir, NonExistingDir ) {
52e426e5 39 coerce $type,
2474bec1 40 from Str, via { Path::Class::Dir->new("$_") },
52e426e5 41 from ArrayRef, via { Path::Class::Dir->new(@$_) };
52e426e5 42}
fd56ddb6 43
2474bec1 44for my $type ( 'Path::Class::File', File, ExistingFile, NonExistingFile ) {
52e426e5 45 coerce $type,
2474bec1 46 from Str, via { Path::Class::File->new("$_") },
52e426e5 47 from ArrayRef, via { Path::Class::File->new(@$_) };
dae5e463 48}
fd56ddb6 49
dae5e463 50# optionally add Getopt option type
dae5e463 51eval { require MooseX::Getopt; };
2b21d04e 52if ( !$@ ) {
dae5e463 53 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', )
2474bec1 54 for (
55 'Path::Class::Dir', 'Path::Class::File',
56 Dir, File,
57 ExistingDir, ExistingFile,
58 NonExistingFile, NonExistingDir,
59 );
52e426e5 60}
fd56ddb6 61
621;
63__END__
64
d752957e 65
fd56ddb6 66=head1 NAME
67
68MooseX::Types::Path::Class - A Path::Class type library for Moose
69
70
71=head1 SYNOPSIS
72
73 package MyClass;
74 use Moose;
dae5e463 75 use MooseX::Types::Path::Class;
76 with 'MooseX::Getopt'; # optional
fd56ddb6 77
78 has 'dir' => (
79 is => 'ro',
dae5e463 80 isa => 'Path::Class::Dir',
fd56ddb6 81 required => 1,
82 coerce => 1,
83 );
84
85 has 'file' => (
86 is => 'ro',
dae5e463 87 isa => 'Path::Class::File',
fd56ddb6 88 required => 1,
89 coerce => 1,
90 );
91
92 # these attributes are coerced to the
93 # appropriate Path::Class objects
255a36e7 94 MyClass->new( dir => '/some/directory/', file => '/some/file' );
fd56ddb6 95
4b3d0bfc 96
fd56ddb6 97=head1 DESCRIPTION
98
dae5e463 99MooseX::Types::Path::Class creates common L<Moose> types,
100coercions and option specifications useful for dealing
101with L<Path::Class> objects as L<Moose> attributes.
fd56ddb6 102
dae5e463 103Coercions (see L<Moose::Util::TypeConstraints>) are made
fd56ddb6 104from both 'Str' and 'ArrayRef' to both L<Path::Class::Dir> and
dae5e463 105L<Path::Class::File> objects. If you have L<MooseX::Getopt> installed,
106the Getopt option type ("=s") will be added for both
107L<Path::Class::Dir> and L<Path::Class::File>.
fd56ddb6 108
d752957e 109
fd56ddb6 110=head1 EXPORTS
111
dae5e463 112None of these are exported by default. They are provided via
113L<MooseX::Types>.
fd56ddb6 114
115=over
116
dae5e463 117=item Dir, File
118
119These exports can be used instead of the full class names. Example:
120
121 package MyClass;
122 use Moose;
123 use MooseX::Types::Path::Class qw(Dir File);
124
125 has 'dir' => (
126 is => 'ro',
127 isa => Dir,
128 required => 1,
129 coerce => 1,
130 );
131
132 has 'file' => (
133 is => 'ro',
134 isa => File,
135 required => 1,
136 coerce => 1,
137 );
138
2474bec1 139Note that there are no quotes around Dir or File.
140
4b3d0bfc 141=item ExistingDir, ExistingFile
142
143Like File and Dir, but the files or directories must exist on disk
144when the type is checked, and the object on disk must be a file (for
145ExistingFile) or directory (for ExistingDir).
146
2474bec1 147At no point will this library attempt to coerce a path into existence
148by creating directories or files. The coercions for ExistingDir and ExistingFile
149simply coerce 'Str' and 'ArrayRef' to L<Path::Class> objects.
150
151These types do rely on I/O. The case could be made that this makes them not
152suitable to be called "types". Consider a file that gets removed after the
153ExistingFile type is checked. This can be a source of difficult to find bugs
154(you've been warned). Often, you're going to check (either explicitly or implicitly)
155whether a path exists at the point where you're actually going to use it
156anyway. In such cases you may be better off using the Dir or File type and then
157later (say, in some method) check for existence yourself instead of using these types
158constraints that you can't really trust to remain true indefinitely.
159
160
161=item NonExistingDir, NonExistingFile
162
163Like File and Dir, but the path must not exist on disk
164when the type is checked.
165
166At no point will this library attempt to coerce a path into non-existence
167by removing directories or files. The coercions for NonExistingDir and NonExistingFile
168simply coerce 'Str' and 'ArrayRef' to L<Path::Class> objects.
169
170The same caveats regarding I/O for the Existing* types above apply here as well.
dae5e463 171
2474bec1 172=item is_$type($value)
dae5e463 173
2474bec1 174Returns true or false based on whether $value passes the constraint for $type.
dae5e463 175
2474bec1 176=item to_$type($value)
fd56ddb6 177
2474bec1 178Attempts to coerce $value to the given $type. Returns the coerced value
4d069f3e 179or false if the coercion failed.
fd56ddb6 180
181=back
182
183
184=head1 DEPENDENCIES
185
dae5e463 186L<Moose>, L<MooseX::Types>, L<Path::Class>
fd56ddb6 187
188
189=head1 BUGS AND LIMITATIONS
190
192bdd61 191If you find a bug please either email the author, or add
52e426e5 192the bug to cpan-RT L<http://rt.cpan.org>.
fd56ddb6 193
194
195=head1 AUTHOR
196
197Todd Hepler C<< <thepler@employees.org> >>
198
199
200=head1 LICENCE AND COPYRIGHT
201
2474bec1 202Copyright (c) 2007-2009, Todd Hepler C<< <thepler@employees.org> >>.
fd56ddb6 203
204This module is free software; you can redistribute it and/or
205modify it under the same terms as Perl itself. See L<perlartistic>.
206
207