File Coverage

blib/lib/Dist/Zilla/Tempdir/Item/State.pm
Criterion Covered Total %
statement 61 64 95.3
branch 6 12 50.0
condition n/a
subroutine 18 18 100.0
pod 4 4 100.0
total 89 98 90.8


line stmt bran cond sub pod time code
1 6     6   478 use 5.006; # 06 -> [pragmas, our]
  6         14  
2 6     6   19 use strict;
  6         8  
  6         109  
3 6     6   17 use warnings;
  6         6  
  6         339  
4              
5             package Dist::Zilla::Tempdir::Item::State;
6              
7             our $VERSION = '1.001003';
8              
9             # ABSTRACT: Intermediate state for a file
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 6     6   511 use Moose qw( has );
  6         288806  
  6         31  
14 6     6   19338 use Carp qw( croak );
  6         9  
  6         250  
15 6     6   1478 use Path::Tiny qw( path );
  6         15250  
  6         2895  
16              
17              
18              
19              
20              
21              
22              
23             has 'hash' => ( is => ro =>, lazy_build => 1 );
24              
25              
26              
27              
28              
29              
30              
31             has 'file' => (
32             is => ro =>,
33             required => 1,
34             handles => { name => name => },
35             );
36              
37              
38              
39              
40              
41              
42              
43             has 'new_content' => ( is => ro =>, lazy_build => 1 );
44              
45              
46              
47              
48              
49              
50              
51             has 'new_hash' => ( is => ro =>, lazy_build => 1 );
52              
53              
54              
55              
56              
57              
58              
59             has 'storage_prefix' => ( is => ro =>, required => 1 );
60              
61             has '_digester' => ( is => ro =>, lazy_build => 1 );
62              
63              
64              
65              
66              
67              
68              
69             sub BUILD {
70 14     14 1 16 my ($self) = @_;
71 14         435 $self->hash;
72 14         377 return;
73             }
74              
75             sub _build__digester {
76 14     14   1642 require Digest::SHA;
77             ## no critic ( ProhibitMagicNumbers )
78 14         7172 return Digest::SHA->new(512);
79             }
80              
81             sub _digest_for {
82 26     26   38 my ( $self, $content ) = @_;
83 26 50       56 if ( not defined $content ) {
84 0         0 return croak('->_digest_for( content ) must have a defined value of content');
85             }
86 26         778 $self->_digester->reset();
87 26         926 $self->_digester->add($content);
88 26         708 return $self->_digester->b64digest;
89             }
90              
91             sub _build_hash {
92 14     14   15 my ($self) = @_;
93 14         36 return $self->_digest_for( $self->_encoded_content );
94             }
95              
96             sub _build_new_content {
97 12     12   14 my ($self) = @_;
98 12 50       24 return unless $self->on_disk;
99 12         196 return $self->_relpath->slurp_raw();
100             }
101              
102             sub _build_new_hash {
103 12     12   13 my ($self) = @_;
104 12 50       24 return unless $self->on_disk;
105 12         585 return $self->_digest_for( $self->new_content );
106             }
107              
108             sub _encoded_content {
109 28     28   29 my ($self) = @_;
110 28         29 my $content;
111 28         36 my $method = 'content';
112 28 50       932 if ( $self->file->can('encoded_content') ) {
113 28         37 $method = 'encoded_content';
114 28         755 $content = $self->file->encoded_content;
115             }
116             else {
117 0         0 $content = $self->file->content;
118             }
119 28 50       3418 if ( not defined $content ) {
120 0         0 croak( $self->file . " returned undef for $method" );
121             }
122 28         88 return $content;
123             }
124              
125             sub _relpath {
126 76     76   67 my ($self) = @_;
127 76         2322 my $d = path( $self->storage_prefix );
128 76         4396 my $out_path = $d->child( $self->file->name );
129 76         4297 return $out_path;
130             }
131              
132              
133              
134              
135              
136              
137              
138             sub write_out {
139 14     14 1 17 my ($self) = @_;
140 14         24 my $out_path = $self->_relpath();
141 14         54 $out_path->parent->mkpath(1);
142 14         1404 $out_path->spew_raw( $self->_encoded_content );
143 14         4063 return;
144             }
145              
146              
147              
148              
149              
150              
151              
152             sub on_disk {
153 50     50 1 47 my ($self) = @_;
154 50         67 my $out_path = $self->_relpath();
155 50         142 return -e $out_path;
156             }
157              
158              
159              
160              
161              
162              
163              
164              
165             sub on_disk_changed {
166 12     12 1 362 my ($self) = @_;
167 12 50       35 return unless $self->on_disk;
168 12         589 return $self->hash ne $self->new_hash;
169             }
170              
171 6     6   32 no Moose;
  6         7  
  6         37  
172             __PACKAGE__->meta->make_immutable;
173             1;
174              
175             __END__
176              
177             =pod
178              
179             =encoding UTF-8
180              
181             =head1 NAME
182              
183             Dist::Zilla::Tempdir::Item::State - Intermediate state for a file
184              
185             =head1 VERSION
186              
187             version 1.001003
188              
189             =head1 ATTRIBUTES
190              
191             =head2 C<hash>
192              
193             Provides a digest hash of C<file>'s content
194              
195             =head2 C<file>
196              
197             A C<Dist::Zilla::File>
198              
199             =head2 C<new_content>
200              
201             Content of C<storage_prefix>/C<file> read from disk.
202              
203             =head2 C<new_hash>
204              
205             Hash of C<new_content>
206              
207             =head2 C<storage_prefix>
208              
209             The root directory to write this file out to, and to read it from.
210              
211             =head1 METHODS
212              
213             =head2 C<BUILD>
214              
215             Ensures C<hash> is populated at build time.
216              
217             =head2 C<write_out>
218              
219             Emits C<file> into C<storage_prefix>
220              
221             =head2 C<on_disk>
222              
223             Returns true if C<file> exists in C<storage_prefix>
224              
225             =head2 C<on_disk_changed>
226              
227             Returns true if the file is on disk, and the on-disk hash
228             doesn't match the written out C<file>'s hash.
229              
230             =head1 AUTHOR
231              
232             Kent Fredric <kentnl@cpan.org>
233              
234             =head1 COPYRIGHT AND LICENSE
235              
236             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
237              
238             This is free software; you can redistribute it and/or modify it under
239             the same terms as the Perl 5 programming language system itself.
240              
241             =cut