File Coverage

blib/lib/Insight/Scriptures.pm
Criterion Covered Total %
statement 65 126 51.5
branch 10 38 26.3
condition n/a
subroutine 13 19 68.4
pod 0 14 0.0
total 88 197 44.6


line stmt bran cond sub pod time code
1             package Insight::Scriptures;
2 2     2   135817 use 5.006; use strict; use warnings;
  2     2   17  
  2     2   11  
  2         4  
  2         60  
  2         10  
  2         4  
  2         90  
3             our $VERSION = '0.02';
4 2     2   991 use JSON::Lines;
  2         64046  
  2         21  
5 2     2   78 use JSON;
  2         4  
  2         9  
6              
7             sub new {
8 1     1 0 133 my ($pkg, $args) = @_;
9             my $self = bless {
10             scriptures => undef,
11             scripture => undef,
12             scripture_file => undef,
13             index => -1,
14             jsonl => JSON::Lines->new(),
15             json => JSON->new->pretty(1)->canonical,
16             directory => 'directory',
17 1 50       10 %{ $args || {} },
  1         66  
18             }, $pkg;
19 1         8 $self->{scriptures} = $self->directory($self->{directory});
20 1 50       4 $self->scripture($self->{scripture}) if ($self->{scripture});
21 1         4 return $self;
22             }
23              
24             sub architect {
25 0     0 0 0 my ($self) = @_;
26              
27 0         0 while (1) {
28 0         0 print "insight: ";
29 0         0 my $in = ;
30 0         0 chomp($in);
31              
32 0         0 my %options = (
33             abolish => 1,
34             current => 1,
35             next => 1,
36             previous => 1,
37             start => 1,
38             end => 1,
39             salvage => 1,
40             );
41              
42 0 0       0 if ($in eq 'help') {
    0          
    0          
    0          
    0          
    0          
43 0         0 print qq|
44             Welcome to Insight::Scriptures architect. This is a small module to assist in writing your own scriptures.
45              
46             Help:
47             scripture \$scripture - loads the file containing the scripture lines
48             feather \$text - extends the scripture from the current index
49             abolish - deletes the current index from the scripture
50             current - print the line for the current index
51             next - move to the next index of the scripture and print the line
52             previous - move to the previous index of the scripture and print line
53             salvage - saves the scripture
54             exit - quits the command line application
55              
56             |;
57             } elsif ($options{$in}) {
58 0 0       0 if ($in eq 'salvage') {
59 0         0 $self->$in();
60 0         0 print qq|Scripture salvaged\n|;
61             } else {
62 0         0 my $enc = $self->{json}->encode($self->$in());
63 0         0 print $self->{index} . ": " . $enc;
64             }
65             } elsif ($in =~ m/scripture (.*)/) {
66 0         0 $self->scripture($1);
67 0         0 print qq|Scripture $1 read into memory\n|;
68             } elsif ($in =~ m/chapter (.*)/) {
69 0         0 $self->feather( chapter => 'scripture', text => $1 );
70 0         0 print qq|Chapter added to the scripture\n|;
71             } elsif ($in =~ m/feather (.*)/) {
72 0         0 $self->feather( text => $1 );
73 0         0 print qq|Text added to the scripture\n|;
74             } elsif ($in eq 'exit') {
75 0         0 last;
76             } else {
77 0         0 print qq|Unknown options\n|;
78             }
79             }
80             }
81              
82             sub compose {
83 0     0 0 0 my ($self, %args) = @_;
84            
85 0 0       0 if (! $self->{scripture} ) {
86 0         0 die "Please select a scripture first before composing";
87             }
88              
89 0         0 my ($chapter, $verse) = (0, 0);
90              
91 0         0 my $scripture = "";
92 0         0 for (@{ $self->{scripture} }) {
  0         0  
93 0 0       0 if ($_->{chapter}) {
94 0         0 $chapter++;
95 0         0 $scripture .= sprintf "Chapter %s - %s\n", $chapter, $_->{text};
96             } else {
97 0         0 $verse++;
98 0         0 $scripture .= sprintf "%s:%s %s\n", $chapter, $verse, $_->{text};
99             }
100             }
101              
102 0         0 print $scripture;
103             }
104              
105             sub directory {
106 1     1 0 3 my ($self, $path) = @_;
107              
108 1 50       21 mkdir $path if (! -d $path);
109              
110 1         6 my (@scriptures);
111 1 50       59 opendir my $dh, $path or die "Cannot open directory for reading: $!";
112 1         48 for (readdir $dh) {
113 5 100       17 next if $_ =~ m/^\./;
114 3         14 (my $scripture = $_) =~ s/\..*$//;
115 3         13 push @scriptures, [$scripture, "$path/$_"]
116             }
117 1         39 closedir $dh;
118 1         9 return \@scriptures;
119             }
120              
121             sub scriptures {
122 0     0 0 0 my ($self) = @_;
123 0         0 my @scriptures;
124 0         0 for (@{ $self->{scriptures} }) {
  0         0  
125 0         0 push @scriptures, $_->[0];
126             }
127 0         0 return \@scriptures;
128             }
129              
130             sub scripture {
131 1     1 0 9 my ($self, $name) = @_;
132 1         1 my $scripture;
133 1         3 for (@{ $self->{scriptures} }) {
  1         12  
134 1 50       22 if ($_->[0] =~ m/^$name$/) {
135 1         3 $scripture = $_;
136 1         3 last;
137             }
138             }
139              
140 1 50       5 if (! $scripture ) {
141 0         0 my $file = sprintf "%s/%s.lines", $self->{directory}, $name;
142 0 0       0 open my $fh, '>', $file or die 'Cannot open file for writing';
143 0         0 print $fh "";
144 0         0 close $fh;
145 0         0 $scripture = [ $name, $file ];
146 0         0 push @{ $self->{scriptures} }, $scripture;
  0         0  
147 0         0 $self->{scripture} = [];
148             }
149              
150 1         3 $self->{scripture_file} = $scripture;
151              
152 1         6 $scripture = $self->{jsonl}->decode_file($scripture->[-1]);
153              
154 1         215 $self->{scripture} = $scripture;
155 1         3 $self->{index} = -1;
156              
157 1         2 return $scripture;
158             }
159              
160             sub feather {
161 2     2 0 7 my ($self, %args) = @_;
162              
163 2         5 $self->{index}++;
164              
165 2 50       6 if ($args{chapter}) {
166 0         0 splice @{ $self->{scripture} }, $self->{index}, 0, \%args;
  0         0  
167 0         0 return;
168             }
169              
170 2         8 my %line = (
171             time => time,
172             %args,
173             );
174            
175 2         3 splice @{ $self->{scripture} }, $self->{index}, 0, \%line;
  2         6  
176              
177 2         7 return \%line;
178             }
179              
180             sub salvage {
181 1     1 0 3 my ($self) = @_;
182 1         7 $self->{jsonl}->encode_file($self->{scripture_file}->[-1], $self->{scripture});
183 1         361 return $self;
184             }
185              
186             sub abolish {
187 2     2 0 5 my ($self) = @_;
188 2         4 splice @{ $self->{scripture} }, $self->{index}, 1;
  2         4  
189 2         14 $self->{index}--;
190 2         6 return $self->current();
191             }
192              
193             sub current {
194 2     2 0 4 my ($self) = @_;
195 2 50       7 $self->{index} = 0 if ($self->{index} < 0);
196 2         8 return $self->{scripture}->[$self->{index}];
197             }
198              
199             sub next {
200 1     1 0 6 my ($self) = @_;
201 1         2 $self->{index}++;
202 1         3 my $next = $self->{scripture}->[$self->{index}];
203 1         8 return $next;
204             }
205              
206             sub previous {
207 0     0 0   my ($self) = @_;
208 0           $self->{index}--;
209 0           return $self->{scripture}->[$self->{index}];
210             }
211              
212             sub start {
213 0     0 0   my ($self) = @_;
214 0           $self->{index} = 0;
215 0           return $self->{scripture}->[$self->{index}];
216             }
217              
218             sub end {
219 0     0 0   my ($self) = @_;
220 0           $self->{index} = scalar @{ $self->{scripture} } - 1;
  0            
221 0           return $self->{scripture}->[$self->{index}];
222             }
223              
224             1;
225              
226             __END__