File Coverage

blib/lib/App/dategrep/Iterators.pm
Criterion Covered Total %
statement 50 51 98.0
branch 8 10 80.0
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 69 76 90.7


line stmt bran cond sub pod time code
1             package App::dategrep::Iterators;
2 8     8   30 use strict;
  8         9  
  8         206  
3 8     8   28 use warnings;
  8         8  
  8         198  
4 8     8   3584 use Moo;
  8         73382  
  8         32  
5 8     8   7978 use App::dategrep::Date qw(date_to_epoch);
  8         16  
  8         290  
6 8     8   2892 use App::dategrep::Iterator::File;
  8         21  
  8         243  
7 8     8   3081 use App::dategrep::Iterator::Stdin;
  8         14  
  8         191  
8 8     8   2710 use App::dategrep::Iterator::Uncompress;
  8         24  
  8         3099  
9              
10             has iterators => ( is => 'rw', default => sub { [] } );
11              
12             sub as_array {
13 34     34 0 31 return @{ shift->iterators };
  34         170  
14             }
15              
16             sub BUILDARGS {
17 27     27 0 6190 my ( $class, %options ) = @_;
18             my @filenames =
19 27 50       84 ref $options{filenames} ? @{ $options{filenames} } : $options{filenames};
  27         64  
20             my @args = (
21             start => $options{start},
22             end => $options{end},
23             multiline => $options{multiline},
24             format => $options{format},
25 27         95 skip_unparsable => $options{'skip-unparsable'},
26             );
27 27         27 my @iterators;
28 27         98 for my $filename (@filenames) {
29 31 100       1519 if ( $filename eq '-' ) {
    100          
30 3         41 push @iterators, App::dategrep::Iterator::Stdin->new(@args);
31             }
32             elsif ( $filename =~ /\.(bz|bz2|gz|z)$/ ) {
33 4         78 push @iterators,
34             App::dategrep::Iterator::Uncompress->new( @args,
35             filename => $filename );
36             }
37             else {
38 24         369 push @iterators,
39             App::dategrep::Iterator::File->new( @args,
40             filename => $filename );
41             }
42             }
43 27         7377 return { iterators => \@iterators };
44             }
45              
46             sub sort {
47 8     8 0 9 my $self = shift;
48              
49 8         8 my @timestamps;
50 8         13 for my $iterator ( $self->as_array ) {
51 20         59 my $entry = $iterator->peek_entry;
52              
53             ## remove all iterators with eof
54 20 100       36 next if not defined $entry;
55              
56 17         45 my ( $epoch, $error ) = date_to_epoch( $entry, $iterator->format );
57 17 50       3052 if ( !$epoch ) {
58             ## TODO Which iterator produced the error?
59 0         0 die "No date found in first line: $error\n";
60             }
61 17         33 push @timestamps, [ $epoch, $iterator ];
62             }
63             $self->iterators(
64 8         28 [ map { $_->[1] } sort { $a->[0] <=> $b->[0] } @timestamps ] );
  17         57  
  13         22  
65 8         25 return;
66             }
67              
68             sub interleave {
69 1     1 0 2 my $self = shift;
70 1         3 while ( $self->sort, $self->iterators->[0] ) {
71 6         24 print $self->iterators->[0]->get_entry;
72             }
73 1         4 return;
74             }
75              
76             1;