File Coverage

blib/lib/CPAN/Repository/Role/File.pm
Criterion Covered Total %
statement 15 50 30.0
branch 0 16 0.0
condition n/a
subroutine 5 14 35.7
pod 0 8 0.0
total 20 88 22.7


line stmt bran cond sub pod time code
1             package CPAN::Repository::Role::File;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: Role for file functions
4             $CPAN::Repository::Role::File::VERSION = '0.009';
5 2     2   11518 use Moo::Role;
  2         4  
  2         15  
6              
7 2     2   495 use File::Path qw( make_path );
  2         4  
  2         128  
8 2     2   9 use File::Spec::Functions ':ALL';
  2         2  
  2         324  
9 2     2   972 use IO::Zlib;
  2         93255  
  2         9  
10 2     2   78 use IO::File;
  2         3  
  2         943  
11              
12             requires qw( file_parts generate_content );
13              
14             has repository_root => (
15             is => 'ro',
16             required => 1,
17             );
18              
19             has generate_uncompressed => (
20             is => 'ro',
21             lazy => 1,
22             builder => '_build_generate_uncompressed',
23             );
24              
25 0     0     sub _build_generate_uncompressed { 1 }
26              
27             sub path_inside_root {
28 0     0 0   my ( $self ) = @_;
29 0           return join("/",$self->file_parts);
30             }
31              
32             sub compressed_path_inside_root {
33 0     0 0   my ( $self ) = @_;
34 0           return join("/",$self->file_parts).".gz";
35             }
36              
37             sub full_filename {
38 0     0 0   my ( $self ) = @_;
39 0           return catfile( splitdir($self->repository_root), $self->file_parts );
40             }
41              
42 0     0 0   sub full_compressed_filename { shift->full_filename.".gz" }
43              
44             sub exist {
45 0     0 0   my ( $self ) = @_;
46 0 0         return 0 unless -f $self->full_compressed_filename;
47 0           return 1;
48             }
49              
50             sub save {
51 0     0 0   my ( $self ) = @_;
52 0           my @pps = $self->file_parts;
53 0           pop(@pps);
54 0 0         $self->mkdir( splitdir( $self->repository_root ), @pps ) unless -d catdir( $self->repository_root, @pps );
55 0           my $content = $self->generate_content;
56 0 0         my $gz = IO::Zlib->new($self->full_compressed_filename, "w") or die "cant write to ".$self->full_compressed_filename;
57 0           print $gz $content;
58 0           $gz->close;
59 0 0         if ($self->generate_uncompressed) {
60 0 0         my $txt = IO::File->new($self->full_filename, "w") or die "cant write to ".$self->full_filename;
61 0           print $txt $content;
62 0           $txt->close;
63             }
64 0           return 1;
65             }
66              
67             sub get_file_lines {
68 0     0 0   my ( $self ) = @_;
69 0 0         my $gz = IO::Zlib->new($self->full_compressed_filename, "r") or die "cant read ".$self->full_compressed_filename;
70 0           return <$gz>;
71             }
72              
73             sub mkdir {
74 0     0 0   my ( $self, @path ) = @_;
75 0           make_path(catdir(@path),{ error => \my $err });
76 0 0         if (@$err) {
77 0           for my $diag (@$err) {
78 0           my ($file, $message) = %$diag;
79 0 0         if ($file eq '') {
80 0           die "general error: $message\n";
81             } else {
82 0           die "problem making path $file: $message\n";
83             }
84             }
85             }
86             }
87              
88             1;
89              
90             __END__