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