File Coverage

blib/lib/Bootylicious/FileIteratorLoader.pm
Criterion Covered Total %
statement 41 41 100.0
branch 4 4 100.0
condition 4 6 66.6
subroutine 9 9 100.0
pod 0 3 0.0
total 58 63 92.0


line stmt bran cond sub pod time code
1             package Bootylicious::FileIteratorLoader;
2              
3 14     14   1167 use strict;
  14         22  
  14         610  
4 14     14   68 use warnings;
  14         22  
  14         532  
5              
6 14     14   72 use base 'Mojo::Base';
  14         19  
  14         4993  
7              
8             __PACKAGE__->attr('element_class');
9             __PACKAGE__->attr('filter');
10             __PACKAGE__->attr('root');
11             __PACKAGE__->attr('path');
12              
13 14     14   73971 use Mojo::ByteStream;
  14         503311  
  14         744  
14 14     14   6507 use Mojo::Loader qw(load_class);
  14         44719  
  14         977  
15 14     14   3112 use Bootylicious::Iterator;
  14         30  
  14         116  
16              
17             sub files {
18 58     58 0 107 my $self = shift;
19              
20 58         535 my $root = $self->root;
21 58         658 my $path = $self->path;
22              
23 58 100       20634 return glob $root ? "$root/*" : "$path*";
24             }
25              
26             sub create_element {
27 93     93 0 147 my $self = shift;
28              
29 93         309 load_class($self->element_class);
30              
31 93         2149 return $self->element_class->new;
32             }
33              
34             sub load {
35 58     58 0 4590 my $self = shift;
36 58         101 my $iterator = shift;
37              
38 58   66     721 $iterator ||= Bootylicious::Iterator->new;
39              
40 58         294 my $filter = $self->filter;
41              
42 58         227 my @elements = ();
43 58         436 foreach my $file ($self->files) {
44 113         817 $file = Mojo::ByteStream->new($file)->decode('UTF-8')->to_string;
45              
46 113         15912 my $basename = File::Basename::basename($file);
47 113 100 66     1720 next if $filter && $basename !~ m/$filter/;
48              
49 93         474 my $element = $self->create_element;
50 93         976 $element->load($file);
51              
52 93         272 push @elements, $element;
53             }
54              
55             $iterator->elements(
56 58         435 [sort { $b->created->epoch <=> $a->created->epoch } @elements]);
  55         337  
57 58         811 $iterator->rewind;
58              
59 58         429 return $iterator;
60             }
61              
62             1;