Karabiner/Manipulator.pm

filter: Statementstmt 90.9% 201 / 221 Branchbran 61.5% 48 / 78 Conditioncond 75.0% 18 / 24 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 93.1% 27 / 29 Podpod 90.9% 10 / 11 totaltotal 81.7% 309 / 378 CC 48 SCAR 19.6
CC
48
Cov
83%
add_parameter
15.1
write_file
12.6
new_manipulator
12.2
CRAP 7.1
« Karabiner/Manipulator/Actions/To.pm ↑ Summary Karabiner.pm »
1 package JSON::Karabiner::Manipulator ;
2 19 $JSON::Karabiner::Manipulator::VERSION = '0.018';
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 our @EXPORT = qw'new_manipulator add_action add_description add_condition add_parameter add_key_code
8 add_key_code add_any add_optional_modifiers add_mandatory_modifiers add_simultaneous add_simultaneous_options add_consumer_key_code add_pointing_button add_shell_command add_select_input_source add_set_variable add_mouse_key add_modifiers
9 add_identifier add_description add_value add_bundle_identifiers add_file_path add_input_source add_keyboard_types add_variable add_description _dump_json _fake_write_file write_file set_filename set_title set_rule_name set_save_dir';
10  
11 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
12 2 strict->import;
13 2 warnings->import;
14 2 goto &Exporter::import
15 }
16  
17 30 ▶sub new_manipulator {
Subroutine new_manipulator: called CC=12 SCAR=25.0
Pod documented
18 30 my @caller = caller(0);
19 30 ▶ my $called_directly = $caller[0] eq 'main' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$caller[0] eq 'main' ? : 3 27 30
20 30 ▶ if ($main::current_manip && $called_directly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::current_manip and $called_directly) 1 29 30
Condition100%3/3
$main::current_manip and $called_directly
!ll&&!rl&&r
14151
MC/DC100%2/2
$main::current_manip and $called_directly
$main::current_manip $called_directly
Truth table100%3/3
A and B
A= $main::current_manipB= $called_directly
ABresult
0X0
100
111
21 1 write_file($main::file_title_written);
22 }
23 30 my $class = 'JSON::Karabiner::Manipulator';
24  
25 # trash the first arg if this is using the old school OO interface
26 30 ▶ shift if $_[0] && $_[0] =~ /^JSON::Karabiner::Manipulator$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] =~ /^JSON::Karabiner::Manipulator$/ 27 3 30
Condition66%2/3
$_[0] and $_[0] =~ /^JSON::Karabiner::Manipulator$/
!ll&&!rl&&r
3027
MC/DC50%1/2
$_[0] and $_[0] =~ /^JSON::Karabiner::Manipulator$/
$_[0] $_[0] =~ /^JSON::Karabiner::Manipulator$/
Truth table66%2/3
A and B
A= $_[0]B= $_[0] =~ /^JSON::Karabiner::Manipulator$/
ABresult
0X0
100
111
27  
28 30 my @kb_obj_args = @_;
29  
30 # derive the filename from the called script
31 19 ▶ { no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 30 ▶ if (!$main::save_to_file_name && $called_directly) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $main::save_to_file_name and $called_directly) { } 3 27 30
Condition66%2/3
not $main::save_to_file_name and $called_directly
!ll&&!rl&&r
0273
MC/DC50%1/2
not $main::save_to_file_name and $called_directly
not $main::save_to_file_name $called_directly
Truth table66%2/3
not(A) and B
A= not $main::save_to_file_nameB= $called_directly
ABresult
0X0
100
111
33  
34 3 my $program_name = $0;
35 3 $program_name =~ s/^.*[\/\\]//;
36 3 $program_name =~ s/\..*$//;
37 3 $program_name = "$program_name.json";
38 3 push @kb_obj_args, $program_name;
39 3 $main::save_to_file_name = $program_name;
40 } else {
41 27 ▶ push @kb_obj_args, $main::save_to_file_name if $main::save_to_file_name;
Branch: 1 branch
BranchTrueFalseTotal
if $main::save_to_file_name 0 27 27
42 }
43  
44 30 ▶ if ($called_directly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($called_directly) 3 27 30
45 3 ▶ unshift @kb_obj_args, $main::rule_name if $main::rule_name;
Branch: 1 branch
BranchTrueFalseTotal
if $main::rule_name 3 0 3
46 3 my $save_to_dir = $main::save_to_dir;
47 3 ▶ if ($save_to_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save_to_dir) 0 3 3
48 0 push @kb_obj_args, { mod_file_dir => $save_to_dir };
49 }
50 }
51 }
52  
53  
54 30 my $self = {
55 actions => {},
56 _disable_validity_tests => 0,
57 _kb_obj_args => \@kb_obj_args,
58 _fake_write_flag => 0,
59 };
60 30 bless $self, $class;
61 {
62 19 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 30 $main::current_manip = $self;
64 }
65 30 return $self;
66 }
67  
68 0 ▶sub set_filename {
Subroutine set_filename: not called CC=3 SCAR=24.8
Pod documented
69 0 my $fn = shift;
70 0 ▶ croak 'You must pass a file name' unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 0 0
71  
72 0 ▶ if ($fn !~ /\.json$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fn =~ /\.json$/) 0 0 0
73 0 $fn = $fn . '.json';
74 }
75  
76 0 $main::save_to_file_name = $fn;
77 }
78  
79 0 ▶sub set_save_dir {
Subroutine set_save_dir: not called CC=2 SCAR=11.6
Pod documented
80 0 my $sd = shift;
81 0 ▶ croak 'You must pass a file name' unless $sd;
Branch: 1 branch
BranchTrueFalseTotal
unless $sd 0 0 0
82 0 ▶ { no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.2
83 0 $main::save_to_dir = $sd;
84 }
85 }
86  
87 1 ▶sub set_title {
Subroutine set_title: called CC=2 SCAR=7.1
Pod documented
88 1 my $title = shift;
89 1 ▶ croak 'You must pass a file name' unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 1 1
90  
91 1 $main::file_title_written = $title;
92 }
93  
94 2 ▶sub set_rule_name {
Subroutine set_rule_name: called CC=2 SCAR=7.0
Pod documented
95 2 my $rule_name = shift;
96 2 ▶ croak 'You must pass a rule name' unless $rule_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_name 0 2 2
97  
98 19 ▶ { no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 $main::rule_name = $rule_name;
100 }
101 }
102  
103 18 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
104 18 our $AUTOLOAD;
105 18 my $program = $AUTOLOAD;
106 18 my ($func) = $program =~ /.*::(.*)$/;
107 18 my @action_functions = qw (add_key_code add_any add_optional_modifiers add_mandatory_modifiers add_simultaneous add_simultaneous_options add_consumer_key_code add_pointing_button add_shell_command add_select_input_source add_set_variable add_mouse_key add_modifiers);
108  
109 18 my $is_action_function = grep { $_ eq $func } @action_functions;
110 18 ▶ if ($is_action_function) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_action_function) 6 12 18
111 6 my $current_action;
112 {
113 19 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 6 $current_action = $main::current_action;
115 }
116 6 $current_action->$func(@_);
117 6 return;
118 }
119  
120 12 my @condition_functions = qw(add_identifier add_description add_value add_bundle_identifiers add_file_path add_input_source add_keyboard_types add_variable add_description);
121  
122 12 my $is_condition_function = grep { $_ eq $func } @condition_functions;
123 12 ▶ if ($is_condition_function) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_condition_function) 0 12 12
124 0 my $current_condition;
125 {
126 0 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.2
127 0 $current_condition = $main::current_condition;
128 }
129 0 $current_condition->$func(@_);
130 0 return;
131 }
132  
133 }
134  
135 40 ▶sub _get_args {
Subroutine _get_args: called CC=3 SCAR=11.0
136 40 my @args = @_;
137 40 ▶ croak "No args passed" unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 40 40
138  
139 40 my $s = shift;
140 40 my $type;
141 40 ▶ if (ref $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s) { } 34 6 40
142 34 $type = shift;
143 } else {
144 6 $type = $s;
145 6 $s = $main::current_manip;
146 }
147 40 return ($s, $type, @_);
148 }
149  
150 31 ▶sub add_action {
Subroutine add_action: called CC=2 SCAR=6.9
Pod documented
151 31 my ($s, $type) = _get_args(@_);
152 31 ▶ croak 'To add a action, you must tell me which kind you\'d like to add' if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 31 31
153  
154 31 my $uctype = ucfirst($type);
155 31 my $package = "JSON::Karabiner::Manipulator::Actions::" . $uctype;
156 31 eval "require $package";
157 31 my $action = $package->new($type);
158 30 my %hash = %{$s->{actions}};
159 30 $type = $action->{def_name};
160 30 $hash{$type} = $action->{data};
161 30 $s->{actions} = \%hash;
162 30 return $action;
163 }
164  
165 7 ▶sub add_condition {
Subroutine add_condition: called CC=3 SCAR=11.0
Pod documented
166 7 my ($s, $type) = _get_args(@_);
167 7 ▶ croak 'To add a condition, you must tell me which kind you\'d like to add' if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 7 7
168  
169 7 my $uctype = ucfirst($type);
170 7 my $package = "JSON::Karabiner::Manipulator::Conditions::" . $uctype;
171 7 eval "require $package";
172 7 my $condition = $package->new($type);
173 7 ▶ if (defined $s->{actions}{conditions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{'actions'}{'conditions'}) { } 5 2 7
174 5 push @{$s->{actions}{conditions}}, $condition;
175 } else {
176 2 $s->{actions}{conditions} = [ $condition ];
177 }
178 7 return $condition;
179 }
180  
181 1 ▶sub add_description {
Subroutine add_description: called CC=2 SCAR=7.1
Pod documented
182 1 my ($s, $desc) = _get_args(@_);
183  
184 1 ▶ croak 'To add a description, you must provide one' if !$desc;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc 0 1 1
185 1 $s->{actions}{description} = $desc;
186 }
187  
188 1 ▶sub add_parameter {
Subroutine add_parameter: called CC=8 SCAR=27.2
Pod documented
189 1 my ($s, $param, $value) = _get_args(@_);
190 1 ▶ croak 'To add a parameter, you must tell me which kind you\'d like to add' if !$param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 0 1 1
191 1 ▶ croak 'To add a parameter, you must provide a value' if !$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 1 1
192  
193 1 my @acceptable_values = qw( to_if_alone_timeout_milliseconds
194 alone_timeout
195 alone
196 to_if_held_down_threshold_milliseconds
197 held_down_threshold
198 down_threshold
199 held_down
200 down
201 to_delayed_action_delay_milliseconds
202 delayed_action_delay
203 action_delay
204 delay
205 simultaneous_threshold_milliseconds
206 simultaneous_threshold
207 simultaneous
208 );
209  
210 1 my $param_exists = grep { $param eq $_ } @acceptable_values;
211 1 ▶ croak "'$param' in an unrecognzed parameter" unless $param_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $param_exists 0 1 1
212  
213 # get param full name
214 1 ▶ if ($param =~ /alone/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($param =~ /alone/) { } 1 0 1
elsif ($param =~ /down/) { } 0 0 0
elsif ($param =~ /delay/) { } 0 0 0
elsif ($param =~ /simultaneous/) { } 0 0 0
215 1 $param = 'to_if_alone_timeout_milliseconds';
216 } elsif ($param =~ /down/) {
217 0 $param = 'to_if_held_down_threshold_milliseconds';
218 } elsif ($param =~ /delay/) {
219 0 $param = 'to_delayed_action_delay_milliseconds';
220 } elsif ($param =~ /simultaneous/) {
221 0 $param = 'simultaneous_threshold_milliseconds';
222 }
223  
224 1 $s->{actions}{parameters}{"basic.$param"} = $value;
225 }
226  
227 32 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=2 SCAR=6.9
Pod undocumented
228 32 my $obj = shift;
229 #TODO: Change this under certain conditions
230 32 $obj->{actions}{type} = 'basic';
231 32 ▶ $obj->_do_validity_checks($obj->{actions}) unless $obj->{_disable_validity_tests};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"_disable_validity_tests"} 11 21 32
232 29 return $obj->{actions};
233 }
234  
235 11 ▶sub _do_validity_checks {
Subroutine _do_validity_checks: called CC=1 SCAR=0.0
236 11 my $s = shift;
237 11 my $actions = shift;
238 11 my $from = $actions->{from};
239 11 $s->_do_from_validity_checks($from);
240 }
241  
242 3 ▶sub _dump_json {
Subroutine _dump_json: called CC=2 SCAR=6.9
243 3 my $s = $main::current_manip;
244 3 my @kb_obj_args = @{$s->{_kb_obj_args}};
245 3 ▶ if (!@kb_obj_args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@kb_obj_args) 2 1 3
246 2 croak "The _dump_json method cannot be run on this manipulator.";
247 }
248  
249 1 require JSON::Karabiner;
250 1 my $little_title = shift @kb_obj_args;
251 1 unshift @kb_obj_args, 'SET WITH write_file METHOD';
252 1 my $kb_obj = JSON::Karabiner->new( @kb_obj_args );
253  
254 1 my $rule = $kb_obj->add_rule($little_title);
255 1 my $temp_manip = $rule->add_manipulator();
256 1 %{$temp_manip} = %{$s};
257 1 $kb_obj->_dump_json;
258 }
259  
260 4 ▶sub write_file {
Subroutine write_file: called CC=12 SCAR=25.4
Pod documented
261 4 my $s = $main::current_manip;
262 4 ▶ shift if $_[0] && (ref $_[0]) =~ /^JSON::Karabiner::Manipulator$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and ref($_[0]) =~ /^JSON::Karabiner::Manipulator$/ 3 1 4
Condition66%2/3
$_[0] and ref($_[0]) =~ /^JSON::Karabiner::Manipulator$/
!ll&&!rl&&r
013
MC/DC50%1/2
$_[0] and ref($_[0]) =~ /^JSON::Karabiner::Manipulator$/
$_[0] ref($_[0]) =~ /^JSON::Karabiner::Manipulator$/
Truth table66%2/3
A and B
A= $_[0]B= ref($_[0]) =~ /^JSON::Karabiner::Manipulator$/
ABresult
0X0
100
111
263 4 my $title = shift;
264  
265 4 my $filename;
266 4 ▶ if ($title && $title =~ /\.json$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title and $title =~ /\.json$/) { } 0 4 4
Condition66%2/3
$title and $title =~ /\.json$/
!ll&&!rl&&r
130
MC/DC0%0/2
$title and $title =~ /\.json$/
$title $title =~ /\.json$/
Truth table66%2/3
A and B
A= $titleB= $title =~ /\.json$/
ABresult
0X0
100
111
267 0 $filename = $title;
268 } else {
269 4 ▶ $filename = shift || $main::save_to_file_name || $main::file_written;
Condition83%5/6
shift() || $main::save_to_file_name || $main::file_written
l!l&&r!l&&!r
310
shift() or $main::save_to_file_name
l!l&&r!l&&!r
121
MC/DC0%0/3
shift() || $main::save_to_file_name || $main::file_written
shift() $main::save_to_file_name $main::file_written
Truth table75%3/4
A or B || C
A= shift()B= $main::save_to_file_nameC= $main::file_written
ABCresult
1XX1
01X1
0011
0000
270 }
271  
272 4 my @kb_obj_args = @{$s->{_kb_obj_args}};
273 4 ▶ if (!@kb_obj_args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@kb_obj_args) 0 4 4
274 0 croak "The _write_file method cannot be run on this manipulator.";
275 }
276  
277 4 ▶ croak 'You must supply a title for the first manipulator' if !$title && !$main::file_title_written;
Branch: 1 branch
BranchTrueFalseTotal
unless $title or $main::file_title_written 0 4 4
Condition66%2/3
$title or $main::file_title_written
l!l&&r!l&&!r
310
MC/DC0%0/2
$title or $main::file_title_written
$title $main::file_title_written
Truth table66%2/3
A or B
A= $titleB= $main::file_title_written
ABresult
1X1
011
000
278 4 ▶ if (!$title) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($title) 1 3 4
279 1 $title = $main::file_title_written;
280 }
281  
282 4 require JSON::Karabiner;
283 4 my $little_title = shift @kb_obj_args;
284 4 unshift @kb_obj_args, $title;
285 4 $kb_obj_args[1] = $filename;
286  
287 4 my $kb_obj = JSON::Karabiner->new( @kb_obj_args );
288 4 ▶ if ($s->{_fake_write_flag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"_fake_write_flag"}) 4 0 4
289 4 $kb_obj->{_fake_write_flag} = 1;
290 }
291 4 my $rule = $kb_obj->add_rule($little_title);
292 4 my $temp_manip = $rule->add_manipulator();
293 4 %{$temp_manip} = %{$s};
294 4 $kb_obj->write_file();
295 4 $kb_obj->{_fake_write_flag} = 0;
296 {
297 19 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
298 4 $main::file_written = $kb_obj_args[1];
299 4 $main::file_title_written = $kb_obj_args[0];
300 }
301  
302 }
303  
304 5 ▶sub _fake_write_file {
Subroutine _fake_write_file: called CC=2 SCAR=6.9
305 5 my $s = $main::current_manip;
306 5 my $title = shift;
307 5 my $file_name = shift;
308  
309 5 my @kb_obj_args = @{$s->{_kb_obj_args}};
310 5 ▶ if (!@kb_obj_args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@kb_obj_args) 2 3 5
311 2 croak "The _fake_write method cannot be run on this manipulator.";
312 }
313  
314 3 $s->{_fake_write_flag} = 1;
315 3 $s->write_file($title, $file_name);
316 3 $s->{_fake_write_flag} = 0;
317 }
318  
319 11 ▶sub _do_from_validity_checks {
Subroutine _do_from_validity_checks: called CC=3 SCAR=11.0
320 11 my $s = shift;
321 11 my $from = shift;
322  
323 11 ▶ if (! defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from) 2 9 11
324 2 croak "No 'from' action found in the manipulator. You must add a 'from' action.'";
325 }
326  
327 9 ▶ if (! %$from) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$from) 1 8 9
328 1 croak "The 'from' action is empty. Perform methods on the 'from' action to tell it how to behave.";
329 }
330  
331 8 return;
332  
333 # my @from_keys = keys %$from;
334 # my $has_key_code = grep { $_ =~ /^any|consumer_key_code|key_code$/ } @from_keys;
335 # if (!$has_key_code && grep { $_ =~ /modifiers/ } @from_keys) {
336 # croak "You cannot have modifiers without anything to modify in a 'from' action.";
337 # }
338 }
339  
340 # ABSTRACT: manipulator object for containing and outputting json data to file
341  
342 19 1;
343  
344 __END__
« Karabiner/Manipulator/Actions/To.pm ↑ Summary Karabiner.pm »