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