Moose/Instrument/Cache.pm

filter: Statementstmt 70.4% 88 / 125 Branchbran 53.8% 28 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 73.3% 11 / 15 Podpod 100% 1 / 1 totaltotal 66.3% 128 / 193 CC 142 SCAR 17.2
CC
142
Cov
66%
__ANON__
8.5
cached_amplitude
8.5
cached_format_data
8.5
CRAP 5.6
« Moose/Instrument/Rigol_DG5000.pm ↑ Summary Moose/Instrument/Cryomagnetics_4G.pm »
1 package Lab::Moose::Instrument::Cache;
2 15 $Lab::Moose::Instrument::Cache::VERSION = '3.931';
3 #ABSTRACT: Device caching functionality in Moose::Instrument drivers
4  
5 18 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 Moose::Exporter->setup_import_methods( with_meta => ['cache'] );
12  
13 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 210 ▶sub cache {
Subroutine cache: called CC=1 SCAR=0.5
Pod documented
16 210 my ( $meta, $name, %options ) = @_;
17  
18 210 my @options = %options;
19 210 validated_hash(
20 \@options,
21 getter => { isa => 'Str' },
22 isa => { optional => 1, default => 'Any' },
23 index_arg => { isa => 'Str', optional => 1 },
24 );
25  
26 210 my $getter = $options{getter};
27 210 my $isa = $options{isa};
28 210 my $index_arg = $options{index_arg};
29 210 my $have_index_arg = defined $index_arg;
30 210 my $function = "cached_$name";
31 210 my $attribute = "cached_${name}_attribute";
32 210 my $builder = "cached_${name}_builder";
33 210 my $clearer = "clear_cached_$name";
34 210 my $predicate = "has_cached_$name";
35  
36 # Creat builder method for the entry. The user can override
37 # (method modifier) this in an instrument driver to add additional
38 # arguments to the getter.
39 $meta->add_method(
40 0 ▶ $builder => sub {
Subroutine cached_line_notch_filters_builder: not called CC=2 SCAR=10.1
Subroutine __ANON__: called CC=2 SCAR=10.1
Subroutine cached_source_function_builder: not called CC=2 SCAR=10.1
41 0 my $self = shift;
42 0 ▶ if ($have_index_arg) {
Branch: 3 branches
BranchTrueFalseTotal
if ($have_index_arg) 0 0 0
if ($have_index_arg) 1 403 404
if ($have_index_arg) 0 0 0
43 0 my ($index) = validated_list(
44 \@_,
45 $index_arg => { isa => 'Int' }
46 );
47 0 return $self->$getter( $index_arg => $index );
48 }
49 0 return $self->$getter();
50 }
51 210 );
52  
53 $meta->add_attribute(
54 $attribute => (
55 is => 'rw',
56 init_arg => undef,
57 isa => 'ArrayRef',
58 173 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 )
60 210 );
61  
62 $meta->add_method(
63 572 ▶ $function => sub {
Subroutine cached_amplitude: called CC=6 SCAR=21.4
Subroutine cached_func3: not called CC=6 SCAR=21.4
64 0 my $self = shift;
65 0 my $array = $self->$attribute();
66  
67 0 ▶ if ($have_index_arg) {
Branch: 3 branches
BranchTrueFalseTotal
if ($have_index_arg) 7 565 572
if ($have_index_arg) 0 0 0
if ($have_index_arg) 0 0 0
68 7 my ( $index, $value ) = validated_list(
69 \@_,
70 $index_arg => { isa => 'Int' },
71 value => { optional => 1 },
72 );
73 5 ▶ if ( defined $value ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $value) 2 3 5
if (defined $value) 0 0 0
if (defined $value) 545 232 777
74  
75 # Store entry.
76 0 return $array->[$index] = $value;
77 }
78  
79 # Query cache.
80 0 ▶ if ( defined $array->[$index] ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $array->[$index]) 2 1 3
if (defined $array->[$index]) 201 181 382
if (defined $array->[$index]) 545 0 545
81 2 return $array->[$index];
82 }
83 1 return $array->[$index]
84 = $self->$builder( $index_arg => $index );
85 }
86  
87 # No vector index argument. Behave like usual Moose attribute.
88 0 ▶ if ( @_ == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 0) 317 248 565
if (@_ == 0) 192 9 201
if (@_ == 0) 0 0 0
89  
90 # Query cache.
91 0 ▶ if ( defined $array->[0] ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $array->[0]) 297 20 317
if (defined $array->[0]) 0 0 0
if (defined $array->[0]) 0 0 0
92 0 return $array->[0];
93 }
94 0 $array->[0] = $self->$builder();
95 0 return $array->[0];
96 }
97  
98 # Store entry.
99 0 my ($value) = pos_validated_list( \@_, { isa => $isa } );
100 0 return $array->[0] = $value;
101 }
102 210 );
103  
104 $meta->add_method(
105 2 ▶ $clearer => sub {
Subroutine clear_cached_sense_bandwidth_video: called CC=3 SCAR=14.2
Subroutine __ANON__: not called CC=3 SCAR=14.2
106 0 my $self = shift;
107 0 my $index;
108 0 ▶ if ($have_index_arg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($have_index_arg) 1 1 2
if ($have_index_arg) 0 0 0
109  
110 # If no index is given, clear them all!
111 0 ($index) = validated_list(
112 \@_,
113 $index_arg => { isa => 'Int', optional => 1 },
114 );
115 }
116 0 ▶ if ( defined $index ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $index) { } 1 1 2
if (defined $index) { } 0 0 0
117 0 $self->$attribute->[$index] = undef;
118 }
119 else {
120 0 $self->$attribute( [] );
121 }
122 }
123 210 );
124  
125 $meta->add_method(
126 4 ▶ $predicate => sub {
Subroutine has_cached_ground: called CC=3 SCAR=11.4
Subroutine __ANON__: called CC=3 SCAR=11.4
127 0 my $self = shift;
128 0 my $index = 0;
129 0 ▶ if ($have_index_arg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($have_index_arg) 2 2 4
if ($have_index_arg) 0 0 0
130 2 ($index) = validated_list(
131 \@_,
132 $index_arg => { isa => 'Int' }
133 );
134 }
135  
136 4 my $array = $self->$attribute();
137 4 ▶ if ( defined $array->[$index] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $array->[$index]) 2 2 4
if (defined $array->[$index]) 0 784 784
138 2 return 1;
139 }
140 2 return;
141 }
142 210 );
143 }
144  
145 15 1;
146  
147 __END__
« Moose/Instrument/Rigol_DG5000.pm ↑ Summary Moose/Instrument/Cryomagnetics_4G.pm »