File Coverage

/root/.cpan/build/Riji-v1.1.4-0/lib/Riji/Model/Entry.pm
Criterion Covered Total %
statement 41 41 100.0
branch 4 6 66.6
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 56 58 96.5


line stmt bran cond sub pod time code
1             package Riji::Model::Entry;
2 9     9   65 use feature ':5.10';
  9         20  
  9         1542  
3 9     9   55 use strict;
  9         17  
  9         203  
4 9     9   35 use warnings;
  9         16  
  9         609  
5              
6 9     9   976 use HTTP::Date;
  9         4929  
  9         727  
7 9     9   58 use Time::Piece;
  9         13  
  9         98  
8 9     9   954 use URI::tag;
  9         18  
  9         273  
9              
10 9     9   4820 use Mouse;
  9         282715  
  9         47  
11              
12             extends 'Riji::Model::Article';
13              
14             has repo_path => (
15             is => 'ro',
16             lazy => 1,
17             default => sub {
18             my $self = shift;
19             my $repo_dir = $self->repo->run(qw/rev-parse --show-toplevel/);
20             $self->file_path->relative($repo_dir)
21             },
22             );
23              
24             has file_history => (
25             is => 'ro',
26             lazy => 1,
27             default => sub {
28             my $self = shift;
29             $self->repo->file_history($self->repo_path.'', {branch => $self->blog->branch});
30             },
31             handles => [qw/created_by last_modified_by/],
32             );
33              
34             has tag_uri => (
35             is => 'ro',
36             lazy => 1,
37             default => sub {
38             my $self = shift;
39              
40             my $tag_uri = URI->new('tag:');
41             $tag_uri->authority($self->fqdn);
42             $tag_uri->date($self->created_at->strftime('%Y-%m-%d'));
43             $tag_uri->specific($self->blog->tag_uri_specific_prefix . join('-', grep {$_ ne ''} split(m{/}, $self->site_path)));
44              
45             $tag_uri;
46             },
47             );
48              
49             has next => (
50             is => 'rw',
51             lazy => 1,
52             default => sub {
53             my $self = shift;
54             my ($prev, $next) = $self->_search_prev_and_next;
55             $self->prev($prev);
56             $next;
57             },
58             );
59              
60             has prev => (
61             is => 'rw',
62             lazy => 1,
63             default => sub {
64             my $self = shift;
65             my ($prev, $next) = $self->_search_prev_and_next;
66             $self->next($next);
67             $prev;
68             },
69             );
70              
71             has last_modified_at => (
72             is => 'ro',
73             lazy => 1,
74             default => sub {
75             my $self = shift;
76              
77             my $updated_at = $self->updated_at;
78             my $published_at = $self->published_at;
79             $published_at > $updated_at ? $published_at : $updated_at;
80             },
81             );
82              
83             has created_at => (
84             is => 'ro',
85             lazy => 1,
86             default => sub {
87             localtime($_[0]->file_history->created_at);
88             },
89             );
90              
91             has updated_at => (
92             is => 'ro',
93             lazy => 1,
94             default => sub {
95             localtime($_[0]->file_history->updated_at);
96             },
97             );
98              
99             has published_at => (
100             is => 'ro',
101             lazy => 1,
102             default => sub {
103             my $self = shift;
104             if (my $pubdate = $self->header('pubdate')) {
105             return localtime(str2time($pubdate));
106             }
107             $self->created_at;
108             }
109             );
110              
111 9     9   14792 no Mouse;
  9         23  
  9         56  
112              
113             sub BUILD {
114 28     28 1 78 my $self = shift;
115 28 50       218 return unless $self->file_history;
116              
117             # surely assign them
118 28         1195128 $self->last_modified_at;
119 28         1912 $self->created_at;
120             }
121              
122             sub _search_prev_and_next {
123 13     13   60 my $self = shift;
124 13         67 my ($prev, $next);
125              
126 13         0 my $found;
127 13         49 my @entries = @{ $self->blog->entries };
  13         172  
128 13         142 while (my $entry = shift @entries) {
129 24 100       187 if ($entry->file eq $self->file) {
130 13         38 $prev = shift @entries;
131 13         57 $found++; last;
  13         51  
132             }
133 11         85 $next = $entry;
134             }
135 13 50       67 return () unless $found;
136 13         67 ($prev, $next);
137             }
138              
139             1;