File Coverage

blib/lib/App/dategrep/Iterators.pm
Criterion Covered Total %
statement 50 50 100.0
branch 9 10 90.0
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 70 75 93.3


line stmt bran cond sub pod time code
1             package App::dategrep::Iterators;
2 9     9   31 use strict;
  9         12  
  9         228  
3 9     9   27 use warnings;
  9         11  
  9         216  
4 9     9   4173 use Moo;
  9         83066  
  9         44  
5 9     9   9317 use App::dategrep::Date qw(date_to_epoch);
  9         15  
  9         332  
6 9     9   3237 use App::dategrep::Iterator::File;
  9         25  
  9         269  
7 9     9   3558 use App::dategrep::Iterator::Stdin;
  9         16  
  9         214  
8 9     9   3057 use App::dategrep::Iterator::Uncompress;
  9         24  
  9         3150  
9              
10             has iterators => ( is => 'rw', default => sub { [] } );
11              
12             sub as_array {
13 26     26 0 29 return @{ shift->iterators };
  26         109  
14             }
15              
16             sub BUILDARGS {
17 29     29 0 6933 my ( $class, %options ) = @_;
18             my @filenames =
19 29 50       101 ref $options{filenames} ? @{ $options{filenames} } : $options{filenames};
  29         82  
20             my @args = (
21             start => $options{start},
22             end => $options{end},
23             multiline => $options{multiline},
24             format => $options{format},
25 29         118 skip_unparsable => $options{'skip-unparsable'},
26             );
27 29 100       81 push @args, blocksize => $options{blocksize} if defined $options{blocksize};
28              
29 29         60 my @iterators;
30 29         51 for my $filename (@filenames) {
31 33 100       178 if ( $filename eq '-' ) {
    100          
32 3         54 push @iterators, App::dategrep::Iterator::Stdin->new(@args);
33             }
34             elsif ( $filename =~ /\.(bz|bz2|gz|z)$/ ) {
35 4         82 push @iterators,
36             App::dategrep::Iterator::Uncompress->new( @args,
37             filename => $filename );
38             }
39             else {
40 26         461 push @iterators,
41             App::dategrep::Iterator::File->new( @args,
42             filename => $filename );
43             }
44             }
45 27         647 return { iterators => \@iterators };
46             }
47              
48             sub sort {
49 8     8 0 7 my $self = shift;
50              
51 8         9 my @iterators = @{ $self->iterators };
  8         17  
52              
53             @iterators =
54 13         30 sort { $a->next_date <=> $b->next_date }
55 8         14 grep { !$_->eof } @iterators;
  20         49  
56              
57 8         39 $self->iterators( \@iterators );
58              
59 8         22 return;
60             }
61              
62             sub interleave {
63 1     1 0 2 my $self = shift;
64              
65 1         3 while ( $self->sort, $self->iterators->[0] ) {
66 6         4 my $until;
67 6 100       15 if ( $self->iterators->[1] ) {
68 5         7 $until = $self->iterators->[1]->next_date;
69             }
70 6         17 $self->iterators->[0]->print($until);
71             }
72 1         3 return;
73             }
74              
75             1;