| 1 | package Code::TidyAll::CacheModel; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 58 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 58 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 58 | ▶ | use Digest::SHA qw(sha1_hex); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 58 | ▶ | use Path::Tiny (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 58 | ▶ | use Specio::Declare; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 58 | ▶ | use Specio::Library::Builtins; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 58 | ▶ | use Specio::Library::Path::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 58 | ▶ | use Specio::Library::String; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 58 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 58 | our $VERSION = '0.85'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 58 | has base_sig => ( | |||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||
| 19 | isa => t('Str'), | ||||||||||||||||||||
| 20 | default => q{}, | ||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 58 | has cache_engine => ( | |||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||
| 25 | isa => object_can_type( | ||||||||||||||||||||
| 26 | methods => [qw( get set remove )], | ||||||||||||||||||||
| 27 | ), | ||||||||||||||||||||
| 28 | predicate => '_has_cache_engine', | ||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 58 | has cache_key => ( | |||||||||||||||||||
| 32 | is => 'lazy', | ||||||||||||||||||||
| 33 | isa => t('NonEmptyStr'), | ||||||||||||||||||||
| 34 | clearer => 1, | ||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 58 | has cache_value => ( | |||||||||||||||||||
| 38 | is => 'lazy', | ||||||||||||||||||||
| 39 | isa => t('NonEmptyStr'), | ||||||||||||||||||||
| 40 | clearer => 1, | ||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 58 | has file_contents => ( | |||||||||||||||||||
| 44 | is => 'rw', | ||||||||||||||||||||
| 45 | isa => t('Str'), | ||||||||||||||||||||
| 46 | lazy => 1, | ||||||||||||||||||||
| 47 | builder => 1, | ||||||||||||||||||||
| 48 | trigger => 1, | ||||||||||||||||||||
| 49 | clearer => 1, | ||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 58 | has full_path => ( | |||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||
| 54 | isa => t('Path'), | ||||||||||||||||||||
| 55 | required => 1, | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 58 | has is_cached => ( | |||||||||||||||||||
| 59 | is => 'rw', | ||||||||||||||||||||
| 60 | isa => t('Bool'), | ||||||||||||||||||||
| 61 | lazy => 1, | ||||||||||||||||||||
| 62 | builder => 1, | ||||||||||||||||||||
| 63 | clearer => 1, | ||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 58 | has path => ( | |||||||||||||||||||
| 67 | is => 'ro', | ||||||||||||||||||||
| 68 | isa => t('Path'), | ||||||||||||||||||||
| 69 | required => 1, | ||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 121 | ▶ | sub _build_file_contents { | ||||||||||||||||||
|
Subroutine
_build_file_contents: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 73 | 121 | my ($self) = @_; | |||||||||||||||||||
| 74 | 121 | return Path::Tiny::path( $self->full_path )->slurp_raw; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 60 | ▶ | sub _trigger_file_contents { | ||||||||||||||||||
|
Subroutine
_trigger_file_contents: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 78 | 60 | my $self = shift; | |||||||||||||||||||
| 79 | 60 | $self->clear_cache_key; | |||||||||||||||||||
| 80 | 60 | $self->clear_is_cached; | |||||||||||||||||||
| 81 | 60 | $self->clear_cache_value; | |||||||||||||||||||
| 82 | 60 | return; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 115 | ▶ | sub _build_cache_key { | ||||||||||||||||||
|
Subroutine
_build_cache_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | 115 | my ($self) = @_; | |||||||||||||||||||
| 87 | 115 | return 'sig/' . $self->path; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 49 | ▶ | sub _build_cache_value { | ||||||||||||||||||
|
Subroutine
_build_cache_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 91 | 49 | my ($self) = @_; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # this stat isn't ideal, but it'll do | ||||||||||||||||||||
| 94 | 49 | my $last_mod = ( stat( $self->full_path ) )[9]; | |||||||||||||||||||
| 95 | 49 | return $self->_sig( [ $self->base_sig, $last_mod, $self->file_contents ] ); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 121 | ▶ | sub _build_is_cached { | ||||||||||||||||||
|
Subroutine
_build_is_cached: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 99 | 121 | my ($self) = @_; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 121 | ▶ | return unless $self->_has_cache_engine; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 71 | my $cached_value = $self->cache_engine->get( $self->cache_key ); | |||||||||||||||||||
| 103 | 71 | ▶ | return defined $cached_value && $cached_value eq $self->cache_value; | ||||||||||||||||||
|
Condition100%3/3
defined $cached_value && $cached_value eq $self->cache_value
MC/DC100%2/2
defined $cached_value && $cached_value eq $self->cache_value
defined $cached_value
$cached_value eq $self->cache_value
Truth table100%3/3
A && B
A= defined $cached_valueB= $cached_value eq $self->cache_value
| |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 99 | ▶ | sub update { | ||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 107 | 99 | my ($self) = @_; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 99 | ▶ | return unless $self->_has_cache_engine; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 53 | $self->cache_engine->set( $self->cache_key, $self->cache_value ); | |||||||||||||||||||
| 111 | 53 | $self->is_cached(1); | |||||||||||||||||||
| 112 | 53 | return; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 0 | ▶ | sub remove { | ||||||||||||||||||
|
Subroutine
remove: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 116 | 0 | my ($self) = @_; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | return unless $self->_has_cache_engine; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 0 | $self->cache_engine->remove( $self->cache_key ); | |||||||||||||||||||
| 120 | 0 | return; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 73 | ▶ | sub _sig { | ||||||||||||||||||
|
Subroutine
_sig: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 124 | 73 | my ( $self, $data ) = @_; | |||||||||||||||||||
| 125 | 73 | return sha1_hex( join( ',', @$data ) ); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 58 | 1; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | # ABSTRACT: Caching model for Code::TidyAll | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||