File Coverage

blib/lib/CPAN/Index/API/Role/Writable.pm
Criterion Covered Total %
statement 59 68 86.7
branch 4 16 25.0
condition 0 3 0.0
subroutine 18 20 90.0
pod 4 4 100.0
total 85 111 76.5


line stmt bran cond sub pod time code
1             package CPAN::Index::API::Role::Writable;
2             {
3             $CPAN::Index::API::Role::Writable::VERSION = '0.007';
4             }
5              
6             # ABSTRACT: Writes index files
7              
8 3     3   3026 use strict;
  3         6  
  3         145  
9 3     3   32 use warnings;
  3         5  
  3         113  
10              
11 3     3   15 use File::Slurp qw(write_file read_file);
  3         4  
  3         218  
12 3     3   24 use File::Basename qw(fileparse);
  3         4  
  3         242  
13 3     3   686 use Path::Class qw(file dir);
  3         25679  
  3         177  
14 3     3   3234 use Text::Template qw(fill_in_string);
  3         11973  
  3         232  
15 3     3   27 use Symbol qw(qualify_to_ref);
  3         7  
  3         217  
16 3     3   19 use Scalar::Util qw(blessed);
  3         7  
  3         152  
17 3     3   16 use Carp qw(croak);
  3         7  
  3         158  
18 3     3   2305 use Compress::Zlib qw(gzopen $gzerrno);
  3         155463  
  3         513  
19 3     3   28 use namespace::autoclean;
  3         6  
  3         31  
20 3     3   4406 use Moose::Role;
  3         22812  
  3         19  
21              
22             requires 'default_location';
23              
24             has tarball_is_default => (
25             is => 'ro',
26             isa => 'Bool',
27             lazy_build => 1,
28             );
29              
30             has repo_path => (
31             is => 'ro',
32             isa => 'Str',
33             );
34              
35             has template => (
36             is => 'ro',
37             isa => 'Str',
38             required => 1,
39             lazy_build => 1,
40             );
41              
42             has content => (
43             is => 'ro',
44             isa => 'Str',
45             required => 1,
46             lazy_build => 1,
47             );
48              
49             sub _build_template {
50 6     6   14 my $self = shift;
51 6         58 my $glob = qualify_to_ref("DATA", blessed $self);
52 6         189 return read_file($glob);
53             }
54              
55             sub _build_content {
56 7     7   34 my $self = shift;
57 7         281 my $content = fill_in_string(
58             $self->template,
59             DELIMITERS => [ '[%', '%]' ],
60             HASH => { self => \$self },
61             );
62 7         533 chomp $content;
63 7         263 return $content;
64             }
65              
66             sub _build_tarball_is_default {
67 0     0   0 my $self = shift;
68 0 0       0 return $self->default_location =~ /\.gz$/ ? 1 : 0;
69             }
70              
71             sub rebuild_content {
72 1     1 1 3 my $self = shift;
73 1         10 my $meta = (blessed $self)->meta;
74 1         83 $meta->get_attribute('content')->set_value($self, $self->_build_content);
75             }
76              
77             sub write_to_tarball {
78 1     1 1 2553 my ($self, $filename) = @_;
79 1         6 my $file = $self->_prepare_file($filename, 1);
80 1 50       5 my $gz = gzopen($file->stringify, 'wb') or croak "Cannot open $file: $gzerrno";
81 1         1822 $gz->gzwrite($self->content);
82 1 50       164 $gz->gzclose and croak "Error closing $file";
83             }
84              
85             sub write_to_file {
86 5     5 1 5434 my ($self, $filename) = @_;
87 5         22 my $file = $self->_prepare_file($filename);
88 5         286 write_file($file, { err_mode => 'carp' }, $self->content);
89             }
90              
91             sub write_to_default_location {
92 0     0 1 0 my ($self) = @_;
93 0 0       0 $self->tarball_is_default
94             ? $self->write_to_tarball
95             : $self->write_to_file;
96             }
97              
98             sub _prepare_file {
99 6     6   11 my ( $self, $file, $is_tarball ) = @_;
100              
101 6 50 0     21 if ( defined $file ) {
    0          
102 6         24 $file = file($file);
103             } elsif ( not defined $file and $self->repo_path ) {
104 0         0 my $location = $self->default_location;
105              
106             # first normalize the extension
107 0         0 $location =~ s/\.gz$//;
108             # then make sure we have it if we need a tarball
109 0 0       0 $location .= '.gz' if $is_tarball;
110              
111 0         0 $file = file( $self->repo_path, $location);
112             } else {
113 0         0 croak "Unable to write to file without a filename or repo path";
114             }
115              
116 6 50       744 $file->dir->mkpath unless -e $file->dir;
117              
118 6         272 return $file;
119             }
120              
121             1;
122              
123              
124             __END__
125             =pod
126              
127             =head1 NAME
128              
129             CPAN::Index::API::Role::Writable - Writes index files
130              
131             =head1 VERSION
132              
133             version 0.007
134              
135             =head1 DESCRIPTION
136              
137             This role provides attributes and methods shared between classes that write
138             index files.
139              
140             =head1 REQUIRES
141              
142             =head2 default_location
143              
144             Class method that returns a string specifying the path to the default location
145             of this file relative to the repository root.
146              
147             =head2 C<__DATA__>
148              
149             Consuming packages are expected to have a C<DATA> section that contains the
150             template to use for generating the file contents.
151              
152             =head1 PROVIDES
153              
154             =head2 tarball_is_default
155              
156             Required attribute. Boolean - indicates whether the file should be compressed
157             by default. Automatically set to true if the file path in C<default_location>
158             ends in C<.gz>.
159              
160             =head2 repo_path
161              
162             Optional attribute. Path to the repository root.
163              
164             =head2 template
165              
166             Optional attribute. The template to use for generating the index files. The
167             defalt is fetched from the C<DATA> section of the consuming package.
168              
169             =head2 content
170              
171             Optional attribute. The index file content. Built by default from the
172             provided L</template>.
173              
174             =head2 rebuild_content
175              
176             C<content> is a lazy read-only attribute which normally is built only once.
177             Use C<rebuild_content> to generate a new value for C<content> if you've made
178             changes to the list of packages.
179              
180             =head2 write_to_file
181              
182             This method builds the file content if necessary and writes it to a file. A
183             path to a file to write to can be passed as an argument, otherwise the default
184             location will be used (a C<.gz> suffix, if it exists, will be removed).
185              
186             =head2 write_to_tarball
187              
188             This method builds the file content if necessary and writes it to a tarball. A
189             path to a file to write to can be passed as an argument, otherwise the default
190             location will be used.
191              
192             =head2 write_to_default_location
193              
194             This method builds the file content if necessary and writes it to the default
195             location.
196              
197             =head1 AUTHOR
198              
199             Peter Shangov <pshangov@yahoo.com>
200              
201             =head1 COPYRIGHT AND LICENSE
202              
203             This software is copyright (c) 2012 by Venda, Inc..
204              
205             This is free software; you can redistribute it and/or modify it under
206             the same terms as the Perl 5 programming language system itself.
207              
208             =cut
209