Commit | Line | Data |
fd56ddb6 |
1 | package MooseX::Types::Path::Class; |
2 | |
3 | use warnings FATAL => 'all'; |
4 | use strict; |
5 | |
fd56ddb6 |
6 | use Path::Class (); |
d752957e |
7 | # TODO: export dir() and file() from Path::Class? (maybe) |
fd56ddb6 |
8 | |
9 | use MooseX::Types |
2474bec1 |
10 | -declare => [qw( Dir File ExistingDir ExistingFile NonExistingDir NonExistingFile )]; |
fd56ddb6 |
11 | |
dae5e463 |
12 | use MooseX::Types::Moose qw(Str ArrayRef); |
fd56ddb6 |
13 | |
dae5e463 |
14 | class_type('Path::Class::Dir'); |
15 | class_type('Path::Class::File'); |
fd56ddb6 |
16 | |
dae5e463 |
17 | subtype Dir, as 'Path::Class::Dir'; |
18 | subtype File, as 'Path::Class::File'; |
fd56ddb6 |
19 | |
4b3d0bfc |
20 | subtype ExistingFile, as File, where { -e $_->stringify }, |
2474bec1 |
21 | message { "File '$_' must exist." }; |
4b3d0bfc |
22 | |
2474bec1 |
23 | subtype NonExistingFile, as File, where { !-e $_->stringify }, |
24 | message { "File '$_' must not exist." }; |
4b3d0bfc |
25 | |
2474bec1 |
26 | subtype ExistingDir, as Dir, |
27 | where { -e $_->stringify && -d $_->stringify }, |
28 | message { "Directory '$_' must exist" }; |
29 | |
30 | subtype NonExistingDir, as Dir, |
31 | where { !-e $_->stringify }, |
32 | message { "Directory '$_' not must exist" }; |
33 | |
34 | for 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 |
40 | for 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 |
47 | eval { require MooseX::Getopt; }; |
2b21d04e |
48 | if ( !$@ ) { |
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 | |
58 | 1; |
59 | __END__ |
60 | |
d752957e |
61 | |
fd56ddb6 |
62 | =head1 NAME |
63 | |
64 | MooseX::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 |
95 | MooseX::Types::Path::Class creates common L<Moose> types, |
96 | coercions and option specifications useful for dealing |
97 | with L<Path::Class> objects as L<Moose> attributes. |
fd56ddb6 |
98 | |
dae5e463 |
99 | Coercions (see L<Moose::Util::TypeConstraints>) are made |
fd56ddb6 |
100 | from both 'Str' and 'ArrayRef' to both L<Path::Class::Dir> and |
dae5e463 |
101 | L<Path::Class::File> objects. If you have L<MooseX::Getopt> installed, |
102 | the Getopt option type ("=s") will be added for both |
103 | L<Path::Class::Dir> and L<Path::Class::File>. |
fd56ddb6 |
104 | |
d752957e |
105 | |
fd56ddb6 |
106 | =head1 EXPORTS |
107 | |
dae5e463 |
108 | None of these are exported by default. They are provided via |
109 | L<MooseX::Types>. |
fd56ddb6 |
110 | |
111 | =over |
112 | |
dae5e463 |
113 | =item Dir, File |
114 | |
115 | These 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 |
135 | Note that there are no quotes around Dir or File. |
136 | |
4b3d0bfc |
137 | =item ExistingDir, ExistingFile |
138 | |
139 | Like File and Dir, but the files or directories must exist on disk |
140 | when the type is checked, and the object on disk must be a file (for |
141 | ExistingFile) or directory (for ExistingDir). |
142 | |
2474bec1 |
143 | At no point will this library attempt to coerce a path into existence |
144 | by creating directories or files. The coercions for ExistingDir and ExistingFile |
145 | simply coerce 'Str' and 'ArrayRef' to L<Path::Class> objects. |
146 | |
147 | These types do rely on I/O. The case could be made that this makes them not |
148 | suitable to be called "types". Consider a file that gets removed after the |
149 | ExistingFile 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) |
151 | whether a path exists at the point where you're actually going to use it |
152 | anyway. In such cases you may be better off using the Dir or File type and then |
153 | later (say, in some method) check for existence yourself instead of using these types |
154 | constraints that you can't really trust to remain true indefinitely. |
155 | |
156 | |
157 | =item NonExistingDir, NonExistingFile |
158 | |
159 | Like File and Dir, but the path must not exist on disk |
160 | when the type is checked. |
161 | |
162 | At no point will this library attempt to coerce a path into non-existence |
163 | by removing directories or files. The coercions for NonExistingDir and NonExistingFile |
164 | simply coerce 'Str' and 'ArrayRef' to L<Path::Class> objects. |
165 | |
166 | The 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 |
170 | Returns true or false based on whether $value passes the constraint for $type. |
dae5e463 |
171 | |
2474bec1 |
172 | =item to_$type($value) |
fd56ddb6 |
173 | |
2474bec1 |
174 | Attempts to coerce $value to the given $type. Returns the coerced value |
4d069f3e |
175 | or false if the coercion failed. |
fd56ddb6 |
176 | |
177 | =back |
178 | |
179 | |
f398d545 |
180 | =head1 SEE ALSO |
181 | |
182 | L<MooseX::Types::Path::Class::MoreCoercions> |
183 | |
184 | |
fd56ddb6 |
185 | =head1 DEPENDENCIES |
186 | |
dae5e463 |
187 | L<Moose>, L<MooseX::Types>, L<Path::Class> |
fd56ddb6 |
188 | |
189 | |
190 | =head1 BUGS AND LIMITATIONS |
191 | |
192bdd61 |
192 | If you find a bug please either email the author, or add |
52e426e5 |
193 | the bug to cpan-RT L<http://rt.cpan.org>. |
fd56ddb6 |
194 | |
195 | |
196 | =head1 AUTHOR |
197 | |
198 | Todd Hepler C<< <thepler@employees.org> >> |
199 | |
200 | |
201 | =head1 LICENCE AND COPYRIGHT |
202 | |
2474bec1 |
203 | Copyright (c) 2007-2009, Todd Hepler C<< <thepler@employees.org> >>. |
fd56ddb6 |
204 | |
205 | This module is free software; you can redistribute it and/or |
206 | modify it under the same terms as Perl itself. See L<perlartistic>. |
207 | |
208 | |