Karabiner/Manipulator.pm

filter: Statementstmt 94.2% 164 / 174 Branchbran 64.0% 32 / 50 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 23 / 23 Podpod 57.1% 4 / 7 totaltotal 86.4% 224 / 259 CC 27 SCAR 15.4
CC
27
Cov
87%
add_parameter
15.1
write_file
6.2
AUTOLOAD
3.0
CRAP 4.7
« Karabiner.pm ↑ Summary Karabiner/Manipulator/Conditions/Input_source_if.pm »
1 package JSON::Karabiner::Manipulator ;
2 17 $JSON::Karabiner::Manipulator::VERSION = '0.011';
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 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';
10  
11 sub import {
12 2 ▶ strict->import;
Subroutine import: called CC=1 SCAR=0.0
13 2 warnings->import;
14 2 goto &Exporter::import
15 }
16  
17 sub new_manipulator {
18 26 ▶ my $class = 'JSON::Karabiner::Manipulator';
Subroutine new_manipulator: called CC=2 SCAR=6.9
Pod undocumented
19 26 ▶ shift if $_[0] =~ /^JSON::Karabiner::Manipulator$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^JSON::Karabiner::Manipulator$/ 24 2 26
20  
21 26 my @kb_obj_args = @_;
22 26 my $self = {
23 actions => {},
24 _disable_validity_tests => 0,
25 _kb_obj_args => \@kb_obj_args,
26 _fake_write_flag => 0,
27 };
28 26 bless $self, $class;
29 {
30 17 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 26 $main::current_manip = $self;
32 }
33 26 return $self;
34 }
35  
36 sub AUTOLOAD {
37 16 ▶ our $AUTOLOAD;
Subroutine AUTOLOAD: called CC=3 SCAR=11.1
38 17 ▶ use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 16 my $program = $AUTOLOAD;
40 16 my ($func) = $program =~ /.*::(.*)$/;
41 16 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);
42  
43 16 my $is_action_function = grep { $_ eq $func } @action_functions;
44 16 ▶ if ($is_action_function) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_action_function) 4 12 16
45 4 my $current_action;
46 {
47 17 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 $current_action = $main::current_action;
49 }
50 4 $current_action->$func(@_);
51 4 return;
52 }
53  
54 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);
55  
56 12 my $is_condition_function = grep { $_ eq $func } @condition_functions;
57 12 ▶ if ($is_condition_function) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_condition_function) 0 12 12
58 0 my $current_condition;
59 {
60 17 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.2
61 0 $current_condition = $main::current_condition;
62 }
63 0 $current_condition->$func(@_);
64 0 return;
65 }
66  
67 }
68  
69 sub _get_args {
70 35 ▶ my @args = @_;
Subroutine _get_args: called CC=3 SCAR=11.0
71 35 ▶ croak "No args passed" unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 35 35
72  
73 35 my $s = shift;
74 35 my $type;
75 35 ▶ if (ref $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s) { } 31 4 35
76 31 $type = shift;
77 } else {
78 4 $type = $s;
79 4 $s = $main::current_manip;
80 }
81 35 return ($s, $type, @_);
82 }
83  
84 sub add_action {
85 26 ▶ my ($s, $type) = _get_args(@_);
Subroutine add_action: called CC=2 SCAR=6.9
Pod documented
86 26 ▶ 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 26 26
87  
88 26 my $uctype = ucfirst($type);
89 26 my $package = "JSON::Karabiner::Manipulator::Actions::" . $uctype;
90 26 eval "require $package";
91 26 my $action = $package->new($type);
92 25 my %hash = %{$s->{actions}};
93 25 $type = $action->{def_name};
94 25 $hash{$type} = $action->{data};
95 25 $s->{actions} = \%hash;
96 25 return $action;
97 }
98  
99 sub add_condition {
100 7 ▶ my ($s, $type) = _get_args(@_);
Subroutine add_condition: called CC=3 SCAR=11.0
Pod documented
101 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
102  
103 7 my $uctype = ucfirst($type);
104 7 my $package = "JSON::Karabiner::Manipulator::Conditions::" . $uctype;
105 7 eval "require $package";
106 7 my $condition = $package->new($type);
107 7 ▶ if (defined $s->{actions}{conditions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{'actions'}{'conditions'}) { } 5 2 7
108 5 push @{$s->{actions}{conditions}}, $condition;
109 } else {
110 2 $s->{actions}{conditions} = [ $condition ];
111 }
112 7 return $condition;
113 }
114  
115 sub add_description {
116 1 ▶ my ($s, $desc) = _get_args(@_);
Subroutine add_description: called CC=2 SCAR=7.1
Pod documented
117  
118 1 ▶ croak 'To add a description, you must provide one' if !$desc;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc 0 1 1
119 1 $s->{actions}{description} = $desc;
120 }
121  
122 sub add_parameter {
123 1 ▶ my ($s, $param, $value) = _get_args(@_);
Subroutine add_parameter: called CC=8 SCAR=27.2
Pod documented
124 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
125 1 ▶ croak 'To add a parameter, you must provide a value' if !$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 1 1
126  
127 1 my @acceptable_values = qw( to_if_alone_timeout_milliseconds
128 alone_timeout
129 alone
130 to_if_held_down_threshold_milliseconds
131 held_down_threshold
132 down_threshold
133 held_down
134 down
135 to_delayed_action_delay_milliseconds
136 delayed_action_delay
137 action_delay
138 delay
139 simultaneous_threshold_milliseconds
140 simultaneous_threshold
141 simultaneous
142 );
143  
144 1 my $param_exists = grep { $param eq $_ } @acceptable_values;
145 1 ▶ croak "'$param' in an unrecognzed parameter" unless $param_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $param_exists 0 1 1
146  
147 # get param full name
148 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
149 1 $param = 'to_if_alone_timeout_milliseconds';
150 } elsif ($param =~ /down/) {
151 0 $param = 'to_if_held_down_threshold_milliseconds';
152 } elsif ($param =~ /delay/) {
153 0 $param = 'to_delayed_action_delay_milliseconds';
154 } elsif ($param =~ /simultaneous/) {
155 0 $param = 'simultaneous_threshold_milliseconds';
156 }
157  
158 1 $s->{actions}{parameters}{"basic.$param"} = $value;
159 }
160  
161 sub TO_JSON {
162 26 ▶ my $obj = shift;
Subroutine TO_JSON: called CC=2 SCAR=6.9
Pod undocumented
163 #TODO: Change this under certain conditions
164 26 $obj->{actions}{type} = 'basic';
165 26 ▶ $obj->_do_validity_checks($obj->{actions}) unless $obj->{_disable_validity_tests};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"_disable_validity_tests"} 6 20 26
166 23 return $obj->{actions};
167 }
168  
169 sub _do_validity_checks {
170 6 ▶ my $s = shift;
Subroutine _do_validity_checks: called CC=1 SCAR=0.0
171 6 my $actions = shift;
172 6 my $from = $actions->{from};
173 6 $s->_do_from_validity_checks($from);
174 }
175  
176 sub _dump_json {
177 4 ▶ my $s = $main::current_manip;
Subroutine _dump_json: called CC=2 SCAR=6.9
178 4 my @kb_obj_args = @{$s->{_kb_obj_args}};
179 4 ▶ if (!@kb_obj_args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@kb_obj_args) 2 2 4
180 2 croak "The _dump_json method cannot be run on this manipulator.";
181 }
182  
183 2 require JSON::Karabiner;
184 2 my $little_title = shift @kb_obj_args;
185 2 unshift @kb_obj_args, 'SET WITH write_file METHOD';
186 2 my $kb_obj = JSON::Karabiner->new( @kb_obj_args );
187  
188 2 my $rule = $kb_obj->add_rule($little_title);
189 2 my $temp_manip = $rule->add_manipulator();
190 2 %{$temp_manip} = %{$s};
191 2 $kb_obj->_dump_json;
192 }
193  
194 sub write_file {
195 2 ▶ my $s = $main::current_manip;
Subroutine write_file: called CC=6 SCAR=18.3
Pod undocumented
196 2 my $title = shift;
197 2 my @kb_obj_args = @{$s->{_kb_obj_args}};
198 2 ▶ if (!@kb_obj_args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@kb_obj_args) 0 2 2
199 0 croak "The _write_file method cannot be run on this manipulator.";
200 }
201  
202 2 ▶ croak 'You must supply a title for the first manipulator' if !$title && !$main::file_written;
Branch: 1 branch
BranchTrueFalseTotal
unless $title or $main::file_written 0 2 2
Condition33%1/3
$title or $main::file_written
l!l&&r!l&&!r
200
MC/DC0%0/2
$title or $main::file_written
$title $main::file_written
Truth table33%1/3
A or B
A= $titleB= $main::file_written
ABresult
1X1
011
000
203 2 ▶ if (!$title) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($title) 0 2 2
204 0 $title = $main::file_title_written;
205 }
206  
207 2 require JSON::Karabiner;
208 2 my $little_title = shift @kb_obj_args;
209 2 unshift @kb_obj_args, $title;
210 2 my $kb_obj = JSON::Karabiner->new( @kb_obj_args );
211 2 ▶ if ($s->{_fake_write_flag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"_fake_write_flag"}) 1 1 2
212 1 $kb_obj->{_fake_write_flag} = 1;
213 }
214 2 my $rule = $kb_obj->add_rule($little_title);
215 2 my $temp_manip = $rule->add_manipulator();
216 2 %{$temp_manip} = %{$s};
217 2 $kb_obj->write_file(1);
218 1 $kb_obj->{_fake_write_flag} = 0;
219 {
220 17 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 1 $main::file_written = $kb_obj_args[1];
222 1 $main::file_title_written = $kb_obj_args[0];
223 }
224  
225 }
226  
227 sub _fake_write_file {
228 3 ▶ my $s = $main::current_manip;
Subroutine _fake_write_file: called CC=2 SCAR=6.9
229 3 my $title = shift;
230  
231 3 my @kb_obj_args = @{$s->{_kb_obj_args}};
232 3 ▶ if (!@kb_obj_args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@kb_obj_args) 2 1 3
233 2 croak "The _fake_write method cannot be run on this manipulator.";
234 }
235  
236 1 $s->{_fake_write_flag} = 1;
237 1 $s->write_file($title);
238 1 $s->{_fake_write_flag} = 0;
239 }
240  
241 sub _do_from_validity_checks {
242 6 ▶ my $s = shift;
Subroutine _do_from_validity_checks: called CC=3 SCAR=11.0
243 6 my $from = shift;
244  
245 6 ▶ if (! defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from) 2 4 6
246 2 croak "No 'from' action found in the manipulator. You must add a 'from' action.'";
247 }
248  
249 4 ▶ if (! %$from) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$from) 1 3 4
250 1 croak "The 'from' action is empty. Perform methods on the 'from' action to tell it how to behave.";
251 }
252  
253 3 return;
254  
255 # my @from_keys = keys %$from;
256 # my $has_key_code = grep { $_ =~ /^any|consumer_key_code|key_code$/ } @from_keys;
257 # print Dumper \@from_keys;
258 # if (!$has_key_code && grep { $_ =~ /modifiers/ } @from_keys) {
259 # print Dumper 'aksjdkajsdf';
260 # croak "You cannot have modifiers without anything to modify in a 'from' action.";
261 # }
262 }
263  
264 # ABSTRACT: manipulator object for containing and outputting its data
265  
266 17 1;
267  
268 __END__
« Karabiner.pm ↑ Summary Karabiner/Manipulator/Conditions/Input_source_if.pm »