File Coverage

lib/Dist/Zilla/Tempdir/Item/State.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


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