Commit | Line | Data |
fd56ddb6 |
1 | package MooseX::Types::Path::Class; |
2 | |
3 | use warnings FATAL => 'all'; |
4 | use strict; |
5 | |
192bdd61 |
6 | our $VERSION = '0.05'; |
52e426e5 |
7 | our $AUTHORITY = 'cpan:THEPLER'; |
fd56ddb6 |
8 | |
fd56ddb6 |
9 | use Path::Class (); |
d752957e |
10 | # TODO: export dir() and file() from Path::Class? (maybe) |
fd56ddb6 |
11 | |
12 | use MooseX::Types |
4b3d0bfc |
13 | -declare => [qw( Dir File ExistingDir ExistingFile )]; |
fd56ddb6 |
14 | |
dae5e463 |
15 | use MooseX::Types::Moose qw(Str ArrayRef); |
fd56ddb6 |
16 | |
dae5e463 |
17 | class_type('Path::Class::Dir'); |
18 | class_type('Path::Class::File'); |
fd56ddb6 |
19 | |
dae5e463 |
20 | subtype Dir, as 'Path::Class::Dir'; |
21 | subtype File, as 'Path::Class::File'; |
fd56ddb6 |
22 | |
4b3d0bfc |
23 | subtype ExistingFile, as File, where { -e $_->stringify }, |
24 | message { "File '$_' must exist." }; |
25 | |
26 | subtype ExistingDir, as Dir, where { -e $_->stringify && -d $_->stringify }, |
27 | message { "Directory '$_' must exist" }; |
28 | |
29 | for my $type ( 'Path::Class::Dir', Dir, ExistingDir ) { |
52e426e5 |
30 | coerce $type, |
31 | from Str, via { Path::Class::Dir->new($_) }, |
32 | from ArrayRef, via { Path::Class::Dir->new(@$_) }; |
52e426e5 |
33 | } |
fd56ddb6 |
34 | |
4b3d0bfc |
35 | for my $type ( 'Path::Class::File', File, ExistingFile ) { |
52e426e5 |
36 | coerce $type, |
37 | from Str, via { Path::Class::File->new($_) }, |
38 | from ArrayRef, via { Path::Class::File->new(@$_) }; |
dae5e463 |
39 | } |
fd56ddb6 |
40 | |
dae5e463 |
41 | # optionally add Getopt option type |
dae5e463 |
42 | eval { require MooseX::Getopt; }; |
2b21d04e |
43 | if ( !$@ ) { |
dae5e463 |
44 | MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', ) |
4b3d0bfc |
45 | for ( 'Path::Class::Dir', 'Path::Class::File', Dir, File, ExistingDir, ExistingFile ); |
52e426e5 |
46 | } |
fd56ddb6 |
47 | |
48 | 1; |
49 | __END__ |
50 | |
d752957e |
51 | |
fd56ddb6 |
52 | =head1 NAME |
53 | |
54 | MooseX::Types::Path::Class - A Path::Class type library for Moose |
55 | |
56 | |
57 | =head1 SYNOPSIS |
58 | |
59 | package MyClass; |
60 | use Moose; |
dae5e463 |
61 | use MooseX::Types::Path::Class; |
62 | with 'MooseX::Getopt'; # optional |
fd56ddb6 |
63 | |
64 | has 'dir' => ( |
65 | is => 'ro', |
dae5e463 |
66 | isa => 'Path::Class::Dir', |
fd56ddb6 |
67 | required => 1, |
68 | coerce => 1, |
69 | ); |
70 | |
71 | has 'file' => ( |
72 | is => 'ro', |
dae5e463 |
73 | isa => 'Path::Class::File', |
fd56ddb6 |
74 | required => 1, |
75 | coerce => 1, |
76 | ); |
77 | |
78 | # these attributes are coerced to the |
79 | # appropriate Path::Class objects |
255a36e7 |
80 | MyClass->new( dir => '/some/directory/', file => '/some/file' ); |
fd56ddb6 |
81 | |
4b3d0bfc |
82 | |
fd56ddb6 |
83 | =head1 DESCRIPTION |
84 | |
dae5e463 |
85 | MooseX::Types::Path::Class creates common L<Moose> types, |
86 | coercions and option specifications useful for dealing |
87 | with L<Path::Class> objects as L<Moose> attributes. |
fd56ddb6 |
88 | |
dae5e463 |
89 | Coercions (see L<Moose::Util::TypeConstraints>) are made |
fd56ddb6 |
90 | from both 'Str' and 'ArrayRef' to both L<Path::Class::Dir> and |
dae5e463 |
91 | L<Path::Class::File> objects. If you have L<MooseX::Getopt> installed, |
92 | the Getopt option type ("=s") will be added for both |
93 | L<Path::Class::Dir> and L<Path::Class::File>. |
fd56ddb6 |
94 | |
d752957e |
95 | |
fd56ddb6 |
96 | =head1 EXPORTS |
97 | |
dae5e463 |
98 | None of these are exported by default. They are provided via |
99 | L<MooseX::Types>. |
fd56ddb6 |
100 | |
101 | =over |
102 | |
dae5e463 |
103 | =item Dir, File |
104 | |
105 | These exports can be used instead of the full class names. Example: |
106 | |
107 | package MyClass; |
108 | use Moose; |
109 | use MooseX::Types::Path::Class qw(Dir File); |
110 | |
111 | has 'dir' => ( |
112 | is => 'ro', |
113 | isa => Dir, |
114 | required => 1, |
115 | coerce => 1, |
116 | ); |
117 | |
118 | has 'file' => ( |
119 | is => 'ro', |
120 | isa => File, |
121 | required => 1, |
122 | coerce => 1, |
123 | ); |
124 | |
4b3d0bfc |
125 | =item ExistingDir, ExistingFile |
126 | |
127 | Like File and Dir, but the files or directories must exist on disk |
128 | when the type is checked, and the object on disk must be a file (for |
129 | ExistingFile) or directory (for ExistingDir). |
130 | |
dae5e463 |
131 | Note that there are no quotes around Dir or File. |
132 | |
133 | =item is_Dir($value), is_File($value) |
134 | |
135 | Returns true or false based on whether $value is a valid Dir or File. |
136 | |
137 | =item to_Dir($value), to_File($value) |
fd56ddb6 |
138 | |
dae5e463 |
139 | Attempts to coerce $value to a Dir or File. Returns the coerced value |
4d069f3e |
140 | or false if the coercion failed. |
fd56ddb6 |
141 | |
142 | =back |
143 | |
144 | |
145 | =head1 DEPENDENCIES |
146 | |
dae5e463 |
147 | L<Moose>, L<MooseX::Types>, L<Path::Class> |
fd56ddb6 |
148 | |
149 | |
150 | =head1 BUGS AND LIMITATIONS |
151 | |
192bdd61 |
152 | If you find a bug please either email the author, or add |
52e426e5 |
153 | the bug to cpan-RT L<http://rt.cpan.org>. |
fd56ddb6 |
154 | |
155 | |
156 | =head1 AUTHOR |
157 | |
158 | Todd Hepler C<< <thepler@employees.org> >> |
159 | |
160 | |
161 | =head1 LICENCE AND COPYRIGHT |
162 | |
192bdd61 |
163 | Copyright (c) 2007-2008, Todd Hepler C<< <thepler@employees.org> >>. |
fd56ddb6 |
164 | |
165 | This module is free software; you can redistribute it and/or |
166 | modify it under the same terms as Perl itself. See L<perlartistic>. |
167 | |
168 | |