| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | # Store entry. | ||||||||||||||||||
| 76 | 0 | return $array->[$index] = $value; | |||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | # Query cache. | ||||||||||||||||||
| 80 | 0 | ▶ | if ( defined $array->[$index] ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | # Query cache. | ||||||||||||||||||
| 91 | 0 | ▶ | if ( defined $array->[0] ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 138 | 2 | return 1; | |||||||||||||||||
| 139 | } | ||||||||||||||||||
| 140 | 2 | return; | |||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | 210 | ); | |||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | 15 | 1; | |||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||