File Coverage

blib/lib/Sweat/ArticleHandler.pm
Criterion Covered Total %
statement 24 35 68.5
branch 0 2 0.0
condition n/a
subroutine 8 10 80.0
pod 0 2 0.0
total 32 49 65.3


line stmt bran cond sub pod time code
1             package Sweat::ArticleHandler;
2              
3 4     4   29 use warnings;
  4         8  
  4         141  
4 4     4   24 use strict;
  4         5  
  4         77  
5 4     4   18 use Moo;
  4         8  
  4         37  
6 4     4   11069 use namespace::clean;
  4         9  
  4         41  
7 4     4   1131 use utf8::all;
  4         9  
  4         42  
8              
9 4     4   6329 use Types::Standard qw( Str Maybe Int );
  4         9  
  4         67  
10              
11 4     4   4408 use Storable qw(freeze thaw);
  4         9  
  4         268  
12 4     4   3777 use Path::Tiny;
  4         46704  
  4         2118  
13              
14             has 'tempdir' => (
15             is => 'ro',
16             isa => sub { $_[0]->isa('Path::Tiny') },
17             default => sub { Path::Tiny->tempdir( CLEANUP => 1 ) },
18             );
19              
20             has 'next_filename' => (
21             is => 'rw',
22             isa => Int,
23             default => 1,
24             );
25              
26             has 'last_article_read' => (
27             is => 'rw',
28             isa => Int,
29             default => 0,
30             );
31              
32             sub add_article {
33 0     0 0   my ($self, $article) = @_;
34 0           my $file = path( $self->tempdir, $self->next_filename );
35 0           $self->next_filename( $self->next_filename + 1 );
36              
37 0           $file->spew( freeze( $article ) );
38              
39             # warn "Writing about " . $article->title . " to $file.\n";
40             }
41              
42             sub next_article {
43 0     0 0   my $self = shift;
44              
45 0           my $next_filename = $self->last_article_read + 1;
46 0           my $next_file = path( $self->tempdir, $next_filename );
47              
48 0 0         if ( -e $next_file ) {
49             # warn "Loading from $next_file\n";
50 0           $self->last_article_read( $self->last_article_read + 1 );
51 0           return thaw( $next_file->slurp );
52             }
53             else {
54             # warn "No file at $next_file, chief.\n";
55 0           return undef;
56             }
57             }
58              
59             1;