File Coverage

blib/lib/App/TodoList.pm
Criterion Covered Total %
statement 55 55 100.0
branch 9 12 75.0
condition 5 9 55.5
subroutine 12 12 100.0
pod 0 5 0.0
total 81 93 87.1


line stmt bran cond sub pod time code
1             package App::TodoList;
2              
3 2     2   477926 use strict;
  2         3  
  2         79  
4 2     2   9 use warnings;
  2         3  
  2         131  
5              
6 2     2   1420 use JSON;
  2         26184  
  2         12  
7 2     2   1456 use File::HomeDir;
  2         11966  
  2         174  
8 2     2   15 use File::Spec;
  2         3  
  2         1206  
9              
10             our $VERSION = '1.0.0';
11              
12             sub new {
13 7     7 0 239219 my ($class, %args) = @_;
14              
15 7         57 my $home_dir = File::HomeDir->my_home;
16 7         457 my $file = File::Spec->catfile($home_dir, '.tasks.json');
17              
18             my $self = {
19 7   33     63 file => $args{file} || $file,
20             tasks => [],
21             };
22              
23 7         19 bless $self, $class;
24              
25 7         33 $self->_load_tasks();
26 7         36 return $self;
27             }
28              
29             sub add_task {
30 6     6 0 1332 my ($self, $task) = @_;
31 6         10 push @{ $self->{tasks} }, { task => $task, completed => 0 };
  6         64  
32 6         21 $self->_save_tasks();
33 6         22 return scalar @{ $self->{tasks} };
  6         39  
34             }
35              
36             sub list_tasks {
37 4     4 0 22 my ($self) = @_;
38 4         12 return @{ $self->{tasks} };
  4         16  
39             }
40              
41             sub complete_task {
42 2     2 0 20 my ($self, $index) = @_;
43 2 100 66     11 return 0 if $index < 1 || $index > scalar @{ $self->{tasks} };
  2         47  
44 1         5 $self->{tasks}->[$index - 1]->{completed} = 1;
45 1         4 $self->_save_tasks();
46 1         10 return 1;
47             }
48              
49             sub delete_task {
50 2     2 0 13 my ($self, $index) = @_;
51 2 100 66     13 return 0 if $index < 1 || $index > scalar @{ $self->{tasks} };
  2         15  
52 1         3 splice @{ $self->{tasks} }, $index - 1, 1;
  1         4  
53 1         6 $self->_save_tasks();
54 1         10 return 1;
55             }
56              
57             sub _load_tasks {
58 7     7   36 my ($self) = @_;
59 7 50       114 if (-e $self->{file}) {
60 7 50       275 open my $fh, '<', $self->{file} or die "Could not open file '$self->{file}': $!";
61 7         37 local $/;
62 7         241 my $json = <$fh>;
63 7         84 close $fh;
64 7 100       98 $self->{tasks} = decode_json($json) if $json;
65             }
66             }
67              
68             sub _save_tasks {
69 8     8   18 my ($self) = @_;
70 8 50       747 open my $fh, '>', $self->{file} or die "Could not open file '$self->{file}': $!";
71 8         123 print $fh encode_json($self->{tasks});
72 8         1119 close $fh;
73             }
74              
75             1;
76             __END__