Moose/Instrument/Cache.pm

filter: Statementstmt 71.5% 78 / 109 Branchbran 57.1% 24 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 10 / 14 Podpod 100% 1 / 1 totaltotal 68.0% 113 / 166 SCAR 65.3
CC
128
Cov
68%
__ANON__
6.9
cached_func3
6.9
cached_initiate_continuous
6.9
CRAP 687.9
« XPRESS/Data/XPRESS_DataFile.pm ↑ Summary Instrument/AH2700A.pm »
1 package Lab::Moose::Instrument::Cache;
2 15 $Lab::Moose::Instrument::Cache::VERSION = '3.881';
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 sub cache {
16 210 ▶ my ( $meta, $name, %options ) = @_;
Subroutine cache: called CC=1 SCAR=0.4
Pod documented
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 $builder => sub {
41 0 ▶ my $self = shift;
Subroutine cached_sense_average_state_builder: not called CC=2 SCAR=11.6
Subroutine cached_func2_builder: called CC=2 SCAR=11.6
42 0 ▶ if ($have_index_arg) {
Branch: 3 branches
BranchTrueFalseTotal
if ($have_index_arg) 0 0 0
if ($have_index_arg) 1 207 208
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 $function => sub {
64 1551 ▶ my $self = shift;
Subroutine cached_order: called CC=6 SCAR=19.3
Subroutine cached_func3: not called CC=6 SCAR=19.3
65 1551 my $array = $self->$attribute();
66  
67 1551 ▶ if ($have_index_arg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($have_index_arg) 7 1544 1551
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: 2 branches
BranchTrueFalseTotal
if (defined $value) 2 3 5
if (defined $value) 0 0 0
74  
75 # Store entry.
76 2 return $array->[$index] = $value;
77 }
78  
79 # Query cache.
80 3 ▶ if ( defined $array->[$index] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $array->[$index]) 2 1 3
if (defined $array->[$index]) 69 110 179
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 1544 ▶ if ( @_ == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) 993 551 1544
if (@_ == 0) 65 4 69
89  
90 # Query cache.
91 993 ▶ if ( defined $array->[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $array->[0]) 968 25 993
if (defined $array->[0]) 0 0 0
92 968 return $array->[0];
93 }
94 25 $array->[0] = $self->$builder();
95 25 return $array->[0];
96 }
97  
98 # Store entry.
99 551 my ($value) = pos_validated_list( \@_, { isa => $isa } );
100 550 return $array->[0] = $value;
101 }
102 210 );
103  
104 $meta->add_method(
105 $clearer => sub {
106 2 ▶ my $self = shift;
Subroutine clear_cached_imode: called CC=3 SCAR=14.2
Subroutine __ANON__: not called CC=3 SCAR=14.2
107 2 my $index;
108 2 ▶ 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 1 ($index) = validated_list(
112 \@_,
113 $index_arg => { isa => 'Int', optional => 1 },
114 );
115 }
116 2 ▶ if ( defined $index ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $index) { } 1 1 2
if (defined $index) { } 0 0 0
117 1 $self->$attribute->[$index] = undef;
118 }
119 else {
120 1 $self->$attribute( [] );
121 }
122 }
123 210 );
124  
125 $meta->add_method(
126 $predicate => sub {
127 4 ▶ my $self = shift;
Subroutine has_cached_line_notch_filters: called CC=3 SCAR=12.5
Subroutine has_cached_func2: not called CC=3 SCAR=12.5
128 4 my $index = 0;
129 4 ▶ 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 0 0
138 2 return 1;
139 }
140 2 return;
141 }
142 210 );
143 }
144  
145 15 1;
146  
147 __END__
148  
149 =pod
150  
151 =encoding UTF-8
152  
153 =head1 NAME
154  
155 Lab::Moose::Instrument::Cache - Device caching functionality in Moose::Instrument drivers
156  
157 =head1 VERSION
158  
159 version 3.881
160  
161 =head1 SYNOPSIS
162  
163 in your driver:
164  
165 use Lab::Moose::Instrument::Cache;
166  
167 cache foobar => (getter => 'get_foobar');
168  
169 sub get_foobar {
170 my $self = shift;
171
172 return $self->cached_foobar(
173 $self->query(command => ...));
174 }
175  
176 sub set_foobar {
177 my ($self, $value) = @_;
178 $self->write(command => ...);
179 $self->cached_foobar($value);
180 }
181  
182 =head1 DESCRIPTION
183  
184 This package exports a new Moose keyword: B<cache>.
185  
186 Calling C<< cache key => (getter => $getter, isa => $type) >> generates the
187 following functions:
188  
189 =over
190  
191 =item C<cached_key> (accessor)
192  
193 Calling C<< $instr->cached_key() >> will return the last stored value from the
194 cache. If the cache entry is empty, use the C<$getter> method.
195  
196 To update the cache entry, call C<< $instr->cached_key($value) >>.
197  
198 =item C<has_cached_key> (predicate)
199  
200 Return true if the cache entry holds a value (which is not undef).
201  
202 =item C<clear_cached_key> (clearer)
203  
204 Clear the value of the cache entry.
205  
206 =item C<cached_key_builder> (builder)
207  
208 Called by C<cached_key> if the entry is cleared. This will call the C<$getter>
209 method. Can be overriden by 'around' method modifier if the C<$getter> needs
210 special extra arguments.
211  
212 =back
213  
214 The C<isa> argument is optional.
215  
216 =head2 Array cache
217  
218 Some methods take an additional parameter (e.g. channel number). For this case
219 you can give the C<index_arg> argument to the cache keyword:
220  
221 cache foobar => (isa => 'Num', getter => 'get_foobar', index_arg => 'channel');
222  
223 # Get value from cache.
224 my $value = $instr->cached_foobar(channel => 1);
225
226 # Store value.
227 $instr->cached_foobar(channel => 2, value => 1.234);
228
229 # Clear single entry.
230 $instr->clear_cached_foobar(channel => 3);
231
232 # Clear them all.
233 $instr->clear_cached_foobar();
234
235 # Check for cache value
236 if ($instr->has_cached_foobar(channel => 1)) {...}
237  
238 =head1 COPYRIGHT AND LICENSE
239  
240 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
241  
242 Copyright 2016 Simon Reinhardt
243 2017 Andreas K. Huettel, Simon Reinhardt
244 2018 Simon Reinhardt
245 2020 Andreas K. Huettel
246  
247  
248 This is free software; you can redistribute it and/or modify it under
249 the same terms as the Perl 5 programming language system itself.
250  
251 =cut
« XPRESS/Data/XPRESS_DataFile.pm ↑ Summary Instrument/AH2700A.pm »