1 package MooseX::Types::Path::Class;
3 use warnings FATAL => 'all';
7 our $AUTHORITY = 'cpan:THEPLER';
10 # TODO: export dir() and file() from Path::Class? (maybe)
13 -declare => [qw( Dir File ExistingDir ExistingFile )];
15 use MooseX::Types::Moose qw(Str ArrayRef);
17 class_type('Path::Class::Dir');
18 class_type('Path::Class::File');
20 subtype Dir, as 'Path::Class::Dir';
21 subtype File, as 'Path::Class::File';
23 subtype ExistingFile, as File, where { -e $_->stringify },
24 message { "File '$_' must exist." };
26 subtype ExistingDir, as Dir, where { -e $_->stringify && -d $_->stringify },
27 message { "Directory '$_' must exist" };
29 for my $type ( 'Path::Class::Dir', Dir, ExistingDir ) {
31 from Str, via { Path::Class::Dir->new($_) },
32 from ArrayRef, via { Path::Class::Dir->new(@$_) };
35 for my $type ( 'Path::Class::File', File, ExistingFile ) {
37 from Str, via { Path::Class::File->new($_) },
38 from ArrayRef, via { Path::Class::File->new(@$_) };
41 # optionally add Getopt option type
42 eval { require MooseX::Getopt; };
44 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', )
45 for ( 'Path::Class::Dir', 'Path::Class::File', Dir, File, ExistingDir, ExistingFile );
54 MooseX::Types::Path::Class - A Path::Class type library for Moose
61 use MooseX::Types::Path::Class;
62 with 'MooseX::Getopt'; # optional
66 isa => 'Path::Class::Dir',
73 isa => 'Path::Class::File',
78 # these attributes are coerced to the
79 # appropriate Path::Class objects
80 MyClass->new( dir => '/some/directory/', file => '/some/file' );
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.
89 Coercions (see L<Moose::Util::TypeConstraints>) are made
90 from both 'Str' and 'ArrayRef' to both L<Path::Class::Dir> and
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>.
98 None of these are exported by default. They are provided via
105 These exports can be used instead of the full class names. Example:
109 use MooseX::Types::Path::Class qw(Dir File);
125 =item ExistingDir, ExistingFile
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).
131 Note that there are no quotes around Dir or File.
133 =item is_Dir($value), is_File($value)
135 Returns true or false based on whether $value is a valid Dir or File.
137 =item to_Dir($value), to_File($value)
139 Attempts to coerce $value to a Dir or File. Returns the coerced value
140 or false if the coercion failed.
147 L<Moose>, L<MooseX::Types>, L<Path::Class>
150 =head1 BUGS AND LIMITATIONS
152 If you find a bug please either email the author, or add
153 the bug to cpan-RT L<http://rt.cpan.org>.
158 Todd Hepler C<< <thepler@employees.org> >>
161 =head1 LICENCE AND COPYRIGHT
163 Copyright (c) 2007-2008, Todd Hepler C<< <thepler@employees.org> >>.
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>.