| 1 | # -*-CPerl-*- | ||||||||||
| 2 | # Last changed Time-stamp: <2019-08-23 20:19: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 | ▶ | use version; our $VERSION = qv('0.04'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 1 | ▶ | use Path::Class::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 1 | ▶ | use Path::Class::Dir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 1 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 1 | ▶ | use Params::Coerce (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | |||||||||||
| 56 | 1 | subtype 'MyFile' => as class_type('Path::Class::File'); | |||||||||
| 57 | |||||||||||
| 58 | coerce 'MyFile' | ||||||||||
| 59 | => from 'Str' | ||||||||||
| 60 | 2 | ▶ | => via { Path::Class::File->new($_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | 1 | subtype 'MyDir' => as class_type('Path::Class::Dir'); | |||||||||
| 63 | |||||||||||
| 64 | coerce 'MyDir' | ||||||||||
| 65 | => from 'Object' | ||||||||||
| 66 | 0 | ▶ | => via {$_ -> isa('Path::Class::Dir') ? $_ : Params::Coerce::coerce ('Path::Class::Dir', $_); } | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 67 | => from 'ArrayRef' | ||||||||||
| 68 | 1 | ▶ | => via { Path::Class::Dir->new( @{ $_ } ) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 69 | |||||||||||
| 70 | 1 | has 'ifile' => ( | |||||||||
| 71 | is => 'ro', | ||||||||||
| 72 | isa => 'MyFile', | ||||||||||
| 73 | predicate => 'has_ifile', | ||||||||||
| 74 | coerce => 1, | ||||||||||
| 75 | ); | ||||||||||
| 76 | |||||||||||
| 77 | 1 | has 'ifilebn' => ( | |||||||||
| 78 | is => 'rw', | ||||||||||
| 79 | isa => 'Str', | ||||||||||
| 80 | predicate => 'has_ifilebn', | ||||||||||
| 81 | init_arg => undef, # make this unsettable via constructor | ||||||||||
| 82 | ); | ||||||||||
| 83 | |||||||||||
| 84 | 1 | 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 | 2 | ▶ | sub set_ifilebn { | ||||||||
|
Subroutine
set_ifilebn: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 95 | 2 | my $self = shift; | |||||||||
| 96 | 2 | $self->ifilebn(fileparse($self->ifile->basename, qr/\.[^.]*/)); | |||||||||
| 97 | }; | ||||||||||
| 98 | |||||||||||
| 99 | # for perl tests below | ||||||||||
| 100 | package FDU; | ||||||||||
| 101 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 102 | 1 | with 'FileDirUtil'; | |||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | __END__ | ||||||||||