lib/Riji/Model/Blog.pm

filter: Statementstmt 89.8% 132 / 147 Branchbran 60.5% 23 / 38 Conditioncond 52.3% 11 / 21 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 93.7% 30 / 32 Podpod 0.0% 0 / 4 totaltotal 78.0% 199 / 255 CC 30 SCAR 21.3
CC
30
Cov
81%
_search_entries
26.5
entries
6.1
article
4.6
CRAP 8.4
« lib/Riji/CLI/Publish.pm ↑ Summary lib/Riji/CLI/NewEntry.pm »
1 package Riji::Model::Blog;
2 9 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Git::Repository 'FileHistory';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use List::UtilsBy qw/rev_sort_by rev_nsort_by sort_by/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Path::Tiny 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use IPC::Cmd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use Riji::Model::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Riji::Model::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Riji::Model::Tag;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 has base_dir => (is => 'ro', required => 1);
19 9 has author => (is => 'ro', required => 1);
20 9 has title => (is => 'ro', required => 1);
21 9 has site_url => (
22 is => 'ro',
23 isa => 'URI',
24 required => 1
25 );
26  
27 has fqdn => (
28 is => 'ro',
29 lazy => 1,
30 6 ▶ default => sub {shift->site_url->host},
Subroutine __ANON__: called CC=1 SCAR=0.0
31 9 );
32  
33 has tag_uri_specific_prefix => (
34 is => 'ro',
35 lazy => 1,
36 6 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
37 6 my $pref = shift->site_url->path;
38 6 $pref =~ s!/$!!;
39 6 $pref =~ s!^/!!;
40 6 $pref =~ s!/!-!g;
41 6 ▶ $pref .= ':' if $pref;
Branch: 1 branch
BranchTrueFalseTotal
if $pref 0 6 6
42 6 $pref;
43 },
44 9 );
45  
46 9 has article_dir => (
47 is => 'ro',
48 default => 'article',
49 );
50  
51 9 has entry_dir => (
52 is => 'ro',
53 default => 'article/entry',
54 );
55  
56 has article_path => (
57 is => 'ro',
58 lazy => 1,
59 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 my $self = shift;
61 0 path($self->base_dir, $self->article_dir);
62 },
63 9 );
64  
65 has entry_path => (
66 is => 'ro',
67 lazy => 1,
68 8 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 8 my $self = shift;
70 8 path($self->base_dir, $self->entry_dir);
71 },
72 9 );
73  
74 has repo => (
75 is => 'ro',
76 lazy => 1,
77 7 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 7 my $self = shift;
79 7 Git::Repository->new(work_tree => $self->base_dir);
80 },
81 9 );
82  
83 has branch => (
84 is => 'ro',
85 9 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
86 9 ▶ my $git = IPC::Cmd::can_run('git') or return 'master';
Branch: 1 branch
BranchTrueFalseTotal
my $git = IPC::Cmd::can_run("git") or return "master" 9 0 9
87 9 my $ret = IPC::Cmd::run_forked(join(' ', $git, 'config', 'init.defaultBranch'));
88 9 ▶ if ($ret->{exit_code} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->{"exit_code"} == 0) 0 9 9
89 0 chomp($ret->{stdout});
90 0 return $ret->{stdout};
91 }
92 9 'master';
93 },
94 9 );
95  
96 has atom => (
97 is => 'ro',
98 lazy => 1,
99 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 6 my $self = shift;
101 6 Riji::Model::Atom->new(
102 blog => $self
103 );
104 },
105 9 );
106  
107 has tag_map => (
108 is => 'ro',
109 isa => 'HashRef[Riji::Model::Tag]',
110 lazy => 1,
111 6 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
112 6 my $self = shift;
113 6 my %map;
114 6 for my $entry (@{ $self->entries }) {
115 14 for my $tag (@{ $entry->raw_tags }) {
116 4 ▶ $map{$tag} ||= Riji::Model::Tag->new(name => $tag);
Condition33%1/3
$map{$tag} ||= "Riji::Model::Tag"->new("name", $tag)
l!l&&r!l&&!r
040
MC/DC0%0/2
$map{$tag} ||= "Riji::Model::Tag"->new("name", $tag)
$map{$tag} "Riji::Model::Tag"->new("name", $tag)
Truth table33%1/3
A ||= B
A= $map{$tag}B= "Riji::Model::Tag"->new("name", $tag)
ABresult
1X1
011
000
117 4 push @{$map{$tag}->entries}, $entry;
118 }
119 }
120 6 \%map;
121 },
122 9 );
123  
124 has tags => (
125 is => 'ro',
126 isa => 'ArrayRef[Riji::Model::Tag]',
127 lazy => 1,
128 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
129 6 ▶ [rev_nsort_by {$_->count} sort_by {$_->name} values %{ shift->tag_map }]
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
130 },
131 9 );
132  
133 9 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 49 ▶sub entries {
Subroutine entries: called CC=6 SCAR=18.0
Pod undocumented
136 49 my ($self, @args) = @_;
137  
138 $self->{entries} ||= [
139 14 ▶ rev_sort_by { $_->published_at->datetime . $_->file }
Subroutine __ANON__: called CC=1 SCAR=0.0
140 14 ▶ grep { $_ && !$_->is_draft }
Condition33%1/3
$_ && !$_->is_draft
!ll&&!rl&&r
0014
MC/DC0%0/2
$_ && !$_->is_draft
$_ !$_->is_draft
Truth table33%1/3
A && not(B)
A= $_B= !$_->is_draft
ABresult
0X0
100
111
141 14 map { $self->entry($_->relative($self->entry_path) .'') }
142 49 ▶ do {
Condition100%2/2
$self->{"entries"} ||= [&rev_sort_by(sub { $_->published_at->datetime . $_->file; } , grep({$_ and not $_->is_draft;} map({$self->entry($_->relative($self->entry_path) . '');} do { my $itr = $self->entry_path->iterator({"recurse", 1}); my @files; while (my $file = &$itr()) { do { next unless -f -r $file; push @files, $file }; } ; @files })))]
l!l
436
MC/DC100%1/1
$self->{"entries"} ||= [&rev_sort_by(sub { $_->published_at->datetime . $_->file; } , grep({$_ and not $_->is_draft;} map({$self->entry($_->relative($self->entry_path) . '');} do { my $itr = $self->entry_path->iterator({"recurse", 1}); my @files; while (my $file = &$itr()) { do { next unless -f -r $file; push @files, $file }; } ; @files })))]
$self->{"entries"}
Truth table100%2/2
A
A= $self->{"entries"}
Aresult
11
00
143 6 my $itr = $self->entry_path->iterator({recurse => 1});
144 6 my @files;
145 6 ▶ while (my $file = $itr->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = &$itr()) 14 6 20
146 14 ▶ next unless -f -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f -r $file 0 14 14
147 14 push @files, $file;
148 }
149 6 @files;
150 }
151 ];
152 49 ▶ return $self->{entries} unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 37 12 49
153  
154 12 $self->_search_entries(@args);
155 }
156  
157 28 ▶sub entry {
Subroutine entry: called CC=2 SCAR=7.0
Pod undocumented
158 28 my ($self, $file) = @_;
159  
160 28 my $entry = Riji::Model::Entry->new(
161 file => File::Spec->catfile('entry', $file),
162 blog => $self,
163 );
164 28 ▶ return () unless -f -r $entry->file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f -r $entry->file_path 0 28 28
165  
166 28 $entry;
167 }
168  
169 12 ▶sub article {
Subroutine article: called CC=4 SCAR=15.2
Pod undocumented
170 12 my ($self, $file, $opt) = @_;
171  
172 12 my $article = Riji::Model::Article->new(
173 file => $file,
174 blog => $self,
175 %$opt,
176 );
177 12 ▶ return () unless -f -r $article->file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f -r $article->file_path 0 12 12
178 12 ▶ return () if !$article->paginate && $article->page;
Branch: 1 branch
BranchTrueFalseTotal
if not $article->paginate and $article->page 0 12 12
Condition33%1/3
not $article->paginate and $article->page
!ll&&!rl&&r
0120
MC/DC0%0/2
not $article->paginate and $article->page
not $article->paginate $article->page
Truth table33%1/3
not(A) and B
A= not $article->paginateB= $article->page
ABresult
0X0
100
111
179  
180 12 $article;
181 }
182  
183 8 ▶sub tag {
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
184 8 my ($self, $tag) = @_;
185  
186 8 $tag = Riji::Model::Tag->normalize_tag($tag);
187 8 $self->tag_map->{$tag};
188 }
189  
190 # _search_entries(tag => 'hoge', sort_by => 'last_updated_at', sort_order => 'desc', limit => 10);
191 12 ▶sub _search_entries {
Subroutine _search_entries: called CC=15 SCAR=32.8
192 12 my $self = shift;
193 0 ▶ my %opt = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 12 12
194 12 my @entries = @{ $self->entries };
195  
196 12 ▶ if (my $tag = $opt{tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $opt{"tag"}) 0 12 12
197 0 @entries = grep { grep {$_ eq $tag} @{ $_->raw_tags } } @entries;
198 }
199  
200 12 ▶ if (my $sort_by = $opt{sort_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sort_by = $opt{"sort_by"}) 6 6 12
201 6 my @enable_fields = qw/published_at last_modified_at title/;
202 6 ▶ if (grep {$sort_by eq $_} @enable_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$sort_by eq $_;} @enable_fields) { } 6 0 6
203 6 ▶ if ($sort_by eq 'last_modified_at') {
Branch: 2 branches
BranchTrueFalseTotal
if ($sort_by eq 'last_modified_at') { } 6 0 6
elsif ($sort_by ne 'published_at') { } 0 0 0
204 6 ▶ @entries = rev_sort_by {$_->last_modified_at->datetime . $_->title} @entries;
Subroutine __ANON__: called CC=1 SCAR=0.0
205 }
206 elsif ($sort_by ne 'published_at') {
207 0 ▶ @entries = rev_sort_by {$_->title} @entries;
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 }
209 }
210 else {
211 0 warn "$sort_by is unknown sort item";
212 }
213 }
214  
215 12 ▶ my $sort_order = lc($opt{sort_order} || '');
Condition50%1/2
$opt{'sort_order'} || ''
l!l
012
MC/DC0%0/1
$opt{'sort_order'} || ''
$opt{'sort_order'}
Truth table50%1/2
A
A= $opt{'sort_order'}
Aresult
11
00
216 0 ▶ if ($sort_order && ! grep {$sort_order eq $_} qw/asc desc/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_order and not grep({$sort_order eq $_;} "asc", "desc")) 0 12 12
Condition33%1/3
$sort_order and not grep({$sort_order eq $_;} "asc", "desc")
!ll&&!rl&&r
1200
MC/DC0%0/2
$sort_order and not grep({$sort_order eq $_;} "asc", "desc")
$sort_order not grep({$sort_order eq $_;} "asc", "desc")
Truth table33%1/3
A and not(B)
A= $sort_orderB= not grep({$sort_order eq $_;} "asc", "desc")
ABresult
0X0
100
111
217 0 warn "$sort_order is unknown sort_order";
218 0 $sort_order = undef;
219 }
220 12 ▶ if ($opt{sort_by} && !$sort_order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"sort_by"} and not $sort_order) 6 6 12
Condition66%2/3
$opt{"sort_by"} and not $sort_order
!ll&&!rl&&r
606
MC/DC50%1/2
$opt{"sort_by"} and not $sort_order
$opt{"sort_by"} not $sort_order
Truth table66%2/3
A and not(B)
A= $opt{"sort_by"}B= not $sort_order
ABresult
0X0
100
111
221 $sort_order = {
222 last_modified_at => 'desc',
223 published_at => 'desc',
224 title => 'asc',
225 6 }->{$opt{sort_by}};
226 }
227 12 ▶ $sort_order ||= 'desc';
Condition100%2/2
$sort_order ||= "desc"
l!l
66
MC/DC100%1/1
$sort_order ||= "desc"
$sort_order
Truth table100%2/2
A
A= $sort_order
Aresult
11
00
228 12 ▶ @entries = reverse @entries if $sort_order eq 'asc';
Branch: 1 branch
BranchTrueFalseTotal
if $sort_order eq "asc" 0 12 12
229  
230 12 ▶ if (my $limit = $opt{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit = $opt{"limit"}) 6 6 12
231 6 @entries = splice @entries, 0, $limit;
232 }
233 12 [@entries];
234 }
235  
236 9 1;
« lib/Riji/CLI/Publish.pm ↑ Summary lib/Riji/CLI/NewEntry.pm »