File Coverage

blib/lib/Code/TidyAll/CacheModel.pm
Criterion Covered Total %
statement 50 54 92.5
branch 4 6 66.6
condition 3 3 100.0
subroutine 16 17 94.1
pod 2 2 100.0
total 75 82 91.4


line stmt bran cond sub pod time code
1              
2             use strict;
3 27     27   599 use warnings;
  27         50  
  27         743  
4 27     27   118  
  27         60  
  27         721  
5             use Digest::SHA qw(sha1_hex);
6 27     27   555 use Path::Tiny ();
  27         2661  
  27         1291  
7 27     27   838 use Specio::Declare;
  27         10074  
  27         499  
8 27     27   560 use Specio::Library::Builtins;
  27         58559  
  27         259  
9 27     27   6175 use Specio::Library::Path::Tiny;
  27         20946  
  27         271  
10 27     27   217552 use Specio::Library::String;
  27         31921  
  27         208  
11 27     27   324095  
  27         252261  
  27         190  
12             use Moo;
13 27     27   49101  
  27         69  
  27         189  
14             our $VERSION = '0.82';
15              
16             has base_sig => (
17             is => 'ro',
18             isa => t('Str'),
19             default => q{},
20             );
21              
22             has cache_engine => (
23             is => 'ro',
24             isa => object_can_type(
25             methods => [qw( get set remove )],
26             ),
27             predicate => '_has_cache_engine',
28             );
29              
30             has cache_key => (
31             is => 'lazy',
32             isa => t('NonEmptyStr'),
33             clearer => 1,
34             );
35              
36             has cache_value => (
37             is => 'lazy',
38             isa => t('NonEmptyStr'),
39             clearer => 1,
40             );
41              
42             has file_contents => (
43             is => 'rw',
44             isa => t('Str'),
45             lazy => 1,
46             builder => 1,
47             trigger => 1,
48             clearer => 1,
49             );
50              
51             has full_path => (
52             is => 'ro',
53             isa => t('Path'),
54             required => 1,
55             );
56              
57             has is_cached => (
58             is => 'rw',
59             isa => t('Bool'),
60             lazy => 1,
61             builder => 1,
62             clearer => 1,
63             );
64              
65             has path => (
66             is => 'ro',
67             isa => t('Path'),
68             required => 1,
69             );
70              
71             my ($self) = @_;
72             return Path::Tiny::path( $self->full_path )->slurp_raw;
73 66     66   680 }
74 66         244  
75             my $self = shift;
76             $self->clear_cache_key;
77             $self->clear_is_cached;
78 40     40   1562 $self->clear_cache_value;
79 40         695 return;
80 40         881 }
81 40         714  
82 40         337 my ($self) = @_;
83             return 'sig/' . $self->path;
84             }
85              
86 82     82   655 my ($self) = @_;
87 82         396  
88             # this stat isn't ideal, but it'll do
89             my $last_mod = ( stat( $self->full_path ) )[9];
90             return $self->_sig( [ $self->base_sig, $last_mod, $self->file_contents ] );
91 35     35   1842 }
92              
93             my ($self) = @_;
94 35         175  
95 35         1296 return unless $self->_has_cache_engine;
96             my $cached_value = $self->cache_engine->get( $self->cache_key );
97             return defined $cached_value && $cached_value eq $self->cache_value;
98             }
99 66     66   616  
100             my ($self) = @_;
101 66 100       398  
102 50         802 return unless $self->_has_cache_engine;
103 50   100     2265 $self->cache_engine->set( $self->cache_key, $self->cache_value );
104             $self->is_cached(1);
105             return;
106             }
107 53     53 1 137  
108             my ($self) = @_;
109 53 100       153  
110 37         594 return unless $self->_has_cache_engine;
111 37         960 $self->cache_engine->remove( $self->cache_key );
112 37         1401 return;
113             }
114              
115             my ( $self, $data ) = @_;
116 0     0 1 0 return sha1_hex( join( ',', @$data ) );
117             }
118 0 0       0  
119 0         0 1;
120 0         0  
121             # ABSTRACT: Caching model for Code::TidyAll
122              
123              
124 47     47   2974 =pod
125 47         970  
126             =encoding UTF-8
127              
128             =head1 NAME
129              
130             Code::TidyAll::CacheModel - Caching model for Code::TidyAll
131              
132             =head1 VERSION
133              
134             version 0.82
135              
136             =head1 SYNOPSIS
137              
138             my $cache_model = Cody::TidyAll::CacheModel->new(
139             cache_engine => Code::TidyAll::Cache->new(...),
140             path => '/path/to/file/to/cache',
141             );
142              
143             # check cache
144             print 'Yes!' if $cache_model->is_cached;
145              
146             # update cache
147             $cache_model->clear_file_contents;
148             $cache_model->update;
149              
150             # update the cache when you know the file contents
151             $cache_model->file_contents($new_content);
152             $cache_model->update;
153              
154             # force removal from cache
155             $cache_model->remove;
156              
157             =head1 DESCRIPTION
158              
159             A cache model for Code::TidyAll. Different subclasses can employ different
160             caching techniques.
161              
162             The basic model implemented here is simple; It stores a hash key of the file
163             contents keyed by a hash key of the file's path.
164              
165             =head1 METHODS
166              
167             This class has the following methods:
168              
169             =head2 Code::TidyAll::CacheModel->new(%params)
170              
171             The constructor accepts the following attributes:
172              
173             =over 4
174              
175             =item * full_path
176              
177             The full path to the cache file on disk. This is required.
178              
179             =item * path
180              
181             The local path to the file (i.e. what the cache system will consider the
182             canonical name of the file).
183              
184             =item * cache_engine
185              
186             A C<Code::TidyAll::Cache> compatible instance. This can be omitted if no
187             caching is actually being done.
188              
189             =item * base_sig
190              
191             A base signature. This defaults to an empty string.
192              
193             =item * file_contents
194              
195             The contents of the file being cached. This can be omitted, in which case it
196             will be loaded as needed.
197              
198             =item * is_cached
199              
200             A boolean indicating if this file is cached. By default this is computed by
201             checking that the cache key and cache value match what is in the cache.
202              
203             =back
204              
205             =head2 $model->full_path
206              
207             The value passed to the constructor.
208              
209             =head2 $model->path
210              
211             The value passed to the constructor.
212              
213             =head2 $model->cache_engine
214              
215             The value passed to the constructor.
216              
217             =head2 $model->base_sig
218              
219             The value passed to the constructor or the default value, an empty string.
220              
221             =head2 $model->file_contents
222              
223             The file contents, which will be loaded from the file system if needed.
224              
225             =head2 $model->is_cached
226              
227             A boolean indicating whether the path is currently in the cache.
228              
229             =head2 $model->cache_key
230              
231             The computed cache key for the file.
232              
233             =head2 $model->cache_value
234              
235             The computed cache value for the file.
236              
237             =head2 $model->update
238              
239             Updates the cache.
240              
241             =head2 $model->remove
242              
243             Attempts to remove the value from the cache.
244              
245             =head1 SUPPORT
246              
247             Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
248              
249             =head1 SOURCE
250              
251             The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.
252              
253             =head1 AUTHORS
254              
255             =over 4
256              
257             =item *
258              
259             Jonathan Swartz <swartz@pobox.com>
260              
261             =item *
262              
263             Dave Rolsky <autarch@urth.org>
264              
265             =back
266              
267             =head1 COPYRIGHT AND LICENSE
268              
269             This software is copyright (c) 2011 - 2022 by Jonathan Swartz.
270              
271             This is free software; you can redistribute it and/or modify it under
272             the same terms as the Perl 5 programming language system itself.
273              
274             The full text of the license can be found in the
275             F<LICENSE> file included with this distribution.
276              
277             =cut