File Coverage

blib/lib/JSON/Karabiner.pm
Criterion Covered Total %
statement 85 90 94.4
branch 21 26 80.7
condition 2 3 66.6
subroutine 15 16 93.7
pod 1 3 33.3
total 124 138 89.8


line stmt bran cond sub pod time code
1             package JSON::Karabiner ;
2             $JSON::Karabiner::VERSION = '0.016';
3 18     18   1940839 use strict;
  18         188  
  18         623  
4 18     18   110 use warnings;
  18         32  
  18         485  
5 18     18   10529 use JSON;
  18         170254  
  18         97  
6 18     18   2120 use Carp;
  18         38  
  18         964  
7 18     18   9866 use File::HomeDir;
  18         98295  
  18         1062  
8 18     18   7751 use JSON::Karabiner::Rule;
  18         47  
  18         3788  
9              
10             sub new {
11 37     37 0 28866 my $class = shift;
12 37         74 my $title = shift;
13 37         68 my $file = shift;
14 37         63 my $opts = shift;
15              
16 37 100       118 if ($opts) {
17 4 50       17 if (ref $opts ne 'HASH') {
18 0         0 croak 'Options must be passed as a hash reference.';
19             }
20             }
21 37 100       124 croak 'JSON::Karabiner constructor requires a title for the modification.' if !$title;
22 35 100       94 croak 'JSON::Karabiner constructor requires a file name.' if !$file;
23 34 100       194 croak 'File names are required to have a .json extenstion' if $file !~ /\.json$/;
24 33         182 my $home = File::HomeDir->my_home;
25             my $self = {
26             _file => $file,
27 33   66     1735 _mod_file_dir => $opts->{mod_file_dir} || "$home/.config/karabiner/assets/complex_modifications/",
28             _karabiner => { title => $title, rules => [] },
29             _fake_write_flag => 0,
30             _rule_obj => '',
31             };
32 33 100       472 if (!-d $self->{_mod_file_dir}) {
33 31 100       121 if ($opts->{mod_file_dir}) {
34 2         33 croak "The directory you attempted to set with the 'mod_file_dir' option does not exist.\n\n";
35             } else {
36 29 50       132 croak "The default directory for storing complex modifications does not exist. Do you have Karabiner-Elements installed? Is it installed with a non-standard configuration? Try setting the location of the directory manually with the 'mod_file_dir' option. Consult this module's documentation for more information with using the 'perldoc JSON::Karabiner' command in the terminal.\n\n" unless $ENV{HARNESS_ACTIVE};
37             }
38             }
39 31         78 bless $self, $class;
40 18     18   190 { no warnings 'once'; $main::has_delayed_action = ''; }
  18         72  
  18         10812  
  31         50  
  31         74  
41 31         145 return $self;
42             }
43              
44             # used by test scripts
45             sub _fake_write_file {
46 3     3   1158 my $s = shift;
47 3         6 $s->{_fake_write_flag} = 1;
48 3         10 $s->write_file;
49 0         0 $s->{_fake_write_flag} = 0;
50             }
51              
52             sub write_file {
53 6     6 1 11 my $s = shift;
54 6         20 my $using_dsl = ((caller(0))[0] eq 'JSON::Karabiner::Manipulator');
55 6         209 my $file = $s->{_file};
56 6         12 my $dir = $s->{_mod_file_dir};
57 6         12 my $destination = $dir . $file;
58              
59 6 100       20 if ($using_dsl) {
60 3         6 my $rule = $s->{_karabiner}{rules};
61 3 100       8 if (!@main::saved_manips) {
62 2         3 @main::saved_manips = ();
63             }
64 3         7 foreach my $r (@$rule) {
65 3         6 foreach my $manip ($r->{manipulators}) {
66 3         3 push @main::saved_manips, @{$manip};
  3         8  
67             }
68             }
69              
70 3         5 @{$s->{_karabiner}->{rules}[0]{manipulators}} = @main::saved_manips;
  3         18  
71             }
72 6         14 my $json = $s->_get_json();
73              
74             #TODO ensure it works with utf8
75 3 50       11 if (!$s->{_fake_write_flag}) {
76 0 0       0 open (FH, '>', $destination) or die 'Could not open file for writing.';
77 0         0 print FH $json;
78 0         0 close FH;
79             }
80              
81 3         90 print "Your rules were successfully written to:\n\n $destination.\n\nOpen Karabiner-Elements to import the new rules you have generated.\n\nIf your rules do not appear, please report the issue to our issue queue:\n\nhttps://github.com/sdondley/JSON-Karabiner/issues \n\n"
82             }
83              
84             sub _get_json {
85 6     6   10 my $s = shift;
86 6         48 my $json = JSON->new();
87 6         26 $json = $json->convert_blessed();
88 6         69 return $json->pretty->encode($s->{_karabiner});
89             }
90              
91             sub add_rule {
92 30     30 0 2086 my $s = shift;
93 30         51 my $desc = shift;
94 30 100       102 croak "No description passed to rule." if !$desc;
95 28         164 my $rule = JSON::Karabiner::Rule->new($desc);
96 28         88 $s->_add_rule($rule);
97 28         59 $s->{_rule_object} = $rule;
98 28         85 return $rule;
99             }
100              
101             sub _add_rule {
102 28     28   103 my $s = shift;
103 28         47 my $rule = shift;
104 28         47 push @{$s->{_karabiner}{rules}}, $rule;
  28         128  
105             }
106              
107       0     sub _check_if_file_exits {
108              
109             }
110              
111             sub _dump_json {
112 22     22   6838 my $s = shift;
113 22         233 my $json = JSON->new();
114 22         144 $json = $json->convert_blessed();
115              
116             # suppress validity tests
117 22         127 $s->{_rule_object}->_disable_validity_tests();
118              
119 18     18   828 use Data::Dumper qw(Dumper);
  18         7153  
  18         2030  
120 22         352 print Dumper $json->pretty->encode($s->{_karabiner});
121              
122             # renable validity tests
123 22         3372 $s->{_rule_object}->_enable_validity_tests();
124             }
125              
126              
127             # ABSTRACT: easy JSON code generation for Karabiner-Elements
128              
129             1;
130              
131             __END__