dbMan/Extension/CmdSetSuggestionCache.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 125 CC 16 SCAR 35.7
CC
16
Cov
0%
handle_action
72.0
cmdcomplete
42.0
menu
20.0
CRAP 35.5
1 package DBIx::dbMan::Extension::CmdSetSuggestionCache;
2  
3 use strict;
4 use base 'DBIx::dbMan::Extension';
5  
6 our $VERSION = '0.01';
7  
8 1;
9  
10 sub IDENTIFICATION { return "000001-000095-000001"; }
11  
12 sub preference { return 1000; }
13  
14 sub known_actions { return [ qw/COMMAND NOTIFY/ ]; }
15  
16 sub init {
17 my $obj = shift;
18  
19 $obj->{prompt_num} = $obj->{-interface}->register_prompt(800);
20 $obj->{prompt_title} = $obj->{-config}->prompt_tabcache || '[tab-cache]';
21 }
22  
23 sub done {
24 my $obj = shift;
25  
26 $obj->{-interface}->deregister_prompt( $obj->{prompt_num} );
27 }
28  
29 sub menu {
30 my $obj = shift;
31  
32 my $local_mempool = $obj->{-dbi}->mempool();
33 unless ( $local_mempool ) {
34 $obj->{-interface}->prompt($obj->{prompt_num}, '');
35 return ();
36 }
37  
38 my $dir = 'on'; my $sel = ' ';
39 $sel = '*' if $local_mempool->get('suggestion_cache');
40 $dir = 'off' if $sel eq '*';
41  
42 return ( { label => 'Settings', submenu => [
43 { label => $sel.' '.'Suggestion cache',
44 action => { action => 'COMMAND',
45 cmd => 'set suggestion cache '.$dir } }
46 ] } );
47 }
48  
49 sub handle_action {
50 my ($obj,%action) = @_;
51  
52 if ($action{action} eq 'COMMAND') {
53 if ($action{cmd} =~ /^set\s+suggestion\s+cache\s+(on|off)$/i) {
54 my $want = lc $1; my $owant = $want;
55 $want = '' if $want eq 'off';
56 $action{action} = 'OUTPUT';
57  
58 my $local_mempool = $obj->{-dbi}->mempool();
59 if ( $local_mempool ) {
60 $local_mempool->set('suggestion_cache',$want);
61 $action{output} = "Suggestion cache $owant.\n";
62 if ( $local_mempool->get('suggestion_cache') ) {
63 $obj->{-interface}->prompt($obj->{prompt_num}, $obj->{prompt_title});
64 } else {
65 $obj->{-interface}->prompt($obj->{prompt_num}, '');
66 }
67 } else {
68 $action{output} = "There is no active connection where suggestion cache can be used.\n";
69 $obj->{-interface}->prompt($obj->{prompt_num}, '');
70 }
71 $obj->{-interface}->rebuild_menu();
72 }
73 } elsif ( $action{action} eq 'NOTIFY' and $action{notify} eq 'connection_change' ) {
74 my $local_mempool = $obj->{-dbi}->mempool();
75  
76 if ( $local_mempool and $local_mempool->get('suggestion_cache') ) {
77 $obj->{-interface}->prompt($obj->{prompt_num}, $obj->{prompt_title});
78 } else {
79 $obj->{-interface}->prompt($obj->{prompt_num}, '');
80 }
81 }
82  
83 $action{processed} = 1;
84 return %action;
85 }
86  
87 sub cmdhelp {
88 return [
89 'SET SUGGESTION CACHE [ON|OFF]' => 'Set suggestion cache on or off.'
90 ];
91 }
92  
93 sub cmdcomplete {
94 my ($obj,$text,$line,$start) = @_;
95
96 my $local_mempool = $obj->{-dbi}->mempool();
97 if ( $local_mempool ) {
98 return qw/ON OFF/ if $line =~ /^\s*SET\s+SUGGESTION\s+CACHE\s+\S*$/i;
99 return qw/CACHE/ if $line =~ /^\s*SET\s+SUGGESTION\s+\S*$/i;
100 return qw/SUGGESTION/ if $line =~ /^\s*SET\s+\S*$/i;
101 return qw/SET/ if $line =~ /^\s*[A-Z]*$/i;
102 }
103 return ();
104 }