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