File Coverage

lib/FileDirUtil.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 0 1 0.0
total 39 40 97.5


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