| 1 | # -*-CPerl-*- | ||
| 2 | # Last changed Time-stamp: <2017-05-23 00:04:08 mtw> | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | FileDirUtil - A Moose Role for basic File IO | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | package FooBar; | ||
| 11 | use Moose; | ||
| 12 | |||
| 13 | with 'FileDirUtil'; | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | FileDirUtil is a convenience Moose Role for basic File IO, providing | ||
| 18 | transparent access to L<Path::Class::File> and L<Path::Class::Dir> for | ||
| 19 | input files and output directories, respectively, via the following | ||
| 20 | attributes: | ||
| 21 | |||
| 22 | =over 3 | ||
| 23 | |||
| 24 | =item ifile | ||
| 25 | |||
| 26 | A string representing the path to an input file in platform-native | ||
| 27 | syntax, e.g. I<'moo/foo.bar'>. This will be coerced into a | ||
| 28 | L<Path::Class::File> object. | ||
| 29 | |||
| 30 | =item odir | ||
| 31 | |||
| 32 | An ArrayRef specifying path segments of directories which will be | ||
| 33 | joined to create a single L<Path::Class::Dir> directory object. | ||
| 34 | |||
| 35 | =back | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | package FileDirUtil; | ||
| 40 | |||
| 41 | use version; our $VERSION = qv('0.02'); | ||
| 42 | use Moose::Util::TypeConstraints; | ||
| 43 | use Moose::Role; | ||
| 44 | use Path::Class::File; | ||
| 45 | use Path::Class::Dir; | ||
| 46 | use File::Basename; | ||
| 47 | use namespace::autoclean; | ||
| 48 | |||
| 49 | subtype 'MyFile' => as class_type('Path::Class::File'); | ||
| 50 | |||
| 51 | coerce 'MyFile' | ||
| 52 | => from 'Str' | ||
| 53 | => via { Path::Class::File->new($_) }; | ||
| 54 | |||
| 55 | subtype 'MyDir' => as class_type('Path::Class::Dir'); | ||
| 56 | |||
| 57 | coerce 'MyDir' | ||
| 58 | => from 'ArrayRef' | ||
| 59 | => via { Path::Class::Dir->new( @{ $_ } ) }; | ||
| 60 | |||
| 61 | has 'ifile' => ( | ||
| 62 | is => 'ro', | ||
| 63 | isa => 'MyFile', | ||
| 64 | predicate => 'has_ifile', | ||
| 65 | coerce => 1, | ||
| 66 | ); | ||
| 67 | |||
| 68 | has 'ifilebn' => ( | ||
| 69 | is => 'rw', | ||
| 70 | isa => 'Str', | ||
| 71 | predicate => 'has_ifilebn', | ||
| 72 | init_arg => undef, # make this unsettable via constructor | ||
| 73 | ); | ||
| 74 | |||
| 75 | has 'odir' => ( | ||
| 76 | is => 'rw', | ||
| 77 | isa => 'MyDir', | ||
| 78 | predicate => 'has_odir', | ||
| 79 | coerce => 1, | ||
| 80 | ); | ||
| 81 | |||
| 82 | # This should be set automatically inside a BUILD methods, however it | ||
| 83 | # semms this doesnt work well for Roles. Hence do it the ugly way and | ||
| 84 | # call this method manually inside your object... | ||
| 85 | sub set_ifilebn { | ||
| 86 | my $self = shift; | ||
| 87 | $self->ifilebn(fileparse($self->ifile->basename, qr/\.[^.]*/)); | ||
| 88 | }; | ||
| 89 | |||
| 90 | # for perl tests below | ||
| 91 | package FDU; | ||
| 92 | use Moose; | ||
| 93 | with 'FileDirUtil'; | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | __END__ | ||
| 98 | |||
| 99 | |||
| 100 | =head1 SEE ALSO | ||
| 101 | |||
| 102 | =over | ||
| 103 | |||
| 104 | =item L<Path::Class::Dir> | ||
| 105 | |||
| 106 | =item L<Path::Class::File> | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =head1 AUTHOR | ||
| 111 | |||
| 112 | Michael T. Wolfinger, C<< <michael at wolfinger.eu> >> | ||
| 113 | |||
| 114 | =head1 BUGS | ||
| 115 | |||
| 116 | Please report any bugs or feature requests to | ||
| 117 | C<bug-filedirutil at rt.cpan.org>, or through the web | ||
| 118 | interface at | ||
| 119 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FileDirUtil>. | ||
| 120 | I will be notified, and then you'll automatically be notified of | ||
| 121 | progress on your bug as I make changes. | ||
| 122 | |||
| 123 | |||
| 124 | =head1 SUPPORT | ||
| 125 | |||
| 126 | You can find documentation for this module with the perldoc command. | ||
| 127 | |||
| 128 | perldoc FileDirUtil | ||
| 129 | |||
| 130 | |||
| 131 | You can also look for information at: | ||
| 132 | |||
| 133 | =over 4 | ||
| 134 | |||
| 135 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 136 | |||
| 137 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FileDirUtil> | ||
| 138 | |||
| 139 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 140 | |||
| 141 | L<http://annocpan.org/dist/FileDirUtil> | ||
| 142 | |||
| 143 | =item * CPAN Ratings | ||
| 144 | |||
| 145 | L<http://cpanratings.perl.org/d/FileDirUtil> | ||
| 146 | |||
| 147 | =item * Search CPAN | ||
| 148 | |||
| 149 | L<http://search.cpan.org/dist/FileDirUtil/> | ||
| 150 | |||
| 151 | =back | ||
| 152 | |||
| 153 | =head1 LICENSE AND COPYRIGHT | ||
| 154 | |||
| 155 | Copyright 2017 Michael T. Wolfinger <michael@wolfinger.eu> and <michael.wolfinger@univie.ac.at> | ||
| 156 | |||
| 157 | This program is free software; you can redistribute it and/or modify | ||
| 158 | it under the terms of the GNU Affero General Public License as | ||
| 159 | published by the Free Software Foundation; either version 3 of the | ||
| 160 | License, or (at your option) any later version. | ||
| 161 | |||
| 162 | This program is distributed in the hope that it will be useful, but | ||
| 163 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 164 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 165 | Affero General Public License for more details. | ||
| 166 | |||
| 167 | You should have received a copy of the GNU Affero General Public | ||
| 168 | License along with this program. If not, see | ||
| 169 | L<http://www.gnu.org/licenses/>. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | 1; |