blib/lib/App/WRT/EntryStore.pm

filter: Statementstmt 13.9% 27 / 194 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 17.0% 7 / 41 Podpod 90.9% 30 / 33 totaltotal 19.7% 64 / 324 CC 32 SCAR 26.1
CC
32
Cov
11%
__ANON__
56.0
new
30.0
dates_by_depth
20.0
CRAP 13.5
« blib/lib/App/WRT/Markup.pm ↑ Summary blib/lib/App/WRT/Util.pm »
1 package App::WRT::EntryStore;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use App::WRT::Sort qw(sort_entries);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use App::WRT::Util qw(file_get_contents);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =pod
13  
14 =head1 NAME
15  
16 App::WRT::EntryStore - model the contents of a wrt repo's entry_dir
17  
18 =head1 SYNOPSIS
19  
20 use App::WRT::EntryStore;
21 my $entries = App::WRT::EntryStore->new('./archives');
22  
23 my @all = $entries->all();
24 my @months = $entries->all_months();
25 my @years = $entries->all_years();
26 my @days = $entries->all_days();
27  
28 # all_* are wrappers for dates_by_depth():
29 my @days = $entries->dates_by_depth(
30 3 # 1 for years, 2 for months, 3 for days
31 );
32  
33 my @recent_days = $entries->recent_days(30);
34 my @recent_months = $entries->recent_months(12);
35 my @recent_years = $entries->recent_years(10);
36  
37 # recent_* are wrappers for recent_by_depth():
38 my @recent_days $entries->recent_by_depth(
39 3, # 1 for years, 2 for months, 3 for days
40 30 # count
41 );
42  
43 =cut
44  
45 # "Constants"
46  
47 9 my $ENTRYTYPE_FILE = 0;
48 9 my $ENTRYTYPE_DIR = 1;
49 9 my $ENTRYTYPE_VIRT = 2;
50  
51 9 my %SUBENTRY_IGNORE = ('index' => 1);
52 9 my $SUBENTRY_EXPR = qr{
53 ^
54 [[:lower:][:digit:]_-]+
55 (
56 [.]
57 (tgz|zip|tar[.]gz|gz|txt)
58 )?
59 $
60 }x;
61  
62 # What gets considered a renderable entry path:
63 9 my $RENDERABLE_EXPR = qr{
64 ^
65 (
66 [[:lower:][:digit:]_\/-]+
67 )
68 $
69 }x;
70  
71 =head1 METHODS
72  
73 =over
74  
75 =item new($class, $entry_dir)
76  
77 Get a new EntryStore, using a given $entry_dir.
78  
79 Finds a list of entries for the given directory, and builds data structures
80 which can be used to index into entries by depth, property, and next/previous
81 entry.
82  
83 =cut
84  
85 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
86 0 my $class = shift;
87 0 my ($entry_dir) = @_;
88  
89 0 my %params = (
90 entry_dir => $entry_dir
91 );
92  
93 0 my $self = \%params;
94  
95 0 bless $self, $class;
96  
97 0 my @entries;
98 my %source_files;
99 0 my %entry_properties;
100 0 my %property_entries;
101 0 my %children;
102  
103 find(
104 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
105 0 ▶ return unless $File::Find::name =~ m{^ \Q$entry_dir\E / (.*) $}x;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Find::name =~ m[^ \Q$entry_dir\E / (.*) $]x 0 0 0
106  
107 0 my $target = $1;
108  
109 # Build an ordered array of entries:
110 0 push @entries, $target;
111  
112 # Build a hash indicating:
113 # a. that a file exists
114 # b. whether it's a flatfile or a directory
115 0 ▶ if (-f $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $_) { } 0 0 0
elsif (-d $_) { } 0 0 0
116 0 $source_files{$target} = $ENTRYTYPE_FILE;
117 } elsif (-d $_) {
118 0 $source_files{$target} = $ENTRYTYPE_DIR;
119 }
120  
121 # Build hashes of all properties of entries, and all entries of properties:
122 0 ▶ if ($target =~ m{(.*) / (.*) [.]prop $}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ m[(.*) / (.*) [.]prop $]x) 0 0 0
123 0 my ($entry, $property) = ($1, $2);
124  
125 0 ▶ $entry_properties{$entry} //= [];
Condition0%0/2
$entry_properties{$entry} //= []
l!l
00
MC/DC0%0/1
$entry_properties{$entry} //= []
$entry_properties{$entry}
Truth table0%0/2
A
A= $entry_properties{$entry}
Aresult
11
00
126 0 push @{ $entry_properties{$entry} }, $property;
127  
128 0 ▶ $property_entries{$property} //= [];
Condition0%0/2
$property_entries{$property} //= []
l!l
00
MC/DC0%0/1
$property_entries{$property} //= []
$property_entries{$property}
Truth table0%0/2
A
A= $property_entries{$property}
Aresult
11
00
129 0 push @{ $property_entries{$property} }, $entry;
130 }
131 },
132 0 $entry_dir
133 );
134  
135 # Ensure that the entry list for every property is sorted:
136 0 for (keys %property_entries) {
137 0 $property_entries{$_} = [ sort_entries(@{ $property_entries{$_} }) ];
138 }
139  
140 # Create virtual entries based on tags, _if there's not already a file
141 # there_:
142 0 foreach my $prop (keys %property_entries) {
143 0 ▶ if ( $prop =~ m/^ tag[.] (.*)$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop =~ /^ tag[.] (.*)$/x) 0 0 0
144 0 my $tag = $1;
145 0 $tag =~ s{[.]}{/}g;
146 0 ▶ unless (defined $source_files{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $source_files{$tag}) 0 0 0
147 0 push @entries, $tag;
148 0 $source_files{$tag} = $ENTRYTYPE_VIRT;
149 }
150 }
151 }
152  
153 # Stash refs for future use:
154 0 $self->{entries} = \@entries;
155 0 $self->{source_files} = \%source_files;
156 0 $self->{property_entries} = \%property_entries;
157 0 $self->{entry_properties} = \%entry_properties;
158  
159 0 $self->generate_date_hashes();
160 0 $self->store_children();
161  
162 0 return $self;
163 }
164  
165 =item all()
166  
167 Returns a list of all source files for the current entry archive (excepting
168 index files, which are a special case - this part could use some work).
169  
170 This was originally in App::WRT::Renderer, so there may be some pitfalls here.
171  
172 =cut
173  
174 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=6.9
Pod documented
175 0 my ($self) = shift;
176 0 return @{ $self->{entries} };
177 }
178  
179 =item all_renderable()
180  
181 Returns a list of all existing source paths which are considered "renderable".
182  
183 A path should match C<$RENDERABLE_EXPR> and not be an index file.
184  
185 =cut
186  
187 0 ▶sub all_renderable() {
Subroutine all_renderable: not called CC=2 SCAR=17.9
Pod documented
188 0 my ($self) = shift;
189 return grep {
190 0 ▶ (index($_, '/index', -6) == -1)
Condition0%0/3
(index($_, '/index', -6) == -1) && /$RENDERABLE_EXPR/
!ll&&!rl&&r
000
MC/DC0%0/2
(index($_, '/index', -6) == -1) && /$RENDERABLE_EXPR/
(index($_, '/index', -6) == -1) /$RENDERABLE_EXPR/
Truth table0%0/3
A && B
A= (index($_, '/index', -6) == -1)B= /$RENDERABLE_EXPR/
ABresult
0X0
100
111
191 &&
192 m/$RENDERABLE_EXPR/
193 0 } @{ $self->{entries} };
194 }
195  
196 =item dates_by_depth($depth)
197  
198 Returns a sorted list of all date-like entries which are at a specified depth.
199 Use 1 for years, 2 for months, and 3 for days.
200  
201 Fairly silly, but entertaining in its perverse way. all_years(), all_months(),
202 and all_days() are provided for convenience.
203  
204 =cut
205  
206 0 ▶sub dates_by_depth {
Subroutine dates_by_depth: not called CC=4 SCAR=30.0
Pod documented
207 0 my ($self) = shift;
208 0 my ($depth) = @_;
209  
210 0 ▶ croak('No $depth given.')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 0 0
211 unless defined $depth;
212  
213 # Check if we already have a value cached:
214 0 return @{ $self->{by_depth}->{$depth} }
215 0 ▶ if defined $self->{by_depth}->{$depth};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"by_depth"}{$depth} 0 0 0
216  
217 # Build a pattern for matching the given depth of date-like entries. For
218 # example, a day would be depth 3, and matched by \d+/\d+/\d+
219 0 my @particles;
220 0 ▶ for (my $i = 0; $i < $depth; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $depth) 0 0 0
221 0 push @particles, '\d+';
222 }
223 0 my $pattern = join '/', @particles;
224  
225 0 my @by_depth = sort_entries(
226 grep m{^ $pattern $}x, $self->all()
227 );
228  
229 # Stash arrayref for future use:
230 0 $self->{by_depth}->{$depth} = \@by_depth;
231  
232 0 return @by_depth;
233 }
234  
235 =item all_years(), all_months(), all_days()
236  
237 Convenience wrappers for dates_by_depth().
238  
239 =cut
240  
241 0 ▶sub all_years { return $_[0]->dates_by_depth(1); }
Subroutine all_years: not called CC=1 SCAR=6.9
Pod documented
242 0 ▶sub all_months { return $_[0]->dates_by_depth(2); }
Subroutine all_months: not called CC=1 SCAR=6.9
Pod documented
243 0 ▶sub all_days { return $_[0]->dates_by_depth(3); }
Subroutine all_days: not called CC=1 SCAR=6.9
Pod documented
244  
245 =item days_for($month), months_for($year)
246  
247 Convenience wrappers for extracting days or months in a given month
248 or year.
249  
250 =cut
251  
252 0 ▶sub days_for {
Subroutine days_for: not called CC=1 SCAR=6.9
Pod documented
253 0 my ($self, $container) = @_;
254 0 return grep { m{^ \Q$container\E / }x } $self->all_days();
255 }
256  
257 0 ▶sub months_for {
Subroutine months_for: not called CC=1 SCAR=6.9
Pod documented
258 0 my ($self, $year) = @_;
259 0 return grep { m{^ \Q$year\E / }x } $self->all_months();
260 }
261  
262 =item recent_by_depth($depth, $entry_count)
263  
264 Returns the $entry_count most recent dated entries at $depth (1 for year, 2 for
265 month, 3 for day). recent_years(), recent_months(), and recent_days() are
266 provided for convenience.
267  
268 =cut
269  
270 0 ▶sub recent_by_depth {
Subroutine recent_by_depth: not called CC=3 SCAR=24.8
Pod documented
271 0 my ($self) = shift;
272 0 my ($depth, $entry_count) = @_;
273  
274 0 my @entries;
275 0 for my $entry (reverse $self->dates_by_depth($depth)) {
276 0 ▶ last if scalar(@entries) == $entry_count;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @entries == $entry_count 0 0 0
277 0 push @entries, $entry;
278 }
279  
280 0 return @entries;
281 }
282  
283 =item all_years(), all_months(), all_days()
284  
285 Convenience wrappers for recent_by_depth().
286  
287 =cut
288  
289 0 ▶sub recent_years { return $_[0]->recent_by_depth(1, $_[1]); }
Subroutine recent_years: not called CC=1 SCAR=6.9
Pod undocumented
290 0 ▶sub recent_months { return $_[0]->recent_by_depth(2, $_[1]); }
Subroutine recent_months: not called CC=1 SCAR=6.9
Pod undocumented
291 0 ▶sub recent_days { return $_[0]->recent_by_depth(3, $_[1]); }
Subroutine recent_days: not called CC=1 SCAR=6.9
Pod undocumented
292  
293 =item generate_date_hashes()
294  
295 Store hashes which map dated entries to their previous and next entries at the
296 same depth in the tree. That is, something like:
297  
298 %prev_dates = {
299 '2014' => '2013',
300 '2014/1' => '2013/12'
301 '2014/1/1' => '2013/12/30',
302 ...
303 }
304  
305 %next_dates = {
306 '2013' => '2014',
307 '2013/12' => '2014/1',
308 '2013/12/30' => '2014/1/1',
309 ...
310 }
311  
312 =cut
313  
314 0 ▶sub generate_date_hashes {
Subroutine generate_date_hashes: not called CC=4 SCAR=30.0
Pod documented
315 0 my $self = shift;
316  
317 0 my %prev;
318  
319 # Depth 1 is years, 2 is months, 3 is days. Get lists for all:
320 0 for my $depth (1, 2, 3) {
321 0 my @dates = $self->dates_by_depth($depth);
322  
323 0 my $last_seen;
324 0 foreach my $current_date (@dates) {
325 0 ▶ if ($last_seen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_seen) 0 0 0
326 0 $prev{$current_date} = $last_seen;
327 }
328 0 $last_seen = $current_date;
329 }
330 }
331  
332 0 $self->{prev_dates} = { %prev };
333 0 $self->{next_dates} = { reverse %prev };
334 }
335  
336 =item store_children
337  
338 Store hashes of arrayrefs which maps parents to their immediate children.
339  
340 =cut
341  
342 0 ▶sub store_children {
Subroutine store_children: not called CC=3 SCAR=24.8
Pod documented
343 0 my $self = shift;
344  
345 0 my %child_cache;
346  
347 0 for my $entry ($self->all()) {
348 0 my $dirname = $self->dirname($entry);
349 0 ▶ $child_cache{$dirname} //= [ ];
Condition0%0/2
$child_cache{$dirname} //= []
l!l
00
MC/DC0%0/1
$child_cache{$dirname} //= []
$child_cache{$dirname}
Truth table0%0/2
A
A= $child_cache{$dirname}
Aresult
11
00
350 0 push @{ $child_cache{$dirname} }, $entry;
351 }
352  
353 0 $self->{child_cache} = { %child_cache };
354 }
355  
356 =item parent($entry)
357  
358 Return an entry's parent, or undef if it's at the top level.
359  
360 =cut
361  
362 0 ▶sub parent {
Subroutine parent: not called CC=3 SCAR=24.8
Pod documented
363 0 my $self = shift;
364 0 my ($entry) = @_;
365  
366 # Explode unless an entry actually exists in the archives:
367 0 ▶ croak("No such entry: $entry") unless $self->is_extant($entry);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_extant($entry) 0 0 0
368  
369 0 my (@components) = split '/', $entry;
370 0 pop @components;
371 0 ▶ if (@components) {
Branch: 1 branch
BranchTrueFalseTotal
if (@components) 0 0 0
372 0 return join '/', @components;
373 }
374 0 return undef;
375 }
376  
377 =item children($entry)
378  
379 Return an entry's (immediate) children, if any.
380  
381 =cut
382  
383 0 ▶sub children {
Subroutine children: not called CC=3 SCAR=24.8
Pod documented
384 0 my $self = shift;
385 0 my ($entry) = @_;
386  
387 # Explode unless an entry actually exists in the archives:
388 0 ▶ croak("No such entry: $entry") unless $self->is_extant($entry);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_extant($entry) 0 0 0
389  
390 0 ▶ if (defined $self->{child_cache}{$entry}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"child_cache"}{$entry}) 0 0 0
391 0 return @{ $self->{child_cache}{$entry} };
392 }
393 0 return ();
394 }
395  
396 =item children_basenames($entry)
397  
398 Returns an entry's immediate children, but just basenames - not full paths.
399  
400 =cut
401  
402 0 ▶sub children_basenames {
Subroutine children_basenames: not called CC=1 SCAR=6.9
Pod documented
403 0 my $self = shift;
404 0 my ($entry) = @_;
405  
406 0 return map { $self->basename($_) } $self->children($entry);
407 }
408  
409 =item get_sub_entries($entry_loc)
410  
411 Returns "sub entries" based on the C<SUBENTRY_EXPR> regexp.
412  
413 =cut
414  
415 0 ▶sub get_sub_entries {
Subroutine get_sub_entries: not called CC=1 SCAR=6.9
Pod documented
416 0 my ($self, $entry) = @_;
417  
418 # index gets special treatment as the text body of an entry, rather
419 # than as a sub-entry:
420 0 my @subs = grep { m/$SUBENTRY_EXPR/ } $self->children_basenames($entry);
421 0 return sort grep { ! $SUBENTRY_IGNORE{$_} } @subs;
422  
423 # return grep { ! $SUBENTRY_IGNORE{$_} }
424 # grep { m/$SUBENTRY_EXPR/ }
425 # $self->children_basenames($entry);
426 }
427  
428 =item previous($entry)
429  
430 Return the previous entry at the same depth for the given entry.
431  
432 =cut
433  
434 0 ▶sub previous {
Subroutine previous: not called CC=1 SCAR=6.9
Pod documented
435 0 return $_[0]->{prev_dates}->{ $_[1] };
436 }
437  
438 =item next($entry)
439  
440 Return the next entry at the same depth for the given entry.
441  
442 =cut
443  
444 0 ▶sub next {
Subroutine next: not called CC=1 SCAR=6.9
Pod documented
445 0 return $_[0]->{next_dates}->{ $_[1] };
446 }
447  
448 =item by_prop($property)
449  
450 Return an array of any entries for the given property.
451  
452 =cut
453  
454 0 ▶sub by_prop {
Subroutine by_prop: not called CC=2 SCAR=17.9
Pod documented
455 0 my ($self, $property) = @_;
456  
457 0 my @entries;
458 0 ▶ if (defined $self->{property_entries}{$property}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"property_entries"}{$property}) 0 0 0
459 0 @entries = @{ $self->{property_entries}{$property} };
460 }
461  
462 0 return @entries;
463 }
464  
465 =item props_for($entry)
466  
467 Return an array of any properties for the given entry.
468  
469 =cut
470  
471 0 ▶sub props_for {
Subroutine props_for: not called CC=2 SCAR=17.9
Pod documented
472 0 my ($self, $entry) = @_;
473  
474 0 my @props;
475 0 ▶ if (defined $self->{entry_properties}{$entry}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"entry_properties"}{$entry}) 0 0 0
476 0 @props = @{ $self->{entry_properties}{$entry} };
477 }
478  
479 0 return @props;
480 }
481  
482 =item has_prop($entry, $prop)
483  
484 Return 1 if the given entry has the given property.
485  
486 =cut
487  
488 0 ▶sub has_prop {
Subroutine has_prop: not called CC=1 SCAR=6.9
Pod documented
489 0 my ($self, $entry, $prop) = @_;
490 0 my @props = grep { $_ eq $prop } $self->props_for($entry);
491 0 return (@props == 1);
492 }
493  
494 =item prop_value($entry, $prop)
495  
496 Return the value of given property, if it exists. Otherwise return undef.
497  
498 =cut
499  
500 0 ▶sub prop_value {
Subroutine prop_value: not called CC=2 SCAR=17.9
Pod documented
501 0 my ($self, $entry, $prop) = @_;
502 0 ▶ if ($self->has_prop($entry, $prop)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_prop($entry, $prop)) 0 0 0
503 return file_get_contents(
504 0 $self->{entry_dir} . '/' . $entry . '/' . $prop . '.prop'
505 );
506 }
507 0 return undef;
508 }
509  
510 =item all_props()
511  
512 Return an array of all properties.
513  
514 =cut
515  
516 0 ▶sub all_props {
Subroutine all_props: not called CC=1 SCAR=6.9
Pod documented
517 0 my $self = shift;
518 0 return sort keys %{ $self->{property_entries} };
519 }
520  
521 =item is_extant($entry)
522  
523 Check if a given entry exists.
524  
525 =cut
526  
527 0 ▶sub is_extant {
Subroutine is_extant: not called CC=1 SCAR=6.9
Pod documented
528 0 my ($self, $entry) = @_;
529 0 return exists($self->{source_files}{$entry});
530 }
531  
532 =item is_dir($entry)
533  
534 Check if an entry is a directory.
535  
536 =cut
537  
538 0 ▶sub is_dir {
Subroutine is_dir: not called CC=2 SCAR=17.9
Pod documented
539 0 my ($self, $entry) = @_;
540 0 ▶ croak("No such entry: $entry") unless $self->is_extant($entry);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_extant($entry) 0 0 0
541 0 return ($self->{source_files}{$entry} == $ENTRYTYPE_DIR);
542 }
543  
544 =item is_file($entry)
545  
546 Check if an entry is a flatfile.
547  
548 =cut
549  
550 0 ▶sub is_file {
Subroutine is_file: not called CC=2 SCAR=17.9
Pod documented
551 0 my ($self, $entry) = @_;
552 0 ▶ croak("No such entry: $entry") unless $self->is_extant($entry);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_extant($entry) 0 0 0
553 0 return ($self->{source_files}{$entry} == $ENTRYTYPE_FILE);
554 }
555  
556 =item is_renderable($entry)
557  
558 Check if an entry path is, theoretically, renderable.
559  
560 =cut
561  
562 0 ▶sub is_renderable {
Subroutine is_renderable: not called CC=1 SCAR=6.9
Pod documented
563 0 my ($self, $entry) = @_;
564 0 return ($entry =~ $RENDERABLE_EXPR);
565 }
566  
567 =item has_index($entry)
568  
569 Check if an entry contains an index file.
570  
571 TODO: Should this care about the pathological (?) case where index is a
572 directory?
573  
574 =cut
575  
576 0 ▶sub has_index {
Subroutine has_index: not called CC=2 SCAR=17.9
Pod documented
577 0 my ($self, $entry) = @_;
578 0 ▶ croak("No such entry: $entry") unless $self->is_extant($entry);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_extant($entry) 0 0 0
579 0 return $self->is_extant($entry . '/index');
580 }
581  
582 =item basename($entry)
583  
584 Get a base name (i.e., filename without path) for a given entry.
585  
586 =cut
587  
588 0 ▶sub basename {
Subroutine basename: not called CC=1 SCAR=6.9
Pod documented
589 0 my ($self, $entry) = @_;
590 0 my @parts = split '/', $entry;
591 0 return pop @parts;
592 }
593  
594 =item dirname($entry)
595  
596 Get a directory name (i.e., directory without filename) for a given entry.
597  
598 =cut
599  
600 0 ▶sub dirname {
Subroutine dirname: not called CC=1 SCAR=6.9
Pod documented
601 0 my ($self, $entry) = @_;
602 0 my @parts = split '/', $entry;
603 0 pop @parts;
604 0 return join '/', @parts;
605 }
606  
607 =back
608  
609 =cut
610  
611 9 1;
« blib/lib/App/WRT/Markup.pm ↑ Summary blib/lib/App/WRT/Util.pm »