API/Role/Writable.pm

filter: Statementstmt 87.2% 75 / 86 Branchbran 30.0% 6 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.0% 18 / 20 Podpod 100% 4 / 4 totaltotal 74.6% 103 / 138 CC 13 SCAR 16.4
CC
13
Cov
73%
_prepare_file
16.7
_build_tarball_is_default
6.0
write_to_default_location
6.0
CRAP 5.2
« API/File/Whois.pm ↑ Summary API/File/ModList.pm »
1 package CPAN::Index::API::Role::Writable;
2  
3 3 our $VERSION = '0.008';
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Path::Class qw(file dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Text::Template qw(fill_in_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Symbol qw(qualify_to_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Compress::Zlib qw(gzopen $gzerrno);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 requires 'default_location';
20  
21 3 has tarball_is_default => (
22 is => 'ro',
23 isa => 'Bool',
24 lazy_build => 1,
25 );
26  
27 3 has repo_path => (
28 is => 'ro',
29 isa => 'Str',
30 );
31  
32 3 has template => (
33 is => 'ro',
34 isa => 'Str',
35 required => 1,
36 lazy_build => 1,
37 );
38  
39 3 has content => (
40 is => 'ro',
41 isa => 'Str',
42 required => 1,
43 lazy_build => 1,
44 );
45  
46 6 ▶sub _build_template {
Subroutine _build_template: called CC=4 SCAR=14.8
47 6 my $self = shift;
48 6 my $data;
49 my $error;
50 { # catch block
51 6 local $@;
52 6 ▶ $error = $@ || 'Error' unless eval { # try block
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { my $data_glob = &qualify_to_ref("DATA", scalar builtin::blessed($self)); my $tell = tell $data_glob; local $/; $data = readline $data_glob; seek $data_glob, $tell, 0; 1 } } 0 6 6
53 6 my $data_glob = qualify_to_ref("DATA", blessed $self);
54 # find the current location before reading
55 6 my $tell = tell($data_glob);
56 6 local $/;
57 6 $data = <$data_glob>;
58 # put the location back so we can read again
59 # SEEK_SET is 0
60 6 seek($data_glob, $tell, 0);
61 6 ▶ 1;
Condition0%0/2
$@ || 'Error'
l!l
00
MC/DC0%0/1
$@ || 'Error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
62 };
63 }
64 6 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 6 6
65 0 warn $error;
66 0 return;
67 }
68 6 return $data;
69 }
70  
71 7 ▶sub _build_content {
Subroutine _build_content: called CC=1 SCAR=0.0
72 7 my $self = shift;
73 7 my $content = fill_in_string(
74 $self->template,
75 DELIMITERS => [ '[%', '%]' ],
76 HASH => { self => \$self },
77 );
78 7 chomp $content;
79 7 return $content;
80 }
81  
82 0 ▶sub _build_tarball_is_default {
Subroutine _build_tarball_is_default: not called CC=2 SCAR=17.9
83 0 my $self = shift;
84 0 ▶ return $self->default_location =~ /\.gz$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->default_location =~ /\.gz$/ ? : 0 0 0
85 }
86  
87 1 ▶sub rebuild_content {
Subroutine rebuild_content: called CC=1 SCAR=0.0
Pod documented
88 1 my $self = shift;
89 1 my $meta = (blessed $self)->meta;
90 1 $meta->get_attribute('content')->set_value($self, $self->_build_content);
91 }
92  
93 1 ▶sub write_to_tarball {
Subroutine write_to_tarball: called CC=3 SCAR=11.3
Pod documented
94 1 my ($self, $filename) = @_;
95 1 my $file = $self->_prepare_file($filename, 1);
96 1 ▶ my $gz = gzopen($file->stringify, 'wb') or croak "Cannot open $file: $gzerrno";
Branch: 1 branch
BranchTrueFalseTotal
my $gz = &gzopen(scalar $file->stringify, "wb") or croak("Cannot open ${file}: $CPAN::Index::API::Role::Writable::gzerrno") 1 0 1
97 1 $gz->gzwrite($self->content);
98 1 ▶ $gz->gzclose and croak "Error closing $file";
Branch: 1 branch
BranchTrueFalseTotal
$gz->gzclose and croak("Error closing $file") 0 1 1
99 }
100  
101 5 ▶sub write_to_file {
Subroutine write_to_file: called CC=1 SCAR=0.0
Pod documented
102 5 my ($self, $filename) = @_;
103 5 path($self->_prepare_file($filename))->spew_utf8($self->content);
104 }
105  
106 0 ▶sub write_to_default_location {
Subroutine write_to_default_location: not called CC=2 SCAR=17.9
Pod documented
107 0 my ($self) = @_;
108 0 ▶ $self->tarball_is_default
Branch: 1 branch
BranchTrueFalseTotal
$self->tarball_is_default ? : 0 0 0
109 ? $self->write_to_tarball
110 : $self->write_to_file;
111 }
112  
113 6 ▶sub _prepare_file {
Subroutine _prepare_file: called CC=6 SCAR=28.1
114 6 my ( $self, $file, $is_tarball ) = @_;
115  
116 6 ▶ if ( defined $file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $file) { } 6 0 6
elsif (not defined $file and $self->repo_path) { } 0 0 0
117 6 $file = file($file);
118 ▶ } elsif ( not defined $file and $self->repo_path ) {
Condition0%0/3
not defined $file and $self->repo_path
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $file and $self->repo_path
not defined $file $self->repo_path
Truth table0%0/3
not(A) and B
A= not defined $fileB= $self->repo_path
ABresult
0X0
100
111
119 0 my $location = $self->default_location;
120  
121 # first normalize the extension
122 0 $location =~ s/\.gz$//;
123 # then make sure we have it if we need a tarball
124 0 ▶ $location .= '.gz' if $is_tarball;
Branch: 1 branch
BranchTrueFalseTotal
if $is_tarball 0 0 0
125  
126 0 $file = file( $self->repo_path, $location);
127 } else {
128 0 croak "Unable to write to file without a filename or repo path";
129 }
130  
131 6 ▶ $file->dir->mkpath unless -e $file->dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file->dir 0 6 6
132  
133 6 return $file;
134 }
135  
136 3 1;
137  
138 =pod
139  
140 =encoding UTF-8
141  
142 =head1 NAME
143  
144 CPAN::Index::Role::Writable - Writes index files
145  
146 =head1 DESCRIPTION
147  
148 This role provides attributes and methods shared between classes that write
149 index files.
150  
151 =head1 REQUIRES
152  
153 =head2 default_location
154  
155 Class method that returns a string specifying the path to the default location
156 of this file relative to the repository root.
157  
158 =head2 C<__DATA__>
159  
160 Consuming packages are expected to have a C<DATA> section that contains the
161 template to use for generating the file contents.
162  
163 =head1 PROVIDES
164  
165 =head2 tarball_is_default
166  
167 Required attribute. Boolean - indicates whether the file should be compressed
168 by default. Automatically set to true if the file path in C<default_location>
169 ends in C<.gz>.
170  
171 =head2 repo_path
172  
173 Optional attribute. Path to the repository root.
174  
175 =head2 template
176  
177 Optional attribute. The template to use for generating the index files. The
178 default is fetched from the C<DATA> section of the consuming package.
179  
180 =head2 content
181  
182 Optional attribute. The index file content. Built by default from the
183 provided L</template>.
184  
185 =head2 rebuild_content
186  
187 C<content> is a lazy read-only attribute which normally is built only once.
188 Use C<rebuild_content> to generate a new value for C<content> if you've made
189 changes to the list of packages.
190  
191 =head2 write_to_file
192  
193 This method builds the file content if necessary and writes it to a file. A
194 path to a file to write to can be passed as an argument, otherwise the default
195 location will be used (a C<.gz> suffix, if it exists, will be removed).
196  
197 =head2 write_to_tarball
198  
199 This method builds the file content if necessary and writes it to a tarball. A
200 path to a file to write to can be passed as an argument, otherwise the default
201 location will be used.
202  
203 =head2 write_to_default_location
204  
205 This method builds the file content if necessary and writes it to the default
206 location.
207  
208 =cut
« API/File/Whois.pm ↑ Summary API/File/ModList.pm »