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