blib/lib/StorageDisplay/Moose/Cached.pm

filter: Statementstmt 100% 66 / 66 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 95.5% 86 / 90 CC 9 SCAR 7.9
CC
9
Cov
96%
__ANON__
3.0
lv
3.0
pv
3.0
CRAP 2.2
« blib/lib/StorageDisplay/Data/LVM.pm ↑ Summary blib/lib/StorageDisplay/Collect/CMD/Remote.pm »
1 #
2 # This file is part of StorageDisplay
3 #
4 # This software is copyright (c) 2014-2023 by Vincent Danjean.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package StorageDisplay::Moose::Cached;
13 # ABSTRACT: Moose extension for StorageDisplay
14  
15 2 our $VERSION = '2.06'; # VERSION
16  
17 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our %orig_has; # save original 'has' sub routines here
20  
21 18 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
22 18 my $callpkg = caller 0;
23 {
24 9 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
25 9 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 $orig_has{$callpkg} = *{$callpkg."::has"}{CODE};
27 18 *{$callpkg."::has"} = \&cached_has;
28 }
29 18 return;
30 }
31  
32 63 ▶sub cached_has {
Subroutine cached_has: called CC=3 SCAR=11.0
Pod undocumented
33 63 my ($attr, %args) = @_;
34  
35 63 my $callpkg = caller 0;
36 63 ▶ if (exists $args{cached_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"cached_hash"}) 18 45 63
37 18 my $compute = $args{compute};
38 18 my $type = $args{cached_hash};
39 18 ▶ croak "'compute' attribute required" if not exists $args{compute};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"compute"} 0 18 18
40 18 my $cache_set = '_cached_set_'.$attr;
41 18 my $cache_has = '_cached_has_'.$attr;
42 18 my $cache_get = '_cached_get_'.$attr;
43 18 $args{handles}->{$cache_set} = 'set';
44 18 $args{handles}->{$cache_has} = 'exists';
45 18 $args{handles}->{$cache_get} = 'get';
46 %args = (
47 is => 'bare',
48 required => 1,
49 18 ▶ default => sub { return {}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 18 lazy => 1,
51 init_arg => undef, # prevent from being set by constructor
52 %args,
53 traits => [ 'Hash' ],
54 isa => "HashRef[$type]",
55 );
56 18 delete $args{cached_hash};
57 18 delete $args{compute};
58 #print STDERR "My cached arg $attr\n";
59 $callpkg->meta->add_method(
60 98 ▶ $attr => sub {
Subroutine lv: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
61 98 my $self = shift;
62 98 my $name = shift;
63  
64 98 ▶ if ($self->$cache_has($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->$cache_has($name)) 47 51 98
if ($self->$cache_has($name)) 33 45 78
65 47 return $self->$cache_get($name);
66 }
67 51 my $elem = $compute->($self, $name, @_);
68 51 ▶ if (defined($elem)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $elem) 51 0 51
if (defined $elem) 45 0 45
69 51 $self->$cache_set($name, $elem);
70 }
71 51 return $elem;
72  
73 18 });
74 }
75 63 $orig_has{$callpkg}->($attr, %args);
76 }
77  
78 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
79 # Mark current package as loaded;
80 9 my $p = __PACKAGE__;
81 9 $p =~ s,::,/,g;
82 9 chomp(my $cwd = `pwd`);
83 9 $INC{$p.'.pm'} = $cwd.'/'.__FILE__;#k"current file";
84 }
85  
86 2 1;
87  
88 __END__
« blib/lib/StorageDisplay/Data/LVM.pm ↑ Summary blib/lib/StorageDisplay/Collect/CMD/Remote.pm »